2014/09/05

PowerShell - Playing with the new ConvertFrom-String cmdlet

In a previous post I talked about parsing NetStat.exe using PowerShell and some regex, It is a fun exercice but require some knowledge to figure out how the parsing should happen.

Today, It got way easier !! The PowerShell Team just released a new version of the WMF : v5 September preview ! And One of the coolest feature is the new ConvertFrom-String cmdlet.

EDIT (2014/10/02): See also my post about using ConvertFrom-String and the param -TemplateFile against Netstat.exe

Using the same example from my previous post, I will perform a simple parsing of netstat.exe -n and send the output to ConvertFrom-String.

Important: This post is based on the September 2014 preview release of WMF 5.0. This is pre-release software, so this information may change.


Note: $netstat variable is holding the output of netstat.exe -n without the 5 first lines, you can refer to my previous post to see how to do this step.


Playing with ConvertFrom-String


Passing the Output of netstat to ConvertFrom-String, the cmdlet is parsing on the whitespace by default, but you can also specify it using the parameter -Delimiter
$netstat | ConvertFrom-String


The first property is empty since the output of netstat starts with a couple of whitespaces, the parsing interpret it as an empty property.



Adding some Properties


Next we can specify the Properties names to make it more explicit
None is used for my first empty property called P1 in the previous example.
$netstat | ConvertFrom-String -PropertyNames None,Protocol,LocalIP,RemoteIP,Status



To remove the first empty property, we can modify the initial $netstat by removing the whitespace at the beginning of each line. Regex magic: '^\s+'

$netstat -replace '^\s+' | ConvertFrom-String -PropertyNames Protocol,LocalIP,RemoteIP,Status





That's all for today, in a next post I will work with the new parameters TemplateFile and TemplateContent parameters.



Extra: Help Content

Current help content

Synopsis
    
    ConvertFrom-String -InputObject <string> [-Delimiter <string>] [-PropertyNames <string[]>] [<CommonParameters>]
    
    ConvertFrom-String -InputObject <string> [-TemplateFile <string>] [-TemplateContent <string>] [<CommonParameters>]
    

Syntax
    ConvertFrom-String -InputObject <string> [-Delimiter <string>] [-PropertyNames <string[]>] [<CommonParameters>]

    ConvertFrom-String -InputObject <string> [-TemplateFile <string>] [-TemplateContent <string>] [<CommonParameters>]


Parameters
    -Delimiter <string>

        Required?                    false
        Position?                    Named
        Default value                
        Accept pipeline input?       false
        Accept wildcard characters?  

    -InputObject <string>

        Required?                    true
        Position?                    Named
        Default value                
        Accept pipeline input?       true (ByValue)
        Accept wildcard characters?  

    -PropertyNames <string[]>

        Required?                    false
        Position?                    Named
        Default value                
        Accept pipeline input?       false
        Accept wildcard characters?  

    -TemplateContent <string>

        Required?                    false
        Position?                    Named
        Default value                
        Accept pipeline input?       false
        Accept wildcard characters?  

    -TemplateFile <string>

        Required?                    false
        Position?                    Named
        Default value                
        Accept pipeline input?       false
        Accept wildcard characters?  



Inputs
    System.String
    

Outputs
    System.Object

RelatedLinks
     http://go.microsoft.com/fwlink/?LinkId=507579


Remarks
    Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.
        -- To download and install Help files for the module that includes this cmdlet, use Update-Help.
        -- To view the Help topic for this cmdlet online, type: "Get-Help ConvertFrom-String -Online" or 
           go to http://go.microsoft.com/fwlink/?LinkId=507579.




Thanks for reading! If you have any questions, leave a comment or send me an email at fxcat@lazywinadmin.com. I invite you to follow me on Twitter @lazywinadm / Google+ / LinkedIn. You can also follow the LazyWinAdmin Blog on Facebook Page and Google+ Page.

6 comments:

  1. Great example of showing the power of the new cmdlet.

    ReplyDelete
  2. Great! this will simplify a lot importing cmd commands in to powershell workable objects. Good example!

    ReplyDelete
  3. FYI, I posted a second post about the same topic:
    http://www.lazywinadmin.com/2014/09/powershell-convertfrom-string-and.html

    ReplyDelete
  4. I like it. Thanks for the info.

    ReplyDelete