I wrote about hash tables a while back in regards to counting OS types. The one thing I didn't lookinto back then was how to export the information to CSV. Easy, I hear you cry - just use export-csv! Not quite as easy as you would imagine.
The main thing to tackle is the fact that Hash Tables are not objects as we would usually consider them. If you run get-member on a hash table you get the following properties :
Export this to CSV and you get :
complete script :
Cheers!
The main thing to tackle is the fact that Hash Tables are not objects as we would usually consider them. If you run get-member on a hash table you get the following properties :
CountWhat is required is to create an object based on the hash table prior to exporting. Lets create a similar hash table to the one found in my previous post :
IsFixedSize
IsReadOnly
IsSynchronized
Keys
SyncRoot
Values
$complist = get-ADcomputer -filter {enabled -eq $true} -properties operatingsystem
$OSlist = @{}
$complist | % {try {$OSlist.$($_.operatingsystem) ++}catch{$OSlist.None ++}}
Export this to CSV and you get :
PS U:\> $OSlist | Export-Csv "OScount1.csv" -NoTypeInformationNot helpful really! Instead, convert the object like this :
PS U:\> type .\OScount1.csv
"IsReadOnly","IsFixedSize","IsSynchronized","Keys","Values","SyncRoot","Count"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","21"
new-object psobject -property $OSlist | Export-Csv "OScount1.csv" -NoTypeInformationBetter! At least we have data output now! But what I really wanted was 2 columns with lots of rows rather than 2 rows and lots of columns. This is how I worked around the issue :
$collection = @()
foreach ($key in $OSlist.Keys) {
$store = "" | select "OS","count"
$store.OS = "$Key"
$store.count = $oslist.$Key
$collection += $store
}
$collection | Export-Csv "OSCount2.csv" -NoTypeInformation
complete script :
$complist = get-ADcomputer -filter {enabled -eq $true} -properties operatingsystemAs a final thought, I looked at how to export multiple hash tables with multiple values out to a single CSV. This combine aspects from both the above solutions. I have emulated a 2 record foreach loop by writing the code twice for simplicity :
$OSlist = @{}
$complist | % {try {$OSlist.$($_.operatingsystem) ++}catch{$OSlist.None ++}}
$collection = @()
foreach ($key in $OSlist.Keys) {
$store = "" | select "OS","count"
$store.OS = "$Key"
$store.count = $oslist.$Key
$collection += $store
}
$collection | Export-Csv "OSCount2.csv" -NoTypeInformation
$collection = @()This Outputs :
$vars = @{}
$vars.Givenname = "Adam"
$vars.sn = "Stone"
$vars.address = "1 ABC Street"
$vars.City = "Some City"
$collection += new-object psobject -property $vars
$vars = @{}
$vars.Givenname = "John"
$vars.sn = "Citizen"
$vars.address = "2 ABC Street"
$vars.City = "Some other City"
$collection += new-object psobject -property $vars
$collection | Export-Csv "Vars2.csv" -NoTypeInformation
PS U:\> type .\Vars2.csv
"City","sn","address","Givenname"
"Some City","Stone","1 ABC Street","Adam"
"Some other City","Citizen","2 ABC Street","John"
Cheers!
Comments
Post a Comment