PowerShell Scripts

From Hostek.com Wiki
Revision as of 15:46, 16 May 2013 by Davidd (Talk | contribs) (Server Management PowerShell Scripts)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This is a repository of PowerShell Scripts for server management tasks.

Disk Usage Warning

This script allows you to send an e-mail if disk free-space is below a particular percentage.

#SETTINGS
$Computers = 'server1' # multiple servers should be formmated: 'server1', 'server2', 'server3'.  The user executing this script must have administrative access to all servers.
$WarnPercentage = 15
$SmtpHost = 'mail.domainname.com'
$SmtpPort = 25
$FromAddress = 'disk-monitor@domainname.com'
$Recipients = 'admin@domainname.com' # multiple recipients should be separated with commas: 'user1@domainname.com,user2@domainname.com,user3@domainname.com'
#SETTINGS

#GET DISK OVERAGES
$Overages = Get-WmiObject Win32_LogicalDisk -Filter "DriveType=3" -ComputerName $Computers| `
    Where { (100 * ($_.FreeSpace / $_.Size)) -le $WarnPercentage }| `
    Select SystemName, DeviceID, @{ Name="FreeSpace(GB)"; Expression={"{0:N}" -f ($_.FreeSpace / 1GB) }}

$OverageCount = @($Overages).Length
#GET DISK OVERAGES

if ($OverageCount -gt 0) {
    #SEND EMAIL
    $Subject = "$OverageCount drives are below $WarnPercentage% free space"
    $Body = "Warning, the following drives are below $WarnPercentage% free space:`r`n`r`n$($Overages|Out-String -Width 80)"
    $SmtpClient = New-Object System.Net.Mail.SmtpClient($SmtpHost, $SmtpPort)
    $SmtpClient.Send($FromAddress, $Recipients, $Subject, $Body)
    $SmtpClient.Dispose()
    #SEND EMAIL
}