PowerWF News, Tutorials, and more...

Get the latest tips and tricks directly from the PowerWF development team. Find out about new releases and upcoming features.


Seamless Automation

From the Desktop to the Data Center

The PowerWF family of products are easy enough for desktop and departmental automation, yet powerful and scalable enough for the Data Center. PowerWF compliments Opalis and other RBA solutions, lets you leverage your workforce and preserves your investment as your automation needs grow.


Special - 20% Discount


In conjunction with our recently announced Silver Award from Windows IT Magazine we would like to offer our customers an opportunity to save 20% off any Devfarm product purchase through the end of the year. This includes all PowerWF products as well as Devfarm's new PowerVI product!

PowerVI

vSphere Automation fueled by PowerShell


Designed for the VMware Administrator, PowerVI eases the automation of vSphere infrastructures. PowerVI includes over 100 PowerShell automation scripts that simplify everyday VMware administration tasks and PowerVI makes it easy to author new scripts.
November 30, 2011

Persisting a Hash Table

I was on a chat earlier today with one of our users.  I’m not sure why, but he  wanted to find a way to persist a Hash Table between PowerShell sessions.  I was pretty sure the both Ed Wilson and Jeffery Hicks had addressed similar problems in the past, so I gave him links to their sites, but I figured there was probably a pretty easy way to do this.  Here’s what I came up with as a basic premise:

PS C:\Users\jhofer> $myHash = @{“foo”=1;”ack”=2}
PS C:\Users\jhofer> $myHash

Name                           Value
——                           ——-
foo                            1
ack                            2

PS C:\Users\jhofer> $myHash | Export-Clixml c:\myhash.xml
PS C:\Users\jhofer> $newHash = Import-Clixml C:\myhash.xml
PS C:\Users\jhofer> $newHash

Name                           Value
——                           ——-
ack                            2
foo                            1

PS C:\Users\jhofer> $newHash.Keys
ack
foo

So basically, it looks like he could add a line like $myHash = Import-Clixml C:\myhash.xml to his profile so that it would load his hash each time he starts up PowerShell.  He could then add a PowerShell function to that would add or update values in his hash table and then overwrite the xml file.  Here’s the final version of what I would add to the profile to accomplish this:

$global:persistentHash = @{}
$global:persistentHash = import-Clixml c:\Public\persistentHash.xml

function update-PersistentHash($key, $value)
{
    if($global:persistentHash.Contains($key))
    {
        $global:persistentHash.Remove($key)
    }

    $global:persistentHash.Add($key,$value)
    $global:persistentHash | export-Clixml c:\Public\persistentHash.xml -Force
   
}


blog comments powered by Disqus