Sunday, February 8, 2009

This is a weird peice of code inspired by some syntax I found in Hristo Deshev's interesting book: "Pro Windows Powershell". Hristo talks about converting IDictionary objects to Hash Tables.  'PS' or 'get-process' uses the PID as the hash code for the process object. This allows a construct that produces a hash table with explict PIDs as hash keys. It is too late to figure out if this side-effect would have any value to anyone.

$script_block_ID = {ps | %{$_.ID} | Sort-object }
$dict = new-object Collections.Specialized.OrderedDictionary
$script_block_ID.Invoke() | %{$dict[(ps -id $_ | Select Name)] = $_.GetHashCode()}
write `r`n `$dict:
$hash = [hashtable]$dict
write `r`n `$hash:

@{Name=cmd}                    1364                                            
@{Name=alg}                    864                                             
@{Name=explorer}               3212                                            
@{Name=gvim}                   1852                                            
@{Name=wmiprvse}               1332                                            
@{Name=VCSExpress}             3980                                            
@{Name=wscntfy}                352                                             
@{Name=chrome}                 2724                                            
@{Name=svchost}                1536 

No comments: