windows:scripting:list_servers_with_low_disk_space

List servers with low disk space using powershell remoting

$servers = @(
    @{ "fqdn" = "adds01.example.com"; "username" = "administrator"; "password" = "p@55w0rd" },
    @{ "fqdn" = "adds02.example.com"; "username" = "administrator"; "password" = "p@55w0rd" },
    @{ "fqdn" = "tfs.example.com"; "username" = "administrator"; "password" = "p@55w0rd" }
)
 
foreach ($server in $servers) {
    try {
        $cred = New-Object System.Management.Automation.PSCredential -ArgumentList ($server.username, (ConvertTo-SecureString $server.password -AsPlainText -Force))
        $session = New-PSSession -ComputerName $server.fqdn -Credential $cred
 
        $cmd = {
            Get-WmiObject Win32_LogicalDisk | Where-Object { $_.DriveType -eq 3 -and $_.FreeSpace -lt $_.Size * 0.05 } | Select-Object DeviceID, @{Name='Utilization';Expression={($_.Size - $_.FreeSpace) / $_.Size * 100}}
        }
 
        $result = Invoke-Command -Session $session -ScriptBlock $cmd
 
        if ($result) {
            Write-Host "Low disk space warning on $($server.fqdn):"
            $result | Format-Table -AutoSize
        }
    } catch {
        Write-Host "Failed to connect to $($server.fqdn): $_"
    } finally {
        if ($session) {
            Remove-PSSession $session
        }
    }
}
  • windows/scripting/list_servers_with_low_disk_space.txt
  • Last modified: 2024/02/05 08:53
  • by tplecko