Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

PowerShell examples used on ars technica

Ars technica did an article about PowerShell : Microsoft PowerShell rolled into Longhorn Server

In this article two PowerShell examples from my blog are used :

How to get the uptime of a specific service:

$s = gwmi win32_service -filter "name = 'spooler'"

 "{0}" -f ((get-date) - ([wmi]'').ConvertToDateTime((gwmi Win32_Process -filter "ProcessID = '$($s.ProcessId)'").CreationDate))

Another is how to parse a Tab-Delimited file and save it as a CSV:

gc tab.txt |% {$_.split("`t") |? {$_ -match '='} |% {$r = new-object object} {$r | add-Member -memberType noteProperty -name $_.split('=')[0] -Value $_.split('=')[1]}{$r}} | export-csv -NoTypeInformation

It's nice to see that Jeremy Page did read my blog, liked the examples enough to use them in the article, and to have my blog mentioned on Ars technica,

But I think the scripts that are selected for the article show some great PowerShell concepts and possibilties, but are not really good examples for this standalone use and might be misleading when shown out of context and without any further explanation, hence hard to follow, making PowerShell look more intimidating and difficult as needed, instead of showing the ease and power. 

However this two scripts CAN be good examples of working with PowerShell, only a bit more information is needed, so I decided to make this post to give some more background about the scipts and the links to the original posts 

The issues with the examples "standalone" in article

As the PowerShell examples picked from my blog for use in the article are based on VbScript examples from Hey, Scripting Guy!  TechNet columns, the scripts chosen are very specific also the examples do provide solutions for a bit more advanced problems. Without the information given in the posts on my blog where the examples came from and without reading the columns on Technet they are based on, this scripts might be a bit confusing and give a wrong impression about what they exactly do and the complexity of working with PowerShell :

Hence the examples, placed without background information like in this article, are a bit out of context and might lead to wrong idea about the ease of use of PowerShell 

For the system admin's out there without a programming background, this probably looks pretty intimidating at first, but I suspect that we'll grow to love it.

the examples or short but advanced so without further information might look a bit cryptic and support this "wrong" impression of powershell being intimidating,

  • Yes, you can do very advanced stuff with powershell but you do not NEED too.
  • Yes, you can use aliases and do a lot on one line but you do not NEED too. 

Origin , background of the example and how you can get most out of them

be couse the blog entries are based on the Hey, Scripting Guy! articles and the PowerShell scripts are translations of the VbScript examples given in them, you can read the original articles for background information and compare the VbScript and PowerShell versions,but to get a good  impression about the scripts  you really need to read the original Technet column,  read the entry on my blog and after that compare the examples in VbScript with the ones in PowerShell.

Also as the second example about is based on a custom file format it is not a great example and not generic useful so I will show a more useful function to import Tab delimited files in PowerShell in this post .  

 So to get a better picture how to interpret the examples, please see the following remarks about the examples and the links to the posts more information about the scripts. 

 

How to get the uptime of a specific service:

This Example came from this blog entry: Hey, PowerShell Guy ! How Can I Get the Uptime of a Service?

This is a long post, as a lot of different tasks are done in that second line, in this post I explain in detail  how I came to this second line, and show how I did build it op by  breaking it up in parts and give examples and explain them step by step, so  you can see it is not as difficult that working with WMI is very simple in PowerShell ,but that this PowerShell example combined a lot of processing in this single line.

By reading the original Column on TechNet How Can I Get the Uptime of a Service? and by looking at the solution in VbScript that is given there, you can see that the VbScript version is big and complex and by comparing the PowerShell and VbScript's you can see that  this processing of WMI DateTime values was much harder to do in VbScript.

 Also in the comments you find a even simpler solution by Martin :

Using get-process allows an even shorter solution. The second line would be:

"{0}" -f ((get-date) - (gps -id $s.processid).Starttime)

So I'm sure that  after reading this background information and comparing it with the VbScript way to do this in the original Scripting guy column and the VbScript example there, you will see that this code is not as hard as it seems and how simple and powerful working with WMI in PowerShell, also I made this all on one line for ease of use as I made it interactively, but you do not need to work like that you can also make it into a more verbose version for re-use in a script when needed.

 

