Tag Archives: hashtables

Hash tables in powershell

We all have the need to store data in some kind of arrays. I use hashtables a lot. Preferred use is as a lookup table, I can use ‘contains’ instead of looping through each item or reference an object by name instead of index number. Lookup table for licenses is where I use it the most at the time being. A table of license ID versus license name. One place for this is Microsoft licenses in partnercenter, and show licenses assigned to users. Using powershell to connect to Microsoft partnercenter to get all available skus by ID , name and displayname. When I have collected this – how do you ask?.
Here you go:
[cc lang=”powershell”] Import-module partnercenter
Connect-CompanyPartnerCenter

$lic = @{ }
$prodid = @{ }
$customerstenants = Get-PartnerCustomer
ForEach ($customer in $customerstenants) {
$message = “Customer :$($customer.name)”
write-verbose -Message “$message”
$custskus = Get-PartnerCustomerSubscribedSku -CustomerId $customer.customerid
foreach ($custsku in $custskus) {
if ($lic.ContainsKey($custsku.SkuPartNumber)) {

}
else {
$lic.Add($custsku.skupartnumber, $custsku.productname)
$prodid.Add($custsku.SkuId, $custsku.ProductName)
}

}
}
[/cc]

Connect-companypartnercenter is a custom function. Here is how to connect to Office partnercenter.
The script above will (hopefully , no try-catch) end up in 2 hash tables. One “skuid” and productname:

"SkuID","DisplayName"
"WACONEDRIVEENTERPRISE","OneDrive for Business (Plan 2)"
"O365_BUSINESS_ESSENTIALS","Office 365 Business Essentials"
"MCOSTANDARD","Skype for Business Online (Plan 2)"
"Win10_E3_Local","Windows 10 Enterprise E3 (local only)"
"WACONEDRIVESTANDARD","OneDrive for Business (Plan 1)"

As long as I’m using this in the same script it is ok. Exporting it to files is not that straight forward, even tried ‘convertto-json’ –not working (convertfrom-json returns an array)
So I do this to crate a csv file. Creating correct columns.
[cc lang=”powershell”]
$csvobject = ($lic.GetEnumerator() | Select-Object @{expression = { $_.name }; label = “SkuID” }, @{expression = { $_.value }; label = “DisplayName” } | convertto-csv -Delimiter ‘,’ -NoTypeInformation )
$csvobject | Out-File -Encoding unicode $ExportSkuIDFile

$csvobject = ($prodid.GetEnumerator() | Select-Object @{expression = { $_.name }; label = “ProductID” }, @{expression = { $_.value }; label = “DisplayName” } | convertto-csv -Delimiter ‘,’ -NoTypeInformation )
$csvobject | Out-File -Encoding unicode $ExportProductIDFile
[/cc]
Now that we have the file it would be great to be able to use them 🙂
And this is how it is imported in to a hashtable.
[cc lang=”powershell”]
$filecontent=import-csv -Path $importfile -Encoding Unicode -Header “column1″,”column2”
$hashtable=@{}
$filecontent|ForEach-Object{
write-verbose -message “$_.column1 , $_.column2”
$hashtable.add($_.column1,$_.column2)
}
[/cc]
Read the file line by line and build our hash-table. Now we can use : $hashtable[“MCOSTANDARD”] , this will return the full name ‘Skype for Business Online (Plan 2)’
I have this as functions in my default module, the module I import in all my powershell sessions. (Surely you know about powershell profiles?)
Hope this helped.