PowerShell: Controllo dello spazio su disco ed invio mail
Un esempio PowerShell di come controllare lo spazio su dico e inviare una mail
PowerShell: CheckDiskSpace.ps1
#########################################################
# powershell .\CheckDiskSpace.ps1
# controlla lo spazio su disco libero ed invia una mail
# quando lo spazio libero è inferiore alla sogli indicata in $min
#########################################################
#soglia per l'allert in GB
$min = 2;
#########################################################
$warningString = "* Warning < $min GB"
$smtpServer = "smtp.xxxx.net"
$emailFrom = "alert@xxxx.net"
$emailTo = "user1@xxxx, user2@xxxx.net"
$subject = "Report Disk Space ({0})" -f $env:computername
#########################################################
$minByte = $min * 1GB
$drives = Get-WmiObject Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}
$frmt = @{l="Drive";e={"{0}" -F ($_.DeviceID)};width=10},`
@{l="Message";e={if($_.freespace -lt $minByte) {$warningString} else {"OK"}};width=20}, `
@{l="Free %";e={"{0:N2}" -f ([long]$_.FreeSpace / [long]$_.Size * 100)};width=10}, `
@{l="Free GB";e={"{0:N2}" -f ($_.FreeSpace / 1GB)};width=10}, `
@{l="Size GB";e={"{0:N2}" -f ($_.Size / 1GB)};width=10}
$result = $drives | Format-Table $frmt | out-string
if($result.Contains($warningString) )
{
$body = "Server: {0} - Date: {1:dd MMM yyyy HH:mm:ss} `r`n $result `r`n `r`n-- End Report --" -f $env:computername, [DateTime]::Now
#send email
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
write-host $body
}