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

Scripting Games : Formatdata file for Advanced event 6

My solution for Advanced event 6 of the Summer Scripting Games is posted at the Script Center :

Hey, Scripting Guy! Event 6 *Solutions* from Expert Commentators (Beginner and Advanced; the 110-meter hurdles)

But the FormatData file (mow.TracertData.ps1xml) seems to be missing in the Post (I had to re-send it because of AV removing PS1XML files so the post might be updated later), so I will post the contents of this file here for now, for the script and description see the original article.

<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
    <ViewDefinitions>
        <View>
            <Name>Mow.TracertData</Name>
            <ViewSelectedBy>
                <TypeName>Mow.TracertData</TypeName>
            </ViewSelectedBy>

            <TableControl>
                <TableHeaders>
                    <TableColumnHeader>
                        <Width>7</Width>
                    </TableColumnHeader>
                    <TableColumnHeader>
                        <Width>5</Width>
                    </TableColumnHeader>
                    <TableColumnHeader>
                        <Width>5</Width>
                    </TableColumnHeader>
                    <TableColumnHeader>
                        <Width>5</Width>
                    </TableColumnHeader>
                    <TableColumnHeader>
                        <Width>5</Width>
                    </TableColumnHeader>
                    <TableColumnHeader/>
                </TableHeaders>
                <TableRowEntries>
                    <TableRowEntry>
                        <TableColumnItems>
                            <TableColumnItem>
                                <PropertyName>Number</PropertyName>
                            </TableColumnItem>
                            <TableColumnItem>
                                <PropertyName>T1</PropertyName>
                            </TableColumnItem>
                            <TableColumnItem>
                                <PropertyName>T2</PropertyName>
                            </TableColumnItem>
                            <TableColumnItem>
                                <PropertyName>T3</PropertyName>
                            </TableColumnItem>
                            <TableColumnItem>
                                <PropertyName>Time</PropertyName>
                            </TableColumnItem>
                            <TableColumnItem>
                                <PropertyName>Host</PropertyName>
                            </TableColumnItem>
                        </TableColumnItems>
                    </TableRowEntry>
                 </TableRowEntries>
            </TableControl>
        </View>
    </ViewDefinitions>
</Configuration>

 Enjoy

Greetings /\/\o\/\/

Posted by MoW | 1 Comments

Summer Scripting Games 2009

The Summer Scripting Games 2009 are coming June 15–26, 2009

Of course I will be there again ! And as in the last years games I will post all my solutions on my blog:

My solutions from the Winter Scripting Games 2007 and 2008 you can find on my blog here : Scripting games

And the PowerShell versions I did for the 2006 winter games (there was not an official PowerShell category them but I did them anyway) you can find here : Scripting Games 2006 Posts on my old blog

Again I will be a guest commenter for this years games, and one of my solutions will be posted on the Technet  Script Center as in the 2008 Winter Scripting Games my Windows PowerShell solution to Advanced Event 10 Black Jack.

And as last year I suspect some more contestants to post and blog about their solutions for this years Scripting Games.

But there is more, as this years games are organized in close cooperation with PowerShellCommunity.org and the PowerShell Code Repository see also the following : Press release for the 2009 Summer Scripting Games

There will be a special PoshCode.org repository created for all the Solutions for the Summer Scripting Games 2009.

This will mean an even greater possibly to learn from ,and to compare your solutions to the ones other contesters did send in.

Another great reason to join this years games,

I hope to see you there !

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 0 Comments

PowerTab 0.99b2 CTP3 fix

Update to get PowerTab working in PowerShell CTP3 (Win7 and W2008R2)

With thanks to Oisin

Greetings /\/\o\/\/

Posted by MoW | 8 Comments
Filed under: , ,

Attachment(s): PowerTab99b2-fixed.zip

PowerShell V2 : Get-Weather function using a Web service

In PowerShell V2 using a Web service is very easy with the new Cmdlet New-WebServiceProxy,

As I will show in this post by creating a Small Get-Weather function.

 

On the site http://www.webservicex.com we can find a couple of Web services among them a Global Weather service that I will use in my PowerShell Get-Weather function.

If we choose Global Weather on the website above we find some links among them we find the WSDL Location URI

WSDL Location
http://www.webservicex.net/globalweather.asmx?wsdl

this will link to an XML page describing the services and that is all that we need to get started

first we create the webServiceProxy using the following command

$weather = New-WebServiceProxy -uri http://www.webservicex.com/globalweather.asmx?WSDL

and it might seem almost to simple but we are as good as done already, only one line to go ;-)

PS H:\> ([xml]$weather.GetWeather('Zurich-Kloten','Switzerland')).CurrentWeather

