Saturday, 13 March 2021 15:36

    Check drive space for multiple servers using PowerShell

    Written by
    Rate this item
    (0 votes)

    Here is the script to check Disk Space of multiple servers:

    The output is a html file in table format. This script also highlights if free space % is less than threshold.

    $servers = Get-Content servers.txt;
    $output_file = "DriveSpaceReport" + (get-date -UFormat "-%Y-%m-%d-%H_%M") + ".html";
    Add-Content $output_file "<html><head><title>Drive Space Report</title><style>table { border-collapse: collapse;} table, td, th { border: 1px solid black;} </style></head><body>";
    Add-Content $output_file ("Report Started at: " + (Get-Date));
    Add-Content $output_file "<table border='1'><tr><th>DeviceID</th><th>Size (GB)</th><th>FreeSpace (GB)</th><th>FreeSpace %</th></tr>";
    foreach($server in $servers)
    {
        Add-Content $output_file ("<tr><td colspan='4'>" + $server + "</td></tr>");
        
        $drivesinfo = Get-WmiObject win32_logicaldisk -ComputerName $server | Where-Object {$_.DriveType -eq 3};
        foreach($driveinfo in $drivesinfo)
        {
            if(($driveinfo.FreeSpace/$driveinfo.Size)*100 -lt 10)
            { $option = "<td bgcolor='Red'>"; }
            elseif(($driveinfo.FreeSpace/$driveinfo.Size)*100 -lt 20)
            { $option = "<td bgcolor='Yellow'>"; }
            else {$option = "<td>"};
            Add-Content $output_file ("<tr><td> " + $driveinfo.DeviceId + "</td><td>" + [Math]::Round($driveinfo.Size/1GB, 2) + "</td><td>" + [Math]::Round($driveinfo.FreeSpace/1GB, 2) + "</td>"+ $option + [Math]::Round(($driveinfo.FreeSpace/$driveinfo.Size)*100, 2) + "</td></tr>");
        }
    }
    Add-Content $output_file "<br/><br/>";
    Add-Content $output_file ("</table><br/><br/>Report Completed at: " + (Get-Date) + "</body></html>");
    Invoke-Item $output_file;
    

    The HTML output is as follows:

    Disk Space Report Started at: 07/10/2018 23:45:49

    DeviceID Size (GB) FreeSpace (GB) FreeSpace %
    dc01
    C: 59.66 49.14 82.36
    srv01
    C: 59.66 49.64 83.2
    E: 2 0.15 7.63

    Report Completed at: 07/10/2018 23:45:49 

    This script reads a .txt file for the list of servers. Save the list of servers one server per line to servers.txt and keep it in the same folder.

    Downloads:
    (0 votes)
    DateSaturday, 13 March 2021 17:49
    File Size 814 B
    Download 13

     

    Read 61 times Last modified on Thursday, 18 March 2021 18:24
    More in this category: Get Installed Applications »

    Leave a comment

    Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.

    © 2021 WinAdmin.org. All Rights Reserved.