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

PowerShell Script Club in Zurich

Next week I will at the Swiss IT Pro User Group Event 3.Nov 2009 Zurich - PowerShell Script Club

The first PowerShell Scriptclub in Zurich, with thanks to Desmond Lee , the Swiss IT Pro User Group and Digicomp Academy AG  (Zürich)

Below the excerpt from the newsletter (the Dunglish is mine, sorry we had to be quick ) :

Swiss IT Pro User Group Events - Di./Tu. 3 Nov. 2009
    by IT Pros for IT Pros
    Subject:    PowerShell Script Club
    Language:  English
    Tue / Dienstag - 3. Nov 2009
    17:45 - 21:00 Uhr
    Kleiner Apéro 17:45 - 18:15 Uhr und ab 2015 Uhr
    Where / Wo?
Digicomp Academy AG  (Zürich)
        Limmatstrasse 50
        CH-8005 Zürich
        Tel. +41 44 447 21 21

  With thanks to the Swiss IT Pro User Group and Digicomp, the first
  Zurich PowerShell Script Club will be held in Zurich next week Tuesday.
  This Scriptclub will be led by Marc van Orsouw (aka /\/\0\/\/ or
  the PowerShell Guy). Welcome Marc to our Swiss IT Pro User Group family!
       Referat:
        Marc van PowerShell is a Dutch but living in Switzerland and is a
        PowerShell MVP for the last 4 years in a row, he is best known for his
        blog
thePowerShellGuy.com and his utilities for PowerShell as PowerTab
        and the PowerShell WMI explorer.
       Session:  What is a PowerShell Script Club?
            Script Clubs are like a hands on lab with no set topic or teacher. You bring
            an idea for a script, and ask your fellow PowerShell users for help getting
            the script written.
            Script Club is a great way to learn PowerShell.
            Join the PowerShell Script Club today!
            Beginner through Advanced users equally welcome.
         * computers with PowerShell V2 installed may be available.
       -------------
    IMPORTANT REGISTRATION INFO
        -------------
    Important event registration (it's free) and detail session information
here.

Hope to see you next week !

Enjoy,

Greetings /\/\0\/\/

Posted by MoW | 2 Comments

Microsoft Evolution Day

At 3 December I will be giving a PowerShell session at the Microsoft Evolution Day in Zurich,

Microsoft Evolution Day

Windows 7, Windows Server 2008 R2, Office Communication Server 2007 R2, Exchange 2010, Office 2010, SharePoint 2010, PowerShell 2.0 – die Evolution der Microsoft Lösungen geht rasend schnell. Am Microsoft Evolution Day vom 3. Dezember 2009 werden alle neuen Produkte live vorgestellt. Die optimale Gelegenheit für IT Professionals sich den Überblick zu den Neuerungen aus Redmond zu verschaffen.

 

 

For more information or to register see : http://www.digicomp.ch/MicrosoftEvolutionDay

Hope to see you there.

Enjoy,

Greetings /\/\0\/\/

Posted by MoW | 0 Comments
Filed under:

PowerShell Programming Praxis: Nerds, Jocks, and Lockers

And another WTF Programing Praxis :

Nerds, Jocks, and Lockers

 function get-OpenLockers ($num){1..([math]::sqrt($num))|%{$_*$_}}

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 0 Comments
Filed under:

PowerShell Programming Praxis: Josephus' Circle

And another PowerShell excersise from the WTF  (Worse than Failure) contest  Programming Praxis: Josephus' Circle . (for more info see original contest post)

 Function get-SafeSpot ($count,$Skip) {
  $q = [Collections.queue](1..$count)
  while ($q.count -gt 1){
    1..($skip-1) |% {$q.enqueue($q.dequeue())}
    [void]$q.dequeue()
  }
  $q
}

By using a Queue (that works on a FIFO (First In First Out) base, I could keep the loop very simple by just requeueing the surviving members) and could save me the Math otherwise involved in keeping count.

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 0 Comments
Filed under:

PowerShell : Display a GUID as string by default

When you have a GUID object in PowerShell by default this gives no output, only some empty lines.

See output below :

PS C:\> [GUID]$a = "00000000-0000-0000-0000-000000000000"                                                               
PS C:\> $a                                                                                                              
                                                                                                                        
                                                                                                                        
                                                                                                                        
PS C:\> $a.ToString()                                                                                                   
00000000-0000-0000-0000-000000000000                                                                                    
PS C:\> "$a"                                                                                                            
00000000-0000-0000-0000-000000000000                                                                                    
PS C:\> Update-TypeData C:\MowTemp\GUID.ps1xml                                                                          
PS C:\> $a                                                                                                              
                                                                                                                        
GUID                                                                                                                    
----                                                                                                                    
00000000-0000-0000-0000-000000000000                                                                                    
                                                                                                                        
                                                                                                                        

The reason for this is that a GUID has no properties only 2 Methods to show the value of the GUID

ToByteArray()
ToString()

one option is Embedding it in a string "$a" 

But as it is handy to show the GUI as string by default I created the following TypeData file that adds a GUID property to the GUID object so that the string value is Shown by default in PowerShell as in the second example above after the TypeData file is loaded

the TypeData file looks like this :

<?xml version="1.0" encoding="utf-8" ?> 
<Types> 
    <Type> 
        <Name>System.Guid</Name> 
        <Members> 
            <ScriptProperty>
                <Name>GUID</Name>
                 <GetScriptBlock>
                    $this.Tostring()
                </GetScriptBlock>
            </ScriptProperty> 
        </Members> 
    </Type> 
</Types>

Democracy to the Types ! 

Enjoy,

Greetings MOW

Posted by MoW | 1 Comments
Filed under: ,

Russian Peasant Multiplication

Small PowerShell exercise taken from the Programming Praxis: Russian Peasant Multiplication contest of WTF (Worse than Failure) implementing the Russian Peasant Multiplication method (for more info see original contest post)

function Invoke-RussianMultiply ([int]$a,[int]$b)
{
  "$a x $b"
  $r = 0
   while($a -ne 1){
     $a = $a/2
     $b = $b*2
     "$a x $b"
     if ($a%2) {$r+=$b;"`t+$b"}
   }
  "result : $r"
}
 

The result looks like this :

PS C:\> Invoke-RussianMultiply 18 23                                                                                    
18 x 23                                                                                                                 
9 x 46                                                                                                                  
        +46                                                                                                             
4 x 92                                                                                                                  
2 x 184                                                                                                                 
1 x 368                                                                                                                 
        +368                                                                                                            
result : 414                                                                                                            

 

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 2 Comments
Filed under:

Get-Scripting Podcast Episode 11 - (MoW aka the PowerShell Guy)

I had the pleasure to be interviewed for the Get-Scripting Podcast

You can find the show here :

Get-Scripting Podcast Episode 11 - (MoW aka the PowerShell Guy)

 

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 0 Comments

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 | 9 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 | 4 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: ,
More Posts Next page »