Location         : Zurich-Kloten, Switzerland (LSZH) 47-29N 008-32E 432M
Time             : May 15, 2009 - 06:20 AM EDT / 2009.05.15 1020 UTC
Wind             :  Variable at 3 MPH (3 KT):0
Visibility       :  greater than 7 mile(s):0
SkyConditions    :  mostly cloudy
Temperature      :  64 F (18 C)
DewPoint         :  53 F (12 C)
RelativeHumidity :  67%
Pressure         :  29.74 in. Hg (1007 hPa)
Status           : Success

And we are Ready !, I got the current Weather in my region from a Web service in PowerShell V2 with only 2 lines !!.

But as that second line might be a bit cryptic lets look how I came to that

Now we have the $Weather object lets look at it with the Get-Member Cmdlet :

$weather | get-member

We can find 2 methods in the output from Get-member that look interesting :

GetCitiesByCountry

GetWeather

Lets look at that first Method :

PS H:\> $weather.GetCitiesByCountry

MemberType          : Method
OverloadDefinitions : {string GetCitiesByCountry(string CountryName)}
TypeNameOfValue     : System.Management.Automation.PSMethod
Value               : string GetCitiesByCountry(string CountryName)
Name                : GetCitiesByCountry
IsInstance          : True

Using a Methodname without parentheses in PowerShell we get information about the Method, by looking at the overloads we can see that, as you might have expected it takes a Countryname as Parameter.

so lets try that for Switzerland where I live

PS H:\> $weather.GetCitiesByCountry('Switzerland')
<NewDataSet>
  <Table>
    <Country>Switzerland</Country>
    <City>Geneve-Cointrin</City>
  </Table>
  <Table>
    <Country>Switzerland</Country>
    <City>Lausanne</City>
  </Table>
  <Table>
    <Country>Switzerland</Country>
    <City>Neuchatel</City>
  </Table>
  <Table>
    <Country>Switzerland</Country>
    <City>Sion</City>
  </Table>
  <Table>
    <Country>Switzerland</Country>
    <City>Payerne</City>
  </Table>
  <Table>
    <Country>Switzerland</Country>
    <City>Lugano</City>
  </Table>
  <Table>
    <Country>Switzerland</Country>
    <City>Bern / Belp</City>
  </Table>
  <Table>
    <Country>Switzerland</Country>
    <City>Grenchen</City>
  </Table>
  <Table>
    <Country>Switzerland</Country>
    <City>Zurich-Kloten</City>
  </Table>
  <Table>
    <Country>Switzerland</Country>
    <City>Saint Gallen-Altenrhein</City>
  </Table>
</NewDataSet>

And you can see we get back some XML with all the City’s we can pick in Switzerland.

This gives us the information we need but not in a handy format, but no problem as PowerShell perfectly knows how to handle XML for us, we just need to cast it to an XML  object like this :

PS H:\> $xml = [xml]$weather.GetCitiesByCountry('Switzerland')
PS H:\> $xml

NewDataSet
----------
NewDataSet

PS H:\> $xml.NewDataSet

Table
-----
{Table, Table, Table, Table...}

PS H:\> $xml.NewDataSet.table

Country                                                     City
-------                                                     ----
Switzerland                                                 Geneve-Cointrin
Switzerland                                                 Lausanne
Switzerland                                                 Neuchatel
Switzerland                                                 Sion
Switzerland                                                 Payerne
Switzerland                                                 Lugano
Switzerland                                                 Bern / Belp
Switzerland                                                 Grenchen
Switzerland                                                 Zurich-Kloten
Switzerland                                                 Saint Gallen-Altenrhein

Much better not ?

now we can select the City we need lets look at the second method GetWeather

PS H:\> $weather.GetWeather

MemberType          : Method
OverloadDefinitions : {string GetWeather(string CityName, string CountryName)}
TypeNameOfValue     : System.Management.Automation.PSMethod
Value               : string GetWeather(string CityName, string CountryName)
Name                : GetWeather
IsInstance          : True

Again no surprises here I think it takes a City and a Country as Parameters.

As we found the city we where interested in lets try it

PS H:\> $weather.GetWeather('Zurich-Kloten','Switzerland')
<?xml version="1.0" encoding="utf-16"?>
<CurrentWeather>
  <Location>Zurich-Kloten, Switzerland (LSZH) 47-29N 008-32E 432M</Location>
  <Time>May 15, 2009 - 06:20 AM EDT / 2009.05.15 1020 UTC</Time>
  <Wind> Variable at 3 MPH (3 KT):0</Wind>
  <Visibility> greater than 7 mile(s):0</Visibility>
  <SkyConditions> mostly cloudy</SkyConditions>
  <Temperature> 64 F (18 C)</Temperature>
  <DewPoint> 53 F (12 C)</DewPoint>
  <RelativeHumidity> 67%</RelativeHumidity>
  <Pressure> 29.74 in. Hg (1007 hPa)</Pressure>
  <Status>Success</Status>
</CurrentWeather>

And we get back the Weather, again as XML but as we have seen above that is really no problem, we just cast it to [XML] again.

PS H:\> [xml]$weather.GetWeather('Zurich-Kloten','Switzerland')