how to parse a Tab-Delimited file and save it as a CSV

This example came from : Hey PowerShell Guy !,How Can I Parse a Tab-Delimited File and Then Save That as a Comma-Separated Values

This oneliner shows how to parse text and how create custom objects from it first, so after that we have the Content as structured data in an array of objects with all the fields exposed as properties, so we can use all the PowerShell object utilities as select, sort and group on it and as you can see in the example we can just use the export-csv CmdLet to export it to a CSV file.

This way of importing and handling unstructured data as textfiles and other data in PowerShell, by first building custom objects from the data is an incredible powerful of working with it in PowerShell, and this method might come handy often 

The example might seem intimidating at first but there is more information in the post and also you can find more examples on my blog where I turn in webpages into object Tag : TextScraping and again, this example shows that it can be done as a oneliner, but of course it does not HAVE to be done as a oneliner ;-)

What also might be confusing is the format of the inputfile :

The format of the file given in the original Hey Scripting guy column How Can I Parse a Tab-Delimited File and Then Save That as a Comma-Separated Values File? is not in a really common format, with the columnname in the field : 

Cre Rec Name=Jack Address=5 XYZ Drive Phone=555-4567

Cre Rec Name=Jill Address=7 XYZ Drive Phone=555-6547

Cre Rec Name=Jake Address=9 XYZ Drive Phone=555-9876

That format might not be what you expected from a Tab delimited file and the example was build to parse that format.

A more common format for a tab delimited file is with a header row containing the Columnnames like this :

Name Address Phone
Jack 5 XYZ Drive 555-4567
Jill 7 XYZ Drive 555-6547
Jake 9 XYZ Drive 555-9876

More like you might have expected and much more usefull so I made a function for this.

 

function Import-TabDelimited ($Path) {
  gc $path |% {$header = $true}  {
    if ($header){
      $h = $_.split("`t")
      $header = $false
    }
    Else {
      $r = new-object object
      $_.split("`t") |% {$i=0}{
        $r | add-Member -memberType noteProperty -name $h[$i] -value $_
        $i++
      }
      $r 
    }
  }
}

 

this function will parse the first line for the propertynames and will use them to create the objects for the rest of the lines. 

You can use it like this to translate a Tab delimited file to a CSV file :

PoSH> Get-Content tab2.txt                                                                                              
Name    Address Phone                                                                                                   
Jack    5 XYZ Drive     555-4567                                                                                        
Jill    7 XYZ Drive     555-6547                                                                                        
Jake    9 XYZ Drive     555-9876                                                                                        
PoSH> Import-TabDelimited tab2.txt                                                                                      
                                                                                                                        
Name                                    Address                                 Phone                                   
----                                    -------                                 -----                                   
Jack                                    5 XYZ Drive                             555-4567                                
Jill                                    7 XYZ Drive                             555-6547                                
Jake                                    9 XYZ Drive                             555-9876                                
                                                                                                                        
                                                                                                                        
PoSH> Import-TabDelimited tab2.txt | Export-Csv csv2.csv -NoTypeInformation                                             
PoSH> type csv2.csv                                                                                                     
Name,Address,Phone                                                                                                      
Jack,"5 XYZ Drive",555-4567                                                                                             
Jill,"7 XYZ Drive",555-6547                                                                                             
Jake,"9 XYZ Drive",555-9876                                                                                             
PoSH> Import-Csv csv2.csv                                                                                               
                                                                                                                        
Name                                    Address                                 Phone                                   
----                                    -------                                 -----                                   
Jack                                    5 XYZ Drive                             555-4567                                
Jill                                    7 XYZ Drive                             555-6547                                
Jake                                    9 XYZ Drive                             555-9876                                
                                                                                                                        
                                                                                                                        
PoSH>                                                                                   

 

 Now that you have the data in a CSV file you can use the standard PowerShell CmdLet import-csv  to import the data in the CSV file back into an array of objects again and process them in PowerShell.

