Friday, September 25, 2009

Sorting hashtable in Powershell

I am using hashtables (or dictionaries) more and more in Powershell – once I figured out that syntax $Hashtable.Key works, it turned out to be extremely useful as something similar to custom class.

However I run into an issue (maybe bug?) with sorting hashtables:

PS C:\> $X.Martin = "Zugec"
PS C:\> $X.Kamila = "Vlasakova"
PS C:\> $X.Filip = "Puntik"
PS C:\> $X.Tereza = "Liska"
PS C:\> $X | Sort Name

Name                           Value
----                           -----
Filip                          Puntik
Kamila                       Vlasakova
Tereza                      Liska
Martin                       Zugec

As you can see, output is not sorted. After a while I tried to use raw method that is used in .NET:

PS C:\> $X.psbase.GetEnumerator() | Sort Name

Name                           Value
----                           -----
Filip                          Puntik
Kamila                       Vlasakova
Martin                       Zugec
Tereza                      Liska

As you can see, everything is sorted out correctly in this case. Another scenario where you could use GetEnumerator is if you want to handle name (key) and value:

PS C:\> $X.psbase.GetEnumerator() | Where {$_.Name -eq "Martin"}

Name                           Value
----                           -----
Martin                         Zugec

Without using enumerator, you got access only to values.

Monday, September 7, 2009

How to display all performance counters in console

Just run into this today – I am writing some scripts for Citrix License server and wanted to see which performance counters are available…

Turned out to be pretty simple – just use “TypePerf” command with –q:

   1: C:\Users\Martin>typeperf -q | more



   2: \TBS counters\CurrentResources



   3: \TBS counters\CurrentContexts



   4: \WSMan Quota Statistics(*)\Active Users



   5: \WSMan Quota Statistics(*)\Active Operations



   6: \WSMan Quota Statistics(*)\Active Shells



   7: ...