xml                                                         CurrentWeather
---                                                         --------------
version="1.0" encoding="utf-16"                             CurrentWeather

PS H:\> ([xml]$weather.GetWeather('Zurich-Kloten','Switzerland')).CurrentWeather

Location         : Zurich-Kloten, Switzerland (LSZH) 47-29N 008-32E 432M
Time             : May 15, 2009 - 06:20 AM EDT / 2009.05.15 1020 UTC
Wind             :  Variable at 3 MPH (3 KT):0
Visibility       :  greater than 7 mile(s):0
SkyConditions    :  mostly cloudy
Temperature      :  64 F (18 C)
DewPoint         :  53 F (12 C)
RelativeHumidity :  67%
Pressure         :  29.74 in. Hg (1007 hPa)
Status           : Success

We can make a little helper function now to make it even simpler

Function Get-Weather ([switch]$list,$city = 'Zurich-Kloten',$country = 'Switzerland',$filter = '') {

  $weather = New-WebServiceProxy -uri http://www.webservicex.com/globalweather.asmx?WSDL

  if ($list) {
    ([xml]$weather.GetCitiesByCountry($filter)).NewDataSet.table
  } else {
    ([xml]$weather.GetWeather($City,$country)).CurrentWeather
  }
}

And now we can use it like this :

To find out what the weather in the Netherlands where I lived before I can simple use the following 2 commands to find the City and to get the Weather :

PS H:\> Get-Weather -list -filter netherlands

Country                                                     City
-------                                                     ----
Netherlands                                                 Amsterdam Airport Schiphol
Netherlands                                                 Maastricht Airport Zuid Limburg
Netherlands                                                 De Bilt
Netherlands                                                 Deelen
Netherlands                                                 Eindhoven
Netherlands                                                 Groningen Airport Eelde
Netherlands                                                 Gilze-Rijen
Netherlands                                                 De Kooy
Netherlands                                                 Leeuwarden
Netherlands                                                 Rotterdam Airport Zestienhoven
Netherlands                                                 Soesterberg
Netherlands                                                 Twenthe
Netherlands                                                 Valkenburg
Netherlands                                                 Volkel
Netherlands                                                 Vlieland
Netherlands                                                 Woensdrecht
Netherlands Antilles                                        Flamingo Airport, Bonaire
Netherlands Antilles                                        Hato Airport, Curacao
Netherlands Antilles                                        Roosevelt Airport Saint Eustatius
Netherlands Antilles                                        Juliana Airport, Saint Maarten

PS H:\> Get-Weather -country Netherlands -city Eindhoven

Location         : Eindhoven, Netherlands (EHEH) 51-27N 005-25E 28M
Time             : May 15, 2009 - 06:25 AM EDT / 2009.05.15 1025 UTC
Wind             :  from the WSW (240 degrees) at 10 MPH (9 KT):0
SkyConditions    :  overcast
Temperature      :  60 F (16 C)
DewPoint         :  55 F (13 C)
RelativeHumidity :  82%
Pressure         :  29.68 in. Hg (1005 hPa)
Status           : Success

Yihaa, it is 2 degrees warmer here ;-)

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 2 Comments

New-CustomColumn function PowerShell V1.0

In this post  

I got triggered by a question on our MVP maillist, about using HashTables to make Custom columns with Select Object, a technique I use often

'm glad that you don't haev to remember label or name now, but it has always perplexed me why you ever name name,expression at all given that in all these cases you don't have hashtables with many objects, but rather an array of hashtables each with just the single (well two) items in them 

hello"| select-Object length,@{Name="onetoten";Expression = {1..10}},@{Name="thelength";Expression = {$_.length}}

why not just

"hello"| select-Object length,@{onetoten = {1..10};thelength= {$_.length}}