There is more to tell about this and there are also other way's to import and process Tab delimited and Text files, but post would become to long will make an follow post later dedicated to working with Tab delimited files  :

  • some more information about the workings of this script
  • An extended version of this function that is more flexible and has a -Noheader parameter and a flexible delimiter
  • You can also ADO to open Tab delimited files but you need a schema.ini file in the same directory so see also /\/\o\/\/ PowerShelled: Working with Fixed Length delimited Text ... on my old blog I will explain this a bit more also
  • I will cover using ODBC and the Microsoft Text driver to connect to the Inputfile instead of OleDB used in Fixed length files
  • Creating and using file DSN's to connect
  • how to use the Data Sources (ODBC) manager to create DSN's and Schema.ini files for usage from powershell in a GUI using a wizard

The extended version of the script and the ADO alternatives in next post will give more flexibilty handling textfiles in PowerShell

If you need to do even more advanced parsing see also this blogentry parse-textObject – AWK with a vengeance. on Precision Computing, Lee Holmes's blog

You can find more information about text parsing there and a very cool powershell script that provides a even more powerfull way to parse unstructured data into objects, with more advanced parsing and regex support. Examples and even Unit tests are provided  be sure to check it out !

Enjoy,

Greetings /\/\o\/\/

Published Saturday, March 31, 2007 8:30 PM by admin
Filed under: , , , ,

Comments

# Hey, powerShell Guy !How Can I Locate a Word in a Text File and Then Echo Back That Word and Any Remaining Text?

Another translation of an example script from a Hey scripting guy column How Can I Locate a Word in a

Sunday, April 01, 2007 10:15 AM by The PowerShell Guy

# re: PowerShell examples used on ars technica

Thanks for the clarification. I do read your blog but I am not a programming oriented person, I think what seems straitforward to you is mystifying to some folks.

I hope I didn't get anything too wrong.

Monday, April 02, 2007 8:21 AM by Jeremy Page

# re: PowerShell examples used on ars technica

Hi Jeremy,

Thanks for your reaction,

You didn't get anything wrong (Yes, you will grow to like it ;-) ),

but as I tried to explain in this blogentry, the scripts, as they are based on TechNet examples, so might not be straitforward without background.

I hope this post will help to de-mystify them a bit.

Greetings /\/\o\/\/

Monday, April 02, 2007 8:38 AM by MoW

# re: PowerShell examples used on ars technica

any idea about creating a cmdlet for creating a tab-delimited list from objects?

Tuesday, July 03, 2007 2:34 PM by Jeremy (another one)

# re: PowerShell examples used on ars technica

Wednesday, July 04, 2007 5:10 AM by MoW

# re: PowerShell examples used on ars technica

This may be improperly placed, but here is my question.

When I run;

get-wmiobject -class "Win32_NetworkAdapterConfiguration" -property "dnsserversearchorder","dnshostname" -namespace "root\CIMV2" -computername $strComputer -filter "IPEnabled = true"

...the dnsserversearchorder returns more than one IP.

...I want to pipe the result to a csv, but it does not work.

...This thread here discusses the ability to get the 1st IP using computed columns, but I want all results.

Any ideas?

Thanks in advance.

L

Tuesday, August 07, 2007 11:26 AM by lrbell

# re: PowerShell examples used on ars technica

Tuesday, August 07, 2007 11:28 AM by lrbell

# re: PowerShell examples used on ars technica

@ Irbell,

you can use a computed column, to get all the values from the array, by default oSTring() will get called, what does not give wanted result (I did one property as example):

get-wmiobject -class "Win32_NetworkAdapterConfiguration" -property "dnsserversearchorder","dnshostname" -namespace "root\CIMV2" -computername $strComputer -filter "IPEnabled = true" | Select @{n=dns;{$_.dnsserversearchorder}} | export-csv

Greetings /\/\o\/\/

Wednesday, August 08, 2007 8:28 AM by MoW

# re: PowerShell examples used on ars technica

LOL, that link was my answer also

hehe

/\/\o\/\/

Wednesday, August 08, 2007 8:30 AM by MoW
Anonymous comments are disabled