Emit-XML

Emit-XML

  • Comments 10

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

Leave a Comment
  • Please add 4 and 1 and type the answer here:
  • Post
  • 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

  • PingBack from http://www.alvinashcraft.com/2008/10/18/dew-drop-october-18-2008/

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

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

    No.

    jps

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

  • > 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

  • Two questions:

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

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

  • > 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

  • 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.

  • No codename for Version 2.

    jps

Page 1 of 1 (10 items)