Checking Available Disk Space and Memory with Powershell.

I frequently need to display the disk size, available disk space properties and Processor details of my remote Hyper-v 2012 Hosts servers. I could use the Computer Management snapin to do this, but it involves too many steps just to get there. The Get-VM cmdlet while cool, will not display disk based properties.

So I decided to write a quick script that accepts the remote computer name parameter, utilizes the Win32_LogicalDisk WMIObject and Win32_ComputerSystem classes to achieve.

First of all, I did a quick query of the WMI classes related to anything disk based:

Capture

I selected the Win32_Logicaldisk class. Using this class, I ran the following command to displace freespace and size properties of a remote Hyper-v 2102 VM Host server:

PS C:\> Get-WmiObject -Class Win32_logicaldisk -ComputerName hvs03

DeviceID : C:
DriveType : 3
ProviderName :
FreeSpace : 756848578560
Size : 897950150656
VolumeName :

DeviceID : D:
DriveType : 5
ProviderName :
FreeSpace : 0
Size : 4380329984
VolumeName : SQLFULL_ENU

Good news is that I get real time values for the properties I need. But, the values are in bytes and I really do not need all the displayed properties. So I cleaned up the command a bit more, converted the bytes values to gigabytes, formatted the result and put this in a function I can call anytime I need to query a remote Hyper-v host server for disk space availability :

function Get-HostProperties{

param ($computerName = (Read-Host "Enter Computer Name")
)
Get-WmiObject -Class win32_logicaldisk -ComputerName $computerName | ft DeviceID, @{Name="Free Disk Space (GB)";e={$_.FreeSpace /1GB}}, @{Name="Total Disk Size (GB)";e={$_.Size /1GB}} -AutoSize
}
Get-HostProperties

I also added another line of code to retrieve and display the Processor and Memory properties of the remote host:

function Get-HostProperties{

param ($computerName = (Read-Host "Enter Computer Name")
)

Get-WmiObject -Class win32_logicaldisk -ComputerName $computerName | ft DeviceID, @{Name="Free Disk Space (GB)";e={$_.FreeSpace /1GB}}, @{Name="Total Disk Size (GB)";e={$_.Size /1GB}} -AutoSize
Get-WmiObject -Class win32_computersystem -ComputerName $computerName|ft @{Name="Physical Processors";e={$_.NumberofProcessors}} ,@{Name="Logical Processors";e={$_.NumberOfLogicalProcessors}}, @{Name="TotalPhysicalMemory (GB)";e={[math]::truncate($_.TotalPhysicalMemory /1GB)}}, Model -AutoSize
}
Get-HostProperties

The result is displayed in the following screenshot:

Capture

I used the truncate() method of the .NET Math Class to remove the decimal points and report the total physical memory as a whole number.I hope I can further expand this simple script to include other routine tasks at some point.

Advertisement
This entry was posted in Hyper-v, Hyper-v 2012 R2, PowerShell, PowerShell 3.0, Virtual Machines, WIndows 8.1, Windows Server 2012 R2 and tagged , , , , , , , , . Bookmark the permalink.

1 Response to Checking Available Disk Space and Memory with Powershell.

  1. chuma says:

    This is good thinking.
    A very efficient way to acheive this.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s