2021 Dec 19

Checking The Free Space on the Servers

At some point you might want to have the overview of the drives on your Wintel servers, and of course, there are some monitoring tools like Zabbix or SCOM, but in case you don't have them, there is still a room for maneuver. 

In this example, we will take all the enabled servers in ActiveDirectory, and check the free space on the logical drives, and for this, we can have a script like this:

$Computers = Get-ADComputer -filter {operatingsystem -like "*server*" -and enabled -eq $true} | sort name
$Table = foreach ($computer in $computers) {
Get-WmiObject -Computername $computer.name -Class Win32_LogicalDisk | ? {$_.DriveType -ne 5} | Select @{n="Server"; e={$computer.name}},DeviceID,@{n="Size (GB)";e={($_.Size/1GB).ToString('F3')}},@{n="Free (GB)";e={($_.FreeSpace/1GB).ToString('F3')}}, @{n="% Free"; e={(($_.FreeSpace/$_.Size)*100).ToString('F2')}}
}
$Table | FT

So, let's break it down. On the first line, we create an array/list of computers in the domain, and check them to have "Server" in the name, The asterisks before and after it mean all the possible combinations of characters on both sides of the word, it wants to have it more exact, like only for Windows Server 2016 and 2019, feel free to change the string to something like Windows Server 201*. Just don't forget, the OperatingSystem attribute includes the full naming in the version, so it will have Standard/Datacenter in it, and you will still need an asterisk in the end if you don't specify the exact match, otherwise you won't have the results you need.

Lines 2-5 are made for making the script more readable, in theory you can make the whole thing in one line, but never forget about people who might want to read it later, including yourself. So, line 5 is for formatting the output into the table (FT is an alias for Format-Table, you can export the data to the .CSV file: just replace it with the Export-CSV and put the path to the file in the quotes, like "C:\Temp\FreeSpace.csv"). Lines 2-5 are more interesting right now, but what they do?

We assign the variable $Table the data we get from going through one computer/server at a time, and using the good-old Get-WMIObject command we can use for multiple things (including checking for the MSI product codes), and we check the Win32_LogicalDisk now. Then we specify not to have the CD/DVD-Rom in the outuput (the ? is an alias for Where-Object), and drive type 5 is the CD/DVD, you can find more here. After that, we need to have some magic, and put the data on the list, so it looks more reasonable and clear. The Select section meant we put the real server name, as it is known to the domain (without the domain part, it's just a name), put the deviceID — that is the actual logical disk letter, and end up with some math, as you will probably want to see the output in GB, so we calculate it to show like this, and with ToString('F3') we show only 3 decimals. In the script, we show the free size and the total space, also we calculate the percentage of the free space, we just need to divide free by total and multiply by 100, as it is more human-friendly.

In the end, the script will take some time to calculate everything (don't panic, it doesn't show the results instantly — it needs to go through the loop), and you should see something like this, the computers are sorted by name (we ensure this on the 1st line). This is the part of the output not to make the image too long:

They posted on the same topic

Trackback URL : https://dykhl.in/trackback/10

This post's comments feed