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 '   
more ▼

asked Jun 16, 2016 at 01:48 PM in Default

avatar image

130 1 1 6

should the WMI-Object command be referencing

 -ComputerName $computer

instead of

 -ComputerName $computers


Jun 16, 2016 at 03:43 PM ThomasRushton ♦♦

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

Jun 16, 2016 at 03:50 PM DBANovice

Any ideas how I can compare these values using -lt and -gt?

Jun 16, 2016 at 05:14 PM DBANovice
(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jun 16, 2016 at 05:24 PM

avatar image

6.5k 21 26 34

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?

Jun 16, 2016 at 06:04 PM DBANovice

Mistyped, should be fixed now.

Jun 16, 2016 at 06:13 PM Shawn_Melton

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.

Jun 16, 2016 at 06:25 PM DBANovice

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

Jun 16, 2016 at 06:50 PM DBANovice

If you are on PowerShell 2.0 you need to upgrade.

Jun 16, 2016 at 07:36 PM Shawn_Melton
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: Jun 16, 2016 at 01:48 PM

Seen: 76 times

Last Updated: Jun 16, 2016 at 07:36 PM

Copyright 2018 Redgate Software. Privacy Policy