Trouble displaying sizes of multiple drives

I am trying to display the drive letter & drive size of multiple drives on one or more servers. So far I am testing on one server only.

When I run this against a server with 2 local drives (one with a capacity of 40 GB & the other with a capacity of 1821 GB) only the first drive is shown & the size is incorrect:

ComputerName : security5
OSVersion : 6.1.7601
SPVersion : 1
OSBuild : 7601
Manufacturer : Dell Inc.
Model : PowerEdge 2950
Procs : 1
Cores : 4
RAM : 3.99505615234375
Arch : 64
SysDriveFreeSpace : 9670627328
Disk1 ID : C:
Disk1 Size : 235.48
Disk2 ID :
Disk2 Size :
Disk3 ID :
Disk3 Size :
Disk4 ID :
Disk4 Size :

I don’t get any errors when I run this. Any better way of doing this?


Try this - make drive population dynamic by looping through them and adding to the PSCustomObject:

$obj = [pscustomobject]@{
    'ComputerName' = $computer
    'OSVersion' = $os.version
    'SPVersion' = $os.servicepackmajorversion
    'OSBuild' = $os.buildnumber
    'Manufacturer' = $cs.manufacturer
    'Model' = $cs.model
    'Procs' =$cs.numberofprocessors
    'Cores' = $cs.numberoflogicalprocessors
    'RAM' = ($cs.totalphysicalmemory / 1GB)
    'Arch' = $proc.addresswidth
    'SysDriveFreeSpace' = $drive.freespace

$diskCounter = 0

foreach ($logicalDrive in $drives) {
$obj | Add-Member -MemberType NoteProperty -Name (‘Disk{0}Id’ -f ($diskCounter + 1)) -Value $logicalDrive.DeviceId
$obj | Add-Member -MemberType NoteProperty -Name (‘Disk{0}Size’ -f ($diskCounter + 1)) -Value $logicalDrive.Capacity

$diskCounter += 1


Write-Output $obj

Edit: Sorry for multiple edits; formatting issues.

A little more explaining:

Most of your remote queries are using CIM cmdlets with a session object, except for the one below, and this is likely the reason why results for only one drive appears when running against other systems. Do you only have one drive on your system?

When assigning the `$drives` variable, you are using `Get-WmiObject` without a `-ComputerName` parameter, which means it is actually running against your local computer:

$drives = Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '3'" | ...

Change it to CIM as the other cmdlets:

$drives = Get-CimInstance -CimSession $session -ClassName Win32_LogicalDisk -Filter "DriveType = '3'" | ...

By making these changes, and from my previous response, you should get the expected output.

Edit: Sorry, formatting issues again.

That worked great. The only thing I had to change was $logicalDrive.Capacity to $logicalDrive.Size


Thank you.