question

DBANovice avatar image
DBANovice asked

Powershell WMI Drive Space

I am hoping this is the correct forum for this question. I am working on a PS script to monitor disk space on my SQL servers. I am familiar with PS but am having an issue and I think it is related to the WMI cmdlet which I have not used often. I am trying to get the size of the disk and pass it into a foreach loop in order to determine what the next action will be depending on disk size. This is the simplified command I am using. I am trying to save the results to a variable to pass in a foreach loop but the results are outputting it some sort of string table with a column header that i can not pass in my loop correctly. Any suggestions would be appreciated. Thanks You! $AllDisks= Get-WMIObject -ComputerName $computer Win32_LogicalDisk | select size > This is the how I am using the loop foreach ($computer in $computers) { $AllDisks= Get-WMIObject -ComputerName $computer Win32_LogicalDisk | select size foreach ($size in $AllDisks) { If ($size -lt 50 ) { write-host 'less than' } elseIf ($size -gt 50) { write-host 'greater than ' } } }
powershellspacewmi
3 comments
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

should the WMI-Object command be referencing -ComputerName $computer instead of -ComputerName $computers ?
0 Likes 0 ·
That typo seems to be one issue. Now I am getting the error: Cannot compare "@{size=107370971136}" to "50" because the objects are not the same type or the object "@{size=107370971136}" does not implement "IComparable". Working on trying to get them into the same datatype. Not sure why size would not be returned as an INT initially
0 Likes 0 ·
Any ideas how I can compare these values using -lt and -gt?
0 Likes 0 ·

1 Answer

· Write an Answer
Shawn_Melton avatar image
Shawn_Melton answered
If you are looking at making this into reusable code change this around to work on one server, or multiple. function Get-DiskSpace ([string[]]$server) { foreach ($s in $server) { $data = Get-WmiObject win32_logicaldisk -computername $s switch ($data.size) { {$_ -lt 50} {Write-Information "$s found disk $($data.Name) is Less than 50"} {$_ -gt 50} {Write-Information "$s found disk $($data.Name) is Greather than 50"} default {"Unknown"} } } } Get-DiskSpace -server 'Server1','Server2','Server3' If you want to keep with your same code you need to specify the property for "$size" as "$size.size". You can get around doing this by changing your variable command to this: $AllDisk = Get-WmiObject -ComputerName $computer win32_logicaldisk | select -ExpandProperty size This will expand the size property into a string so your "$AllDisks" variable will just hold a string list of all the sizes. You can also pass in an array of servers to the Get-WmiObject cmdlet, so doing something like `Get-WmiObject win32_logicaldisk -ComputerName $serverList` would pull in all the output for every server I pass in. You could then break this out in a foreach loop and check whatever property you want, and include whatever you want into a data table.
5 comments
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Thanks Shawn! Do you know why I am getting the error property size can not be found when I run the script you attached? I check the gm and do see that size is a property. Get-WmiObject win32_logicaldisk -computername $s | gm I am running PS 2.0 is that related?
0 Likes 0 ·
Mistyped, should be fixed now.
0 Likes 0 ·
It doesn't seem like the properties are being passed to $data, as I can not select any of the properties and the error that size property can not be found keeps happening. When enter $data | gm I see all the properties though.
0 Likes 0 ·
I got it working using the ExpandProperty parameter on my original code as you suggested but am curious to know why the function above is throwing the size property does not exist error
0 Likes 0 ·
If you are on PowerShell 2.0 you need to upgrade.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.