Jeffrey Snover Provided the following list for V2 with his answer ( The only place I found a list before for PowerShell V1.0 is is the "MSH User guide "(from the Beta version, seems to be replaced by the primer where I can't find it) hence I will paste the whole list here for reference)

We can take more than just name and expression:

 

·         Select-Object / -Property:

o   Expression: string / scriptblock

o   Name/Label: string

·         Group-Object / -Property:

o   Expression: string / scriptblock

·         Sort-Object / -Property

o   Expression: string / scriptblock

o   Ascending: bool

o   Descending: bool

·         Format-Wide / -Property

o   Expression: string / scriptblock

o   FormatString: string

·         Format-Custom / -Property

o   Expression: string / scriptblock

o   Depth: int

·         Format-List / -Property

o   Expression: string / scriptblock

o   FormatString: string

o   Label/Name: string

·         Format-Table / -Property

o   Expression: string / scriptblock

o   FormatString: string

o   Label/Name: string

o   Width: int

o   Alignment: string (limited to "left", "center", "right")

·         Format-XXX / -GroupBy

o   Expression: string / scriptblock

o   FormatString: string

o   Label/Name: string

As you can see from the list in the answer is that not only the select-object cmdlet takes a Hashtable as a custom property, but much more cmdlets do support this method amongst them all the Format Cmdlets.This technique can be very powerfull, but as I noted above, it is very hard to find information about this functionaly, or find a list of the possible options you have for the HashTable as in this list above.

Next to the use with Select-Object, the most usefull use of HashTables like this is for the Format-Table cmdlet where it provides a very powerfull way to customize and format the output by creating Custom Columns, and as you can see in the list above it is the Cmdlet that has the most options too.

 So I thought that it would make a good blog topic .and I figured that next to that making a function for this work, would make a good example to show the  use of the advanced function possibilities in parameter handling in PowerShell V2 CTP3 .

But as I did not have Powershell V2 CTP3 installed on the OS I'm currently running, I decided to start out with a PowerShell V1 example to show the Possibilities of Format-Table with Custom Columns and will show how we can improve this function in PowerShell V2 in the next post

 

The Basics :

 

we can use scriptblocks as parameters often in PowerShell and this is a very powerfull concept see for example  /\/\o\/\/ PowerShelled: PowerShell : Advanced renaming of files    

 in a select command we can also use scriptblocks instead of property to select , see for example the following post on my old blog :

/\/\o\/\/ PowerShelled: Report MP3 count and size by User from MSH

but as the complete scripttext becomes the property name, this is very hard to work with, we can solve that by creating a HashTable that has a value for the name and a value for the expression

, so we can give the scriptproperty a name. 

PS E:\PowerShell> dir | select name

Name
----
PowerShellAnalyzer
computers.txt
..


PS E:\PowerShell> dir | select {$_.name.toupper()}

$_.name.toupper()
-----------------
POWERSHELLANALYZER
COMPUTERS.TXT
..


PS E:\PowerShell> dir | select @{n='Name';e={$_.name.toupper()}}

Name
----
POWERSHELLANALYZER
COMPUTERS.TXT
..

for more usage examples and information how to preprepare them see also this post (ignore the topic, half way the post you can find a complete walktrough, explaining how using hashtables with select works and how to create the HashTables in front and store them in variables):

/\/\o\/\/ PowerShelled: PowerShell and Active Directory Part 5

 

Using HashTables with format-table 

 

The first caveat in PowerShell V1 is that with format-table the Name property is not supported but the Label property is used instead, this is very confusing and is solved in PowerShell V2 by supporting both Name as Label.

PS E:\PowerShell> dir | ft @{n='Name';e={$_.name.toupper()}}
Format-Table : Illegal key n
At line:1 char:9
+ dir | ft  <<<< @{n='Name';e={$_.name.toupper()}}

 

PS E:\PowerShell> dir | ft @{label='Name';e={$_.name.toupper()}}

Name
----
POWERSHELLANALYZER
COMPUTERS.TXT

As we have a lot of properties this can get very long and cryptic but we can make the hashTables in front and store them in a variable as shown in detail in the 2th post about select /\/\o\/\/ PowerShelled: PowerShell and Active Directory Part 5:

 PS E:\PowerShell> $name = @{label='Name';e={$_.name.toupper()}}
PS E:\PowerShell> dir | ft $name

Name
----
POWERSHELLANALYZER
COMPUTERS.TXT

But as Format-Object knows more properties I made a function to make even easier to make the Custom Columns

New-CustomColumn

 

 # Function New-CustomColumn for PowerShell V1.0
#
# Helper function to create Custom Columns for select or format cmdlets
# for more info and examples see :
#
http://thepowershellguy.com/blogs/posh/archive/2009/01/26/new-customcolumn-function-powershell-v1-0.aspx
#
# /\/\o\/\/ 2008
#
http://thePowerShellGuy.com

Function New-CustomColumn {
  PARAM (
    $Expression,
    $name,
    $label,
    $FormatString,
    [int]$Width,
    $Alignment
  )

  $column = @{}

  if ($Expression){
    $Column.Expression = $Expression
  } else {
    throw "Expression is mandatory"
  }
  if ($Name) {$Column.Name = $name}
  if ($Label) {$Column.Label = $Label}
  if ($FormatString) {$Column.FormatString = $FormatString}
  if ($Width) {$Column.Width = $Width}
  if ($Alignment) {$Column.Alignment = $Alignment}

  $Column.psobject.baseobject

}

Using the function above to create the HashTables for the Columns, I will show how we can make a completly customized output and cover the extra options we have with Format-Table, by changing the output of the dir command in PowerShell to mimic the DOS dir command.

 PS E:\PowerShell> new-CustomColumn -label Size -expression {$_.Length}

Name                           Value
----                           -----
Label                          Size
Expression                     $_.Length


PS E:\PowerShell> $size = new-CustomColumn -label Size -expression {$_.Length}
PS E:\PowerShell> dir | ft name,$size

Name                                                        Size
----                                                        ----
PowerShellAnalyzer
computers.txt                                               25
getworldtime.ps1                                            1181
HttpRest.ps1                                                17057
podiobooks.txt                                              186
poker.ps1                                                   21340
PowerShellAnalyzer.zip                                      7439208
prun.txt                                                    4048
psghost.zip                                                 3312
spaghetticode-powershell.mp3                                19235007
StartPage.vbs                                               537

in the example above we started with only a label to rename a property (Note that we need to use $_.Length now instead of just the name of the property , in the scriptblock the  $_ variable will contain the original Object that is passed into the select command)

PS E:\PowerShell> $modified = new-CustomColumn -l Modified -expression {$_.LastWriteTime} -formatString "yyyy-MM-dd HH:mm" -Width 20
PS E:\PowerShell> dir | ft $modified,$size,name

Modified                                Size                                    Name
--------                                ----                                    ----
2008-12-20 10:50                                                                PowerShellAnalyzer
2008-12-16 21:28                        25                                      computers.txt
2008-12-31 00:26                        1181                                    getworldtime.ps1
2008-12-18 22:30                        17057                                   HttpRest.ps1
2008-12-15 09:47                        186                                     podiobooks.txt
2009-01-01 23:09                        21340                                   poker.ps1
2008-12-20 10:50                        7439208                                 PowerShellAnalyzer.zip
2009-01-01 23:09                        4048                                    prun.txt
2008-12-18 23:38                        3312                                    psghost.zip
2008-12-20 08:49                        19235007                                spaghetticode-powershell.mp3
2008-12-18 12:16                        537                                     StartPage.vbs

In the next custom column we add "Modified" we used a new option -FormatString to not only rename the LastWriteTime property to Modified but also to display the date in a custom format (year-Month-Day time) and next to that we also set the Modified column to a fixed Width.

In the Following example I will show that as the Expression takes a scriptblock, we can embedd a complete multiline PowerShell script here, giving almost unlimited posibilities, in this case I use an if statement in the $type column to check the type of Object and if it is a directory I return <DIR> to mimic the output of the dos dir command :

PS E:\PowerShell> cmd /c dir
 Volume in drive E has no label.
 Volume Serial Number is DCE7-A82D

 Directory of E:\PowerShell

01-01-2009  23:09    <DIR>          .
01-01-2009  23:09    <DIR>          ..
16-12-2008  21:28                25 computers.txt
31-12-2008  00:26             1.181 getworldtime.ps1
18-12-2008  22:30            17.057 HttpRest.ps1
15-12-2008  09:47               186 podiobooks.txt
01-01-2009  23:09            21.340 poker.ps1
20-12-2008  10:50    <DIR>          PowerShellAnalyzer
20-12-2008  10:50         7.439.208 PowerShellAnalyzer.zip
01-01-2009  23:09             4.048 prun.txt
18-12-2008  23:38             3.312 psghost.zip
20-12-2008  08:49        19.235.007 spaghetticode-powershell.mp3
18-12-2008  12:16               537 StartPage.vbs
              10 File(s)     26.721.901 bytes
               3 Dir(s)   3.077.910.528 bytes free

 To get the  <DIR> before the Directories as we see in the output from the DOS dir command above 

PS E:\PowerShell> $type = new-CustomColumn -label type -Width 5 -expression {
>>   if($_.gettype().fullname -eq 'System.IO.DirectoryInfo'){
>>     '<dir>'
>>   }
>> }
>>
PS E:\PowerShell> dir | format-table $modified,$type,$size,$file

Modified                              type  Size                                  File
--------                              ----  ----                                  ----
2008-12-20 10:50                      <dir>                                       PowerShellAnalyzer
2008-12-16 21:28                            25                                    computers.txt
2008-12-31 00:26                            1181                                  getworldtime.ps1
2008-12-18 22:30                            17057                                 HttpRest.ps1
2008-12-15 09:47                            186                                   podiobooks.txt
2009-01-01 23:09                            21340                                 poker.ps1
2008-12-20 10:50                            7439208                               PowerShellAnalyzer.zip
2009-01-01 23:09                            4048                                  prun.txt
2008-12-18 23:38                            3312                                  psghost.zip
2008-12-20 08:49                            19235007                              spaghetticode-powershell.mp3
2008-12-18 12:16                            537                                   StartPage.vbs

Almost there only I'm not content with the Size yet, when we look at the output of dir in CMD.EXE we see that is looking better as it has thousand separators and it is right alligned, so lets see if we can do the same :

PS E:\PowerShell>  $size = new-CustomColumn -label Size -expression {if($_.length){$_.length / 1kb} } -w 15 -Align 'Right'  -f "#,0.## KB"
PS E:\PowerShell> dir | format-table $modified,$type,$size,$file

Modified             type             Size File
--------             ----             ---- ----
2008-12-20 10:50     <dir>                 PowerShellAnalyzer
2008-12-16 21:28                   0,02 KB computers.txt
2008-12-31 00:26                   1,15 KB getworldtime.ps1
2008-12-18 22:30                  16,66 KB HttpRest.ps1
2008-12-15 09:47                   0,18 KB podiobooks.txt
2009-01-01 23:09                  20,84 KB poker.ps1
2008-12-20 10:50               7.264,85 KB PowerShellAnalyzer.zip
2009-01-01 23:09                   3,95 KB prun.txt
2008-12-18 23:38                   3,23 KB psghost.zip
2008-12-20 08:49              18.784,19 KB spaghetticode-powershell.mp3
2008-12-18 12:16                   0,52 KB StartPage.vbs

I used the Alignment parameter to right align the Size, Converted the size into KB in the Expression and formatted it with 2 decimals, seperators and did place KB behind it.

And with that we covered all the options we can use in the HashTable for the Format-Table Cmdlet, below you find the complete code for the examples above  : 

 $modified = new-CustomColumn -label Modified -expression {$_.LastWriteTime} -f "yyyy-MM-dd HH:mm" -w 20

$type = new-CustomColumn -label type -Width 5 -expression {
  if($_.gettype().fullname -eq 'System.IO.DirectoryInfo'){
    '<dir>'
  }
}

$size = new-CustomColumn -label Size -expression {if($_.length){$_.length / 1kb} } -w 15 -Align 'Right'  -f "0.00 KB"

dir | format-table $modified,$type,$size,$file

That is it for now, it has become a long post but I hope it was usefull, In next post I will show how you can improve on the function using the CTP3 build 

Enjoy,

Greetings /\/\o\/\/  

 

Posted by MoW | 5 Comments
Filed under: ,

PowerBoots, These Boots are making GUI’s, and that is what they PoSHly do.

Jaykul (Joel Bennett), did it again !

With a GUI library for PowerShell inspired by Shoes called PowerBoots

Take a look at the amazing examples here : PowerBoots: The tutorial walkthrough

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 0 Comments
Filed under: , ,

Happy New Year !

I’m very happy and proud that I received a e-mail from Microsoft that I got my 4th MVP Award, and may call myself a PowerShell MVP for another year, an excellent start for me ;-).

Dear Marc van Orsouw,
Congratulations! We are pleased to present you with the 2009 Microsoft® MVP Award! This award is given to exceptional technical community leaders who actively share their high quality, real world expertise with others.

Best Wishes for the year 2009  and may the Power of the Shell be with you !

Greetings /\/\o\/\/

Posted by MoW | 6 Comments
Filed under: ,

Regular Expressions and PowerShell Part 2

A helpful tool on the way in this series is Robby Foust’s  RegEx Quick Reference in PoSHcode.org.

The function returns an Object, so you can easy format and filter it to your liking.

I like this view the most:

get-regex | Format-Table Sequence,meaning -GroupBy table –AutoSize

As having a quick regex reference around this one is

POSH> get-regex |? {$_.table -match 'Character classes'}| Format-Table Sequence,meaning -GroupBy table -AutoSize

   Table: Character classes and class-like constructs

Sequence Meaning
-------- -------
[...]    A single character listed or contained within a listed range.
[^...]   A single character not listed and not contained within a listed range.
.        Any character, except a line terminator (unless single-line mode, s).
\w       Word character.
\W       Non-word character.
\d       Digit.
\D       Non-digit.
\s       Whitespace character.
\S       Non-whitespace character.
\p{prop} Character contained by given Unicode block or property.
\P{prop} Character not contained by given Unicode block or property.

Note that I use the –Match operator in this example to filter only the Character Classes in a where (?) clause, a place using a RegEx can be handy.

let’s go for part 3 of the series, there is Some PowerShell mentioned but I wanted to add this,

you can use static methods from the .NET framework (system.regularBLOCKED EXPRESSION as in the VB.NET/C# examples using the [regex] typeacceloprator.

POSH> [regex]::IsMatch('12345','^\d{5}$')
True
POSH> [regex]::IsMatch('123456','^\d{5}$')
False

there was also was a question about linenumbers in the webcast, you can use select-string also for this that gives linenumbers (and can do context etc. and also can work with regexes

POSH> type computers.txt
localhost
foo
localhost
POSH> cat .\computers.txt | select-string 'foo'

foo

POSH> cat .\computers.txt | select-string 'foo' | format-list

IgnoreCase : True
LineNumber : 2
Line       : foo
Filename   : InputStream
Path       : InputStream
Pattern    : foo
Context    :
Matches    : {foo}

POSH> cat .\computers.txt | select-string 'foo' | ft line* -a

LineNumber Line
---------- ----
         2 foo

You can see that select string seems to return text but that is just the default formatter, you get back more info.

and then pick you just the info you want

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 3 Comments
Filed under: ,

Regular Expressions and PowerShell Part 1

Shay pointed me to this excellent series about Regular Expressions Regular Expression Webcast Series , While following this series to fresh up my Regex skill’s, I will post about PowerShell Specifics in this series here.

There are more ways to work with regular expressions with PowerShell, for following the exercises in Part 2 of the series, we can limit us to the –Match Operator.

I this Post I show how to do the presented exercises in PowerShell (Note follow the Second webcast to see complete examples and background I just picked some examples to show the translation to using the –Match operator in PowerShell.

Regular expressions can be used in a lot of places in Powerhell so adding regex skills to your PowerShell toolset will be very profitable in the long time !

So let’s get started with watching the Webcasts and following allong in PowerShell, picking up PowerShell regex specifics on the way ..  

First a little caveat you might encounter with the  –Match  Operator and might confuse you, if you do not know about this behavior, the  –Match operator will react different on scalar values and Collections :

PoSH> '7' -match '7'
True
PoSH> '7','77' -match '7'
7
77
PoSH> '7' -match '7'
True
PoSH> $matches

Name                           Value
----                           -----
0                              7

In the case of a Scalar a Boolean value is return if matched and in case of a collection the matched items are returned.

Note that you can check up on the actual Matchs(and or submatches but more about that later) by inspecting the $matches variable

another thing you have to take note of is that the –Match operator is Case Insensitive by default, as we can see in the Word examples later in the second webcast.

PoSH>  $names = 'jack','jim','betty','sue','barry','Henry','harry'
PoSH>> $names  -match '^h'
Henry
harry
PoSH> $names  -cmatch '^h'
harry
 

you can see to do a Case sensitive comparison, we can use the –cMatch Operator in PowerShell.

This is enough I think to get you trough the samples in the Webcast first the first 2 chapters, looking at the code samples below.

In the Second part so examples are show to mach ZIP codes here are some of of those examples translated to PowerShell to make it more easy to follow the examples in the webcast on PowerShell

'75603','78654','80765','90120','08765','76543','23456','56732' -match '7'

To make it more easy to follow the exercises we store this list in a variable, to make the line a bit shorter (although we can get the list back using the Up arrow, this makes it more readable)  :

$zip = '75603','78654','80765','90120','08765','76543','23456','56732'

$zip -match '^(75|78)'

Some more examples :

$zip -match '^[275]'
$zip -match '^[3-6]'
$zip -match '^[^78]'

$names = 'jack','jim','betty','sue','barry','Henry','harry'
$names  -match '^h'
$names  -cmatch '^h'
$zip -match '^[0-9]{5}'
$zip -match '^[0-9]{2,5}'
$zip -match '^\d{5,}'
$zip -match 'a*
'

I only watched the first to parts of the series but will follow up on how to do more advanced regex stuff in PowerShell when needed in following the Webcast series in PowerShell.

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 3 Comments
Filed under: ,

Jingle Shell, Jingle Shell, PowerShell V2 CTP3 is RTW.

Download Windows PowerShell V2 CTP3

Download WinRM 2.0 CTP3 (required for PowerShell remoting)

Enjoy,

Greetings /\/\o\/\/

 

 

Posted by MoW | 2 Comments
Filed under: ,

New Spaghetti Code Podcast: Neil Iversen Discusses Microsoft PowerShell

New Spaghetti Code Podcast: Neil Iversen Discusses Microsoft PowerShell

Spaghetti Code discusses Microsoft Powershell with Neil Iversen. Neil describes how Powershell is the combination of command line simplicity with the power of .NET.  He also talks about how Powershell can make life easier for developers and how Powershell exposes new capabilities to the applications they create.

A very nice Podcast about what PowerShell and the comunity can do for Developers. 

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 0 Comments
Filed under: ,

PowerShell V2 CTP3 teaser : list PST files on a list of computers using background jobs

At work I had to list al PST files on a list big of computers, as searching for PST files is a timeconsuming job it should be possible to run the job concurently, so I thought that it was a good opportunity to try in PowerShell V2.   

 as I have "The PDC build" of WIndows 7 running, that has a Post-CTP2 version of PowerShell included I prototyped this example., As I noted that he needed CTP2 for it to run, as seemed after he dowloaded the CTP2 build of PowerShell V2, that it was not possible in yet CTP2, As CPT3 is not yet released to general public  (but will be sometime this Month) , he could not use it, but  I still decided to post it on my blog as a teaser (sorry, but it was so Cool I could not resist) :

 

# start job, with max of 10 Job running at the same time 

 

$job = get-wmiobject -query "Select * from CIM_DataFile Where Extension = 'pst'" -ComputerName (Get-Content C:\powershell\computers.txt) -asJob -ThrottleLimit 10

 

# check status

 

$job

 

# Display results on screen, preserving the data

 

Receive-Job -Keep $job | select CSName,FileName,Description,FileSize

 

# when all jobs are finished, we can export the results to a  CSV file

 

Receive-Job $job | select CSName,FileName,Description,FileSize | Export-Csv PstFiles.csv –NoTypeInformation

You can see it is, when you have the needed WQL query sting, simple enough, so simple even that we do not even need a "Stinking Script" anymore, we can just do it it interactive in a console session and check the results inbetween before exporting it to a CSV file in another simple oneliner :

  

The result looks like this :

 

PS C:\PowerShell>  get-wmiobject -query "Select * from CIM_DataFile Where Extension = 'pst'" -ComputerName (Get-Content
C:\powershell\computers.txt) -asJob -ThrottleLimit 10
PS C:\PowerShell>
PS C:\PowerShell> $job = get-wmiobject -query "Select * from CIM_DataFile Where Extension = 'pst'" -ComputerName (Get-C
ntent C:\powershell\computers.txt) -asJob -ThrottleLimit 10
PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell> # check status
PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell> $job

Id              Name            State      HasMoreData     Location             Command
--              ----            -----      -----------     --------             -------
5               Job5            Running    False           localhost,foo,loc... Get-WMIObject


PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell> # Display results on screen, preserving the data
PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell> Receive-Job -Keep $job | select CSName,FileName,Description,FileSize
PS C:\PowerShell> Receive-Job -Keep $job | select CSName,FileName,Description,FileSize
Receive-Job : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:1 char:12
+ Receive-Job <<<<  -Keep $job | select CSName,FileName,Description,FileSize
    + CategoryInfo          : InvalidResult: (:) [Receive-Job], COMException
    + FullyQualifiedErrorId : JobStateFailed,Microsoft.PowerShell.Commands.ReceiveJobCommand

PS C:\PowerShell> $job.ChildJobs

Id              Name            State      HasMoreData     Location             Command
--              ----            -----      -----------     --------             -------
6               Job6            Completed  True            localhost
7               Job7            Failed     False           foo
8               Job8            Completed  True            localhost


PS C:\PowerShell> Receive-Job -Keep $job | select CSName,FileName,Description,FileSize

CSName                        FileName                      Description                                        FileSize
------                        --------                      -----------                                        --------
MOW7                          mow                           c:\users\mow\appdata\local...                        271360
Receive-Job : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:1 char:12
+ Receive-Job <<<<  -Keep $job | select CSName,FileName,Description,FileSize
    + CategoryInfo          : InvalidResult: (:) [Receive-Job], COMException
    + FullyQualifiedErrorId : JobStateFailed,Microsoft.PowerShell.Commands.ReceiveJobCommand

 

As we needed this in production we in the end used another solution, but you can see how easy this will become in PowerShell V2

 

Enjoy soon,

 

Greetings /\/\o\/\/

Posted by MoW | 2 Comments
Filed under: ,

YMCA

Read the full story here :

http://powershellug.com/blogs/richardsiddaway/archive/2008/11/08/ymca.aspx

* Update *

See the Pictures here : http://jonathanmedd.blogspot.com/2008/11/ymca.html 

* Update 2 *

read the rest of the full story here :Gesticulating Ben Pearce

 

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 2 Comments
Filed under: ,

TechNet Webcasts about PowerShell

TechNet Webcast: Introduction to Windows PowerShell (Level 200)

 
Language(s): English.
Product(s): Windows Server 2008.
Audience(s): IT Professional.
Duration: 90 Minutes
Start Date:
Monday, December 08, 2008 11:30 AM Pacific Time (US & Canada)
 

Event Overview

In this webcast, we provide an introduction to Windows PowerShell for IT professionals and show how to automate client administrative activities in the Windows Server 2008 and Windows Vista operating systems. We explore the features and capabilities of Windows PowerShell, describe customer scenarios to manage day-to-day server and client administration activities, and discuss command-line syntax usage.

Presenters: John Baker, IT Pro Evangelist, Microsoft Corporation, and Yung Chou, IT Pro Evangelist, Microsoft Corporation

TechNet Webcast: Advanced Windows Powershell Scripting (Level 400)

Language(s): English.
Product(s): Windows Server 2008.
Audience(s): IT Professional.
Duration: 90 Minutes
Start Date:
Monday, December 15, 2008 11:30 AM Pacific Time (US & Canada)
 

Event Overview

In this webcast, we look at some of the powerful scripting capabilities of Windows PowerShell. First, we introduce some of the basics of Windows PowerShell.  We examine how the help system works and review the Windows PowerShell command structure. We also discuss how to format output and some basic Windows PowerShell commands. Next, we explore variable declaration and scripting constructs in Windows PowerShell to perform basic programmatic functions, like loops and branching. Then we look at advanced scripting techniques involving functions, filters, script blocks, and error handling. We also look briefly at how to use Windows PowerShell with Windows Management Instrumentation (WMI) to gain access to a wide variety of system objects and settings. Finally, we look at how to use the Microsoft .NET Framework to build a simple form using Windows PowerShell.

Presenters: John Baker, IT Pro Evangelist, Microsoft Corporation, and Yung Chou, IT Pro Evangelist, Microsoft Corporation

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 4 Comments
Filed under: ,
More Posts Next page »