This script will check a disk’s free space and send a warning if the server has dropped below the limit you have specified.
The settings are in a XML file. The main entity is the filesystem – let’s say a drive letter or disk. Each disk must be defined as an element, which includes values such as the hostname, limit, and list of recipents.
Both files for this solution must be in the same folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
$ScriptPath = $MyInvocation.MyCommand.Path $RunPath = Split-Path $ScriptPath $Settings = [xml](Get-Content "$RunPath\watchdog.config.xml") $Log = "$RunPath\$($Settings.freeSpaceWatchdog.globalSettings.log)" $SMTPServer = $Settings.freeSpaceWatchdog.globalSettings.smtpserver $EmailSender = $Settings.freeSpaceWatchdog.globalSettings.emailSender $Timestamp = Get-Date -Format "yyyy.MM.dd HH:mm" foreach ($Item in $Settings.freeSpaceWatchdog.filesystem) { $ComputerName = $($Item.Hostname) $Disk = $($Item.Volume) $Limit = [math]::round($($Item.Limit), 2) $EmailRecipients = $($Item.EmailRecipients) $Data = Get-WmiObject Win32_LogicalDisk -ComputerName $ComputerName -Filter "DeviceID='$Disk'" | Select-Object FreeSpace, Size $Size = [math]::round($Data.Size / 1GB, 2) $FreeSpace = [math]::round($Data.FreeSpace / 1GB, 2) if ($FreeSpace -le $Limit) { "$Timestamp - Warning - $Comptername disk $Disk, limit $Limit, freespace $Freespace" >> $Log Send-MailMessage ` -From $EmailSender ` -To $EmailRecipients ` -SmtpServer $SMTPServer ` -Subject "$Computername disk $Disk warning" ` -Body "On $ComputerName disk $Disk has exceeded the limit $Limit GB.`n`nSize $Size GB`nFree space $FreeSpace GB" ` -Priority High } else { "$Timestamp - OK - $Computername disk $Disk, limit $Limit, freespace $Freespace" >> $Log } } |
Here is an overview:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<freeSpaceWatchdog> <globalSettings> <smtpserver>mailer15</smtpserver> <emailSender>freespacelimit@mydomain.com</emailSender> <log>freespacewatchdog.log</log> </globalSettings> <filesystem volume="C:"> <hostname>mbx01</hostname> <limit>50</limit> <emailRecipients>helpdesk@mydomain.com</emailRecipients> </filesystem> <filesystem volume="E:"> <hostname>mbx02</hostname> <limit>70</limit> <emailRecipients>helpdesk@mydomain.com</emailRecipients> </filesystem> </freeSpaceWatchdog> |
Lze to zjistit pomocí nástroje netsh a vyfiltrování jeho výstupu. Protože se mi nepodařilo sestavit příkaz tak, aby se přímo netsh dotázal vzdáleného stroje (ví někdo jak na to?), použil jsem ještě psexec pro získání vzdálené příkazové řádky. Z prvního příkladu je vidět, že na vzdáleném stroji „pc023“ je přihlášen uživatel „Administrator“ z domény „OS“
C:\Temp>psexec \\pc023 cmd
PsExec v1.71 - Execute processes remotely
Copyright (C) 2001-2006 Mark Russinovich
Sysinternals - www.sysinternals.com
Microsoft Windows XP [Verze 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Temp>netsh diag show computer /v | findstr /i "user"
UserName = OS\Administrator
Nebo pomocí PsLoggedon
C:\Temp>psloggedon \\pc012
PsLoggedon v1.34 - See who's logged on
Copyright (C) 2000-2010 Mark Russinovich
Sysinternals - www.sysinternals.com
Users logged on locally:
2.12.2011 12:12:47 OS\strachotao
Users logged on via resource shares:
6.12.2011 14:51:14 OS\STRACHOTAO
C:\Temp>