Welcome to MSDN Blogs Sign in | Join | Help

Emit-XML

I was writing a demo yesterday and needed a quick and dirty way to generate some XML so I wrote the function below.  This highlights a couple of changes that are coming up in CTP3 that I thought I would preview.

1) The keyword "cmdlet" is going away and we'll just have "function".  Notice that now you can specify the [Parameter()]  attribute on parameters.  When you do that, we treat the function like a cmdlet.
2) Here-strings can now NEST!  Look at how simple the code becomes when you do this.  I think you are going to find this to be one of the more important and powerful new features of PS V2.  It provides incredible power in a very pithy (and natural) way.

The first Here-String starts the XML document with a "<Object>".   Because we use a HERE-STRING with double (vs single) quotes, we expand variables $var and subexpressions $( statement;statement; ...).  The next thing is an expression which contains a foreach.  That calculates the name and value of the property and then uses an embedded Here-String to emit that text.

 

function Emit-XML
{
Param ([Parameter(Mandatory=$true,
   ValueFromPipeline=$true)]$object)

@"
<Object>
$(    
    foreach ($p in $object |Get-Member -type *Property)
    {
        $Name  = $p.Name
        $Value = $Object.$Name   
        @"
`t<$Name>$Value</$Name>`n
"@
     }
)
</Object>
"@
}

 

Here is an usage example:

PS> gsv alg | Emit-xml
<Object>
        <Name>ALG</Name>
        <CanPauseAndContinue>False</CanPauseAndContinue>
        <CanShutdown>False</CanShutdown>
        <CanStop>False</CanStop>
        <Container></Container>
        <DependentServices></DependentServices>
        <DisplayName>Application Layer Gateway Service</DisplayName>
        <MachineName>.</MachineName>
        <ServiceHandle>SafeServiceHandle</ServiceHandle>
        <ServiceName>ALG</ServiceName>
        <ServicesDependedOn></ServicesDependedOn>
        <ServiceType>Win32OwnProcess</ServiceType>
        <Site></Site>
        <Status>Stopped</Status>
</Object>

I have to admit - I am VERY excited by some of the stuff we are lined up to deliver to you in V2.  You take that kind of power and give it to this community and exciting things are going to happen.  I can't wait to see all the things you guys do with this stuf!

Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Published Saturday, October 18, 2008 4:03 AM by PowerShellTeam

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Emit-XML

I think removing the cmdlet keyword is a very good decision. It reduces complexity of the language and makes PowerShell a more consistent environment (cmdlets in the function provider?!). Are you still going to enforce verb-noun names for these "funclets"?

PS: Shouldn't Emit-xml be called ConvertTo-XML or Out-XML?

--

greetings

dreeschkind

Saturday, October 18, 2008 9:05 AM by dreeschkind

# Dew Drop - October 18, 2008 | Alvin Ashcraft's Morning Dew

Saturday, October 18, 2008 9:59 AM by Dew Drop - October 18, 2008 | Alvin Ashcraft's Morning Dew

# re: Emit-XML

Haha, except ... you have to! (Wait, I mean)  At least we get to know you're impatient too ;)

Saturday, October 18, 2008 3:32 PM by Jaykul

# re: Emit-XML

> Are you still going to enforce verb-noun names for these "funclets

No.

jps

Saturday, October 18, 2008 6:12 PM by PowerShellTeam

# re: Emit-XML

Hah! You've just re-invented CGI and ASP!

Saturday, October 18, 2008 6:23 PM by bobhy

# re: Emit-XML

> Hah! You've just re-invented CGI and ASP!

Yup.  A good idea is a good idea.  

There are a lot more scenarios for creating documents (e.g. XML) than Web applications though.

jps

Saturday, October 18, 2008 10:31 PM by PowerShellTeam

# re: Emit-XML

Two questions:

1. When you say "CMDLETS go away", does that mean totally?

2. When do you expect to release CTP3? :-)

Friday, October 24, 2008 9:31 AM by tfl

# re: Emit-XML

> 1. When you say "CMDLETS go away", does that mean totally?

The language keyword we introduced in CTP2 will go away and be replaced with an model which just uses FUNCTION.

> 2. When do you expect to release CTP3? :-)

Certainly before the end of the year.

jps

Friday, October 24, 2008 5:41 PM by PowerShellTeam

# re: Emit-XML

What is V2 codename? or, does V2 have codename?

Because of it, i'm not possible to take a nap after i get up in this morning.

Saturday, October 25, 2008 2:57 AM by Smith Catar

# re: Emit-XML

No codename for Version 2.

jps

Saturday, October 25, 2008 11:45 AM by PowerShellTeam

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker