Author Posts

July 13, 2015 at 10:44 pm

What is the format suppose to be for the strings which represent computer names and the partition?

The reason why I am asking is that my free space is always returning zero on the powershell session? This is not correct, because the real free space is 60 gigs.

This is the code used

`
$ip = "151.22.34.45"
$partition = "DeviceID ='E'"
$description = "backup_server"

$ConvertToGB = (1024 * 1024 * 1024)

$disk = Get-WmiObject Win32_LogicalDisk -ComputerName $ip -Filter $partition | Select-Object Size,FreeSpace

$ip + $partition + $description + ([math]::round(($disk.Size / $ConvertToGB),2)) + "GB" + "," + ([math]::round((($disk.FreeSpace / $ConvertToGB)),2)) + "GB"
`

Can someone please advise if the something is needed to be added to the strings for the computer name and partition name? Or perhaps special characters or extra commas or quotes etc???

July 13, 2015 at 11:01 pm

I advice You do not write such a long code if you get any error ;^)
If You launch just small sample – Get-WmiObject Win32_LogicalDisk -ComputerName $ip, you will see that DeviceID is "E:" but not "E", do not get $null in $disk variable and do not get FreeSize=0Gb
🙂

July 14, 2015 at 1:10 am

AS Noted above the DeviceId MUST include the :

DeviceId is the key property for the class. The correct usage is shown on the Path and __Relpath properties

July 14, 2015 at 1:21 am

Looking at you code you can make some improvements to take advantage of PowerShell's builtin features

$computername = $env:COMPUTERNAME
$partition = 'E:'
$description = 'backup_server'

Get-WmiObject -Class Win32_LogicalDisk -ComputerName $computername |
select PSComputerName,
@{Name='Partition'; Expression={$_.DeviceId}},
@{Name='Description'; Expression={$description}},
@{Name='Size(GB)';Expression={[math]::Round(($_.Size / 1GB), 2)}},
@{Name='FreeSpace(GB)';Expression={[math]::Round(($_.FreeSpace / 1GB), 2)}}

Output an object rather than a string
The computername you can get from the PSComputerName property that is added to all remote WMI results. You can perform the calculations directly in select-object. Use 1GB (powershell understand KB, MB, GB, TB, PB) as the divisor