Roxx
Roxx

Reputation: 3986

Powershell: Get physical drive count and size

I am trying to get size of physical drive on multiple computers. Some computer have 1 physical drive and some have more than 1. I tried this script and it is working fine if i run it locally on one machine.

$s = "machine name"
$counts = GET-WMIOBJECT –query "SELECT * FROM Win32_DiskDrive" -ComputerName $s
$total_disk =  $counts.count
$i = 0
$total_disk = $total_disk -1
$infoObjecta = New-Object PSObject
for(; $i -le $total_disk; $i++){

$a = $i
$a  = GET-WMIOBJECT –query "SELECT * FROM Win32_DiskDrive WHERE DeviceID='\\\\.\\PHYSICALDRIVE$i'" -ComputerName $s
$b = $i
$b = [math]::round($a.size /1GB)


Add-Member -inputObject $infoObjecta -memberType NoteProperty -name "Physical_Disk $i" -value $b

}

OS versions are Win Server 2003, 2008 & 2012.

I am not sure what i am doing wrong. If i run this script locally on that machine or give machine name is $s variable then it is working fine. But if i put remote machine name in $s then it is not giving me output.

I debug and found $counts.count doesn't return any value.

Please advise me how to fix this or any alternate way to get the details of hard drive count and size.

Upvotes: 2

Views: 2896

Answers (1)

amit dayama
amit dayama

Reputation: 3326

In some version of Powershell if variable is 1 or 0 it doesn't return anything. check if @($counts).count returns the value.

Upvotes: 2

Related Questions