Author Posts

June 5, 2018 at 6:52 pm

I've a script that holds a Hashtable with Customers name and the Director who manages that customer.The first step of the script is to check for service desk tickets for requests to add new Customers, and if there is, have those new Customers names added to the Hashtable. The step I'm not able to figure out is how I save the script so that the Hashtable, including the new values, is in effect hard coded back to the script, so they're there next time the script is run.
Or is there a better way to go about this ?

June 5, 2018 at 7:17 pm

You would normally store this in an XML or CSV or actual database.

$mySavedData = "{0}\MySavedData.xml" -f $PSScriptRoot

if (Test-Path -Path $mySavedData) {
    "Importing Saved Data"
    $myData = Import-CliXML -Path $mySavedData
}
else {
    "Creating New Data"
    $myData = New-Object -TypeName PSObject -Property @{
        Name = "John"
        Age = 30
    }
}


#...Manipulate Data

$myData | Export-CliXML -Path $mySavedData

The first time you run this, you will see it creating a new record. The second you will see it using the saved data. If you are storing a actual hashtable, I would still create a PSObject and use Export-CliXML to keep it as hashtable for easy export\import:

PS C:\Users\Rob>     
    $hashTable = @{MyKey="MyValue"}

    $myData = New-Object -TypeName PSObject -Property @{
        MyHashTable = $hashTable
    }

PS C:\Users\Rob> $hash = $myData.MyHashTable

PS C:\Users\Rob> $hash

Name                           Value                                                                                                                                                              
----                           -----                                                                                                                                                              
MyKey                          MyValue                                                                                                                                                            



PS C:\Users\Rob> $hash.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                               
-------- -------- ----                                     --------                                                                                                                               
True     True     Hashtable                                System.Object 

I've also done it where I keep a certain number of files, say 10, as data backups. You would create the files with date\time and then use Get-ChildItem to get the last file when the script is loaded.

June 5, 2018 at 7:24 pm

Thanks for the suggestion Rob, I'll give that a try.
I could store the info a xls/csv/DB but was trying to keep everything in 1 script. Thought I'd ask for suggestions before I give in and store the values externally to the script.
I appreciate the help.