1

I'm currently trying to use PowerShell to create a function that would give me a list of computers and their diskdrive serial numbers. I need it to display in a hash table with 2 columns.

Here is my function so far. I thought that by creating objects and specifying what member to get and display, that it would create the array that I needed, but when I run the function, I get an output but the Disks column still outputs as an array of objects. I need to turn it into a hashtable. Do I need to merge the two in order to do that?

function Get-DiskInventory {
    [string[]]$computers = $env:COMPUTERNAME

    foreach ($computer in $computers) {
        $system = Get-WmiObject win32_ComputerSystem -Computername $computer
        $disks = Get-WmiObject win32_DiskDrive -ComputerName $computer

        $computerobj = New-Object -TypeName PSCustomObject
        $computerobj | Add-Member -MemberType NoteProperty -Name Name -Value $computer

        #Gather Disk Info, collect mulitple disks
        $diskinfo = @()
        foreach ($disk in $disks) {
            $diskobj = New-Object -TypeName PSObject
            $diskobj | Add-Member -MemberType NoteProperty -Name SerialNumber -Value $disk.SerialNumber
            $diskinfo += $diskobj
        }

        #add disk collector to computer obj
        $computerobj | Add-Member -MemberType NoteProperty -Name Disks -Value $diskinfo

        $computerobj
    } #End foreach computer

} #End Fucntion

Get-DiskInventory
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
J.Soule
  • 13
  • 2
  • Can you edit your question and mock up an example of what you'd like the output to look like? I'm not completely clear currently. – Mark Wragg Apr 28 '17 at 09:44

1 Answers1

0

I think you're misunderstanding how hashtables work. Hashtables are essentially dictionaries where you have a list of unique keys and another list of corresponding values. If I understood your question correctly you want output like this:

ComputerName   SerialNumber
------------   ------------
ComputerA      1234...
ComputerA      3456...
ComputerB      9876...
ComputerB      7654...
...            ...

but you're actually getting something like this:

ComputerName   Disks
------------   ------------
ComputerA      {@{SerialNumber=1234...
ComputerB      {@{SerialNumber=9876...
...            ...

For the former output you cannot use a hashtable, because the keys (the computer names) aren't unique. Since your code doesn't use hashtables anyway that's not a problem here, though.

However, you need to create distinct objects per disk, not add all disks of one computer to the same object. Also, the WMI object already has a property with the computername, and the -ComputerName parameter accepts an array as input, so you should be able to simplify your code to something like this:

function Get-DiskInventory {
  Param(
    [Parameter(Mandatory=$false)]
    [string[]]$computers = $env:COMPUTERNAME
  )

  Get-WmiObject Win32_DiskDrive -ComputerName $computers |
    Select-Object SystemName, SerialNumber
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328