It is just to understand Hash Tables better


hello dear sirs

as per subject, tried following code:

$computername = "."  $physical_specs = get-wmiobject -computername $computername  win32_diskdrive | `                 select name, maxcapacity, memorydevices, removable, `                        replaceable, serialnumber, version -unique $specs = $null $specs = @{} foreach ($spec in $physical_specs) {     $specs.add("id", $spec.deviceid)      $specs.add("caption", $spec.caption)     $specs.add("manufacturer", $spec.manufacturer)      $specs.add("type", $spec.type)     $specs.add("signature", $spec.signature)     $specs.add("model", $spec.model)     $specs.add("serial#", $spec.serialnumber)     $specs.add("interface", $spec.interface)     $specs.add("partitions", $spec.partitions)     $specs.add("sectors / track", $spec.sectorspertrack)     $specs.add("cylinders", $spec.totalcylinders)     $specs.add("heads", $spec.totalheads)     $specs.add("sectors", $spec.sectors)     $specs.add("tracks", $spec.totaltracks)     $specs.add("tracks / cylinder", $spec.trackpercylinders)     $specs.add("heads", $spec.totalheads)     $specs.add("status", $spec.status) }  $specs

when run code, screen glorifies error messages complaining "the item has been added...." every item.

please me understand reason , oblige.



your get-wmiobject returning multiple disk objects, each same properties.  

the first $spec through loop populate hash table keys , values.  each subsequent $spec trying re-add same keys, , giving error.

note difference if (moving hash table initialization , output inside loop, you're doing once each drive)

$computername = "."  $physical_specs = get-wmiobject -computername $computername  win32_diskdrive | `                 select name, maxcapacity, memorydevices, removable, `                        replaceable, serialnumber, version -unique $specs = $null foreach ($spec in $physical_specs) {      $specs = @{}      $specs.add("id", $spec.deviceid)      $specs.add("caption", $spec.caption)     $specs.add("manufacturer", $spec.manufacturer)      $specs.add("type", $spec.type)     $specs.add("signature", $spec.signature)     $specs.add("model", $spec.model)     $specs.add("serial#", $spec.serialnumber)     $specs.add("interface", $spec.interface)     $specs.add("partitions", $spec.partitions)     $specs.add("sectors / track", $spec.sectorspertrack)     $specs.add("cylinders", $spec.totalcylinders)     $specs.add("heads", $spec.totalheads)     $specs.add("sectors", $spec.sectors)     $specs.add("tracks", $spec.totaltracks)     $specs.add("tracks / cylinder", $spec.trackpercylinders)     $specs.add("heads", $spec.totalheads)     $specs.add("status", $spec.status)      $specs } 



[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Motherboard replacement

Cannot create Full Text Search catalog after upgrading to V12 - Database is not fully started up or it is not in an ONLINE state

Remote Desktop App - Error 0x207 or 0x607