{"id":17876,"date":"2008-10-17T12:52:48","date_gmt":"2008-10-17T20:52:48","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/powershell\/?p=17876"},"modified":"2019-06-07T12:54:00","modified_gmt":"2019-06-07T20:54:00","slug":"emit-xml","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/emit-xml\/","title":{"rendered":"Emit-XML"},"content":{"rendered":"<p><P>I was writing a demo&nbsp;yesterday and needed a quick and dirty&nbsp;way to generate some XML so I wrote the function below.&nbsp;&nbsp;This&nbsp;highlights a couple of changes that are coming up in CTP3 that I thought I would preview.<\/P>\n<P>1)&nbsp;The keyword &#8220;cmdlet&#8221;&nbsp;is going away and we&#8217;ll just have &#8220;function&#8221;.&nbsp; Notice that now you can specify the [Parameter()]&nbsp;&nbsp;attribute on parameters.&nbsp; When you do that, we treat the function like a cmdlet.<BR>2) Here-strings can now NEST!&nbsp; Look at how simple the code becomes when you do this.&nbsp; I think you are going to find this to be one of the more important and powerful new features of PS V2.&nbsp; It provides incredible power in a very pithy (and natural) way.<\/P>\n<BLOCKQUOTE>\n<P>The first Here-String starts the XML document with a &#8220;&lt;Object&gt;&#8221;.&nbsp;&nbsp; Because we use a HERE-STRING with double (vs single) quotes, we expand variables $var and subexpressions $( statement;statement; &#8230;).&nbsp; The next thing is an expression which contains a foreach.&nbsp; That calculates the name and value of the property and then uses an embedded Here-String to emit that text.<\/P><\/BLOCKQUOTE>\n<P mce_keep=\"true\">&nbsp;<\/P>\n<P>function Emit-XML<BR>{<BR>Param ([Parameter(Mandatory=$true,<BR>&nbsp;&nbsp; ValueFromPipeline=$true)]$object)<\/P>\n<P>@&#8221;<BR>&lt;Object&gt;<BR>$(&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp; foreach ($p in $object |Get-Member -type *Property)<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $Name&nbsp; = $p.Name<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $Value = $Object.$Name&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @&#8221;<BR>`t&lt;$Name&gt;$Value&lt;\/$Name&gt;`n<BR>&#8220;@<BR>&nbsp;&nbsp;&nbsp;&nbsp; }<BR>)<BR>&lt;\/Object&gt;<BR>&#8220;@<BR>}<\/P>\n<P mce_keep=\"true\">&nbsp;<\/P>\n<P>Here is an usage example:<\/P>\n<P>PS&gt; gsv alg | Emit-xml<BR>&lt;Object&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Name&gt;ALG&lt;\/Name&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;CanPauseAndContinue&gt;False&lt;\/CanPauseAndContinue&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;CanShutdown&gt;False&lt;\/CanShutdown&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;CanStop&gt;False&lt;\/CanStop&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Container&gt;&lt;\/Container&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;DependentServices&gt;&lt;\/DependentServices&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;DisplayName&gt;Application Layer Gateway Service&lt;\/DisplayName&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;MachineName&gt;.&lt;\/MachineName&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ServiceHandle&gt;SafeServiceHandle&lt;\/ServiceHandle&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ServiceName&gt;ALG&lt;\/ServiceName&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ServicesDependedOn&gt;&lt;\/ServicesDependedOn&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ServiceType&gt;Win32OwnProcess&lt;\/ServiceType&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Site&gt;&lt;\/Site&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Status&gt;Stopped&lt;\/Status&gt;<BR>&lt;\/Object&gt;<BR><\/P>\n<P>I have to admit &#8211; I am VERY excited by some of the stuff we are lined up to deliver to you in V2.&nbsp; You take that kind of power and give it to this community and exciting things are going to happen.&nbsp; I can&#8217;t wait to see all the things you guys do with this stuf!<\/P>\n<P>Jeffrey Snover [MSFT]<BR>Windows Management Partner Architect<BR>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <A href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/A><BR>Visit the Windows PowerShell ScriptCenter at:&nbsp; <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/A><BR><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was writing a demo&nbsp;yesterday and needed a quick and dirty&nbsp;way to generate some XML so I wrote the function below.&nbsp;&nbsp;This&nbsp;highlights a couple of changes that are coming up in CTP3 that I thought I would preview. 1)&nbsp;The keyword &#8220;cmdlet&#8221;&nbsp;is going away and we&#8217;ll just have &#8220;function&#8221;.&nbsp; Notice that now you can specify the [Parameter()]&nbsp;&nbsp;attribute [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[97,137,157],"class_list":["post-17876","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-advanced-functions","tag-ctp3","tag-emit-xml"],"acf":[],"blog_post_summary":"<p>I was writing a demo&nbsp;yesterday and needed a quick and dirty&nbsp;way to generate some XML so I wrote the function below.&nbsp;&nbsp;This&nbsp;highlights a couple of changes that are coming up in CTP3 that I thought I would preview. 1)&nbsp;The keyword &#8220;cmdlet&#8221;&nbsp;is going away and we&#8217;ll just have &#8220;function&#8221;.&nbsp; Notice that now you can specify the [Parameter()]&nbsp;&nbsp;attribute [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/17876","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=17876"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/17876\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=17876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=17876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=17876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}