Get-WMIObject Win32_LogicalDisk returns disk size of 0 sporadically

This topic contains 2 replies, has 2 voices, and was last updated by  Erik Summers 1 year, 3 months ago.

  • Author
  • #38627

    Erik Summers

    I have been monitoring disk space on several servers for about 2 months now. The script runs every 15 minutes and sends me a list of drives that have less than X % of free space. Here's the meat of the code (credit to Sean Duffy on for the main logic of this):

    $tableFragment = Get-WMIObject -ComputerName $computers -ErrorAction "Continue" -ErrorVariable errors Win32_LogicalDisk `
    | select __SERVER, DriveType, VolumeName, Name, @{n='Size (Gb)' ;e={"{0:n2}" -f ($_.size/1gb)}},@{n='FreeSpace (Gb)';e={"{0:n2}" -f ($_.freespace/1gb)}}, @{n='PercentFree';e={"{0:n2}" -f ($_.freespace/$_.size*100)}} `
    | Where-Object {$_.DriveType -eq 3 -and [decimal]$_.PercentFree -lt [decimal]$thresholdspace} `
    | ConvertTo-HTML -fragment

    It then sends an email with an HTML table, using the tableFragment variable as the body. This all has worked swimmingly, except for one small hiccup. 2 times now, I have received an email, each time for a different server, that all of it's disks are reading a size of 0GB. Then the next time the script runs 15 minutes later, everything is reported as normal. Has anyone seen this before? It is not an issue of the script not being able to connect to the server, because 1) that throws a different error, which I am catching, and 2) the script reads each drive and reports it with the correct drive name, but just reports it as having a size of 0. The script has ran about 5,000 times over the last 2 months, and something caused 2 of those times to not be able to correctly read drive sizes on 2 different servers. Any ideas?

  • #38632

    Don Jones

    It's just a hiccup in how the server's WMI repository is querying information from the machine itself, usually under heavy I/O loads. IIRC, LogicalDisk was deprecated to Win32_Volume, which you might try.

  • #38691

    Erik Summers

    Thanks for the reply, Don. I have switched over to using Win32_Volume... we'll see if I see a repeat of the hiccup. Just a side note – I learned PowerShell by the "Learn Windows PowerShell in a Month of Lunches Second Edition" book. Thought I recognized your name when you replied to my question on here. Didn't realize you were an admin on this forum; small world! Thank you for your contributions and activity in the community, your book was a huge help to getting me started with PowerShell!

You must be logged in to reply to this topic.