Welcome › Forums › General PowerShell Q&A › Trouble displaying sizes of multiple drives
- This topic has 3 replies, 2 voices, and was last updated 2 months, 3 weeks ago by
Participant.
-
AuthorPosts
-
-
September 11, 2019 at 11:20 pm #176254
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?
-
September 14, 2019 at 6:56 pm #177127
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. -
September 15, 2019 at 5:01 am #177235
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.
-
September 17, 2019 at 4:46 am #177763
That worked great. The only thing I had to change was $logicalDrive.Capacity to $logicalDrive.Size
Thank you.
-
-
AuthorPosts
- You must be logged in to reply to this topic.