2

Let’s say you're doing something like this:

$partitions = get-wmiobject -query "Associators of {Win32_DiskDrive.DeviceID=""$DevID""} WHERE AssocClass = Win32_DiskDriveToDiskPartition" -computer $ComputerName

If you then want to get the disk caption for each disk (the 'C:') you need something like this:

function GetDiskCaption {
   param ($Partition, $ComputerName)

    $DeviceID = $Partition.DeviceID
    $colLogicalDisk = get-wmiobject -query "Associators of {Win32_DiskPartition.DeviceID=""$DeviceID""} WHERE AssocClass = Win32_LogicalDiskToPartition" -computer $ComputerName

   If ($colLogicalDisk.Caption -ne $null) {
      return $colLogicalDisk.Caption
   }
   Else {
       return "UNASSIGNED"
   }
}

I'm not a very advanced PowerShell user, so the question is: What's the best PowerShell way to add the disk caption to the array so that can pipe it into, say, ft or some other script?

Now I realise I can just do a foreach loop to print this out, but I want to work out how to extend an array with extra columns so that I can then use piping operators to, say, format the array.

The examples on the Microsoft site on array handling are all based on one-dimensional arrays, so adding arrays does not seem to be the answer.

I'm specifically trying to write a script to dump out the list of disks, with their iSCSI mapping (if any) and the various partitions on that disk.

1 Answer 1

7

You can either use a hashtable (associative array):

$hash = @{}
$partitions | %{ $caption = GetDiskCaption $_ $computername; $hash[$caption]=$_ }
$hash

Or create objects:

$combined = $partitions | %{ $caption = GetDiskCaption $_ $computername
                $obj = new-object psobject
                $obj | add-member -name caption -type noteproperty -value $caption
                $obj | add-member -name partition -type noteproperty -value $_
                $obj
            }
1
  • perfect, it was the second syntax I've been fighting with. I got to the point where I just thought there had to be better way. The first maybe simpler. I'll have to look into associative arrays.
    – Ian
    Commented Oct 18, 2011 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.