November 27th, 2007

New-HashTable

PowerShell Team
PowerShell Team

John Sheehan the architect for Microsoft Application Virtualization (aka SoftGrid) recently jumped on the PowerShell bandwagon and was nice enough to write up his experiences.  At one point he wrote himself a process monitor which involved putting all the processes into a hashtable indexed by ProcessID.  This is a fairly trivial piece of code to write but I always thought that we ought to provide it for people so I decided to blast one out and share it.

 

This is the code for New-HashTable.ps1

# FILE:     New-HashTable.ps1
# Author:   Jeffrey Snover
# Version:  0.1
#Requires -Version 1.0
param(
    $Key=$(Throw "USAGE: New-HashTable -Key <property>"),
    [Switch]$NoOverWrite,
    [Switch]$MakeArray
    )
Begin
{
    $hash = @{}
}
Process

    $Property = $_.$key
    if ($NoOverWrite -And $hash.$Property)
    {   Write-Error "$Property already exists"
    }elseif ($MakeArray)
    {
        if (!$hash.$Property)
        {   $hash.$Property = new-object System.Collections.ArrayList
        }
        [void]$hash.$Property.Add($_)
    }else
    {
        $hash.$Property = $_
    }
}
End
{
    $hash
}

 

Here it is running:

PS>gps *ss |new-hashtable
USAGE: New-HashTable -Key <property>
At c:\ps\New-HashTable.ps1:6 char:17
+     $Key=$(Throw <<<<  "USAGE: New-HashTable -Key <property>"),

PS>gps *ss |new-hashtable -key id

Name                           Value
—-                           —–
516                            516 – csrss
620                            620 – lsass
576                            576 – csrss
388                            388 – smss

PS>gps *ss |new-hashtable -key product

Name                           Value
—-                           —–
Microsoft® Windows® Operati… 388 – smss

PS>gps *ss |new-hashtable -key product -NoOverWrite
c:\ps\New-HashTable.ps1 : Microsoft® Windows® Operating System already exists
At line:1 char:23
+ gps *ss |new-hashtable <<<<  -key product -NoOverWrite
c:\ps\New-HashTable.ps1 : Microsoft® Windows® Operating System already exists
At line:1 char:23
+ gps *ss |new-hashtable <<<<  -key product -NoOverWrite
c:\ps\New-HashTable.ps1 : Microsoft® Windows® Operating System already exists
At line:1 char:23
+ gps *ss |new-hashtable <<<<  -key product -NoOverWrite

Name                           Value
—-                           —–
Microsoft® Windows® Operati… 516 – csrss

PS>gps *ss |new-hashtable -key product -MakeArray

Name                           Value
—-                           —–
Microsoft® Windows® Operati… {516 – csrss, 576 – csrss, 620 – lsass, 388 – smss}

Enjoy!

Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Category
PowerShell

Author

PowerShell Team
PowerShell Team

PowerShell is a task-based command-line shell and scripting language built on .NET. PowerShell helps system administrators and power-users rapidly automate tasks that manage operating systems (Linux, macOS, and Windows) and processes.

0 comments

Discussion are closed.