[分享] 定期監控磁碟空間,可自動發信
-
2010年12月30日 上午 11:341. 建立一個純文字檔 computers.txt,裡面每一行輸入一台要監控的伺服器名稱如下
ad.example.com
mail.example.com
sql.example.com
$SMTP=寄信伺服器IP
$WARING=低於多少MB要發信警告
$TO=要收到警告信件的收件人
2.
將下列程式碼存成 .ps1 檔案,並排入工作排程器既可監控磁碟空間
$computers=(Get-Content computers.txt)
$WARING='5000'
$SMTP='smtp.example.com'
$TO='mymail@hotmail.com'
foreach ($computer in $computers)
{
#$ICMP=get-wmiobject -query "select * from win32_pingstatus where address='$COMPUTER'"
$ICMP=get-WmiObject Win32_PingStatus -filter "Address='$COMPUTER' "
if ($ICMP.StatusCode -eq '0')
{
Get-WmiObject -computer $COMPUTER win32_logicaldisk |? {$_.drivetype -eq '3'}|%{
if (([int32]($_.freespace/1MB)) -le "$WARING")
{
$ID=$_.deviceid
$F=[int32]($_.freespace/1MB)
$S="Waring $computer disk $ID Freespace $F MB"
Send-MailMessage -to $TO -from diskmonitor@example.com -subject "$S" -body "$S" -smtp $SMTP
}
}
}
else
{
$N="Waring $computer not respond !"
Send-MailMessage -to $TO -from diskmonitor@example.com -subject "$N" -body "$N" -smtp $SMTP
}
}
- 已變更類型 William-IT 2010年12月30日 上午 11:35
- 已編輯 TechNet 老妹 2011年1月3日 上午 08:49 add event tag
所有回覆
-
2011年1月17日 上午 08:09慘兮兮,兩個多禮拜只有兩票覺得有用,是沒這個需求還是沒人用Powershell ?
-
2011年1月17日 上午 08:27其實我覺得主要狀況在於 PowerShell 還是需要一點程式的底子,
蠻多 IT 人員對程式就不是這麼熟,
遇到像是比較複雜的 PowerShell ,
就可能就不清楚該怎麼改成適合自己公司的。 :)
蘇老碎碎念
資訊無涯,回頭已不見岸
好用的微軟技術支援小工具
Facebook - 微軟台灣官方論壇愛好者俱樂部
如何在論壇正確發問,請參考iThome的文章: 如何問到我要的答案

-
2011年5月26日 上午 01:08
請問這可以直接設定在排程嗎
因為我試了用.bat去執行 .ps1但會出現安全性的問題而不執行...
還是有其他方法可以下排程?
欣賞羽球 發球 殺球 平球 長球 -
2011年5月26日 下午 01:17
因為我試了用.bat去執行 .ps1但會出現安全性的問題而不執行...
由於您未提及訊息是什麼,所以先假設您的作業系統是 Windows Server 2008,請以「系統管理於身份」執行 Windows PowerShell,將「執行原則」設定為 RemoteSigned,亦即執行指令:Set-ExecutionPolicy RemoteSigned
☞ 這裡是「免費的討論區」,付費支援服務請造訪此處,享受尊榮服務 ☜
如果回覆對您有幫助,請記得按下「
標示為解答」。 -
2011年7月29日 上午 11:37請問除了監看磁碟空間外,有辦法用powershell監看記憶體容量嗎?
-
2011年8月1日 下午 03:16
有辦法用powershell監看記憶體容量嗎?
只要 .NET 語言可以寫出來,用 PowerShell 一樣也可以寫出來,可參考:
Get-FreeMemory.ps1
☞ 這裡是「免費的討論區」,付費支援服務請造訪此處,享受尊榮服務 ☜
如果回覆對您有幫助,請記得按下「
標示為解答」。 -
2011年11月21日 上午 07:35
大大~~
請教一下~~我執行後會發生以下訊息~~不知問題出在那?
執行的環境是XP有關係嗎?
Send-MailMessage : 無法連接至遠端伺服器
位於 行:20 字元:25
+ Send-MailMessage <<<< -to $TO -from leon.huang@sheraton-hsinchu.com -subject "$N" -body "$N" -smtp $SMTP
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
ion
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessageGet-WmiObject : 無法取得 RPC 伺服器。 (發生例外狀況於 HRESULT: 0x800706BA)
位於 行:7 字元:22
+ Get-WmiObject <<<< -computer $COMPUTER win32_logicaldisk |? {$_.drivetype -eq '3'}|%{
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand -
2012年3月6日 上午 06:41
1. Send-MailMessage InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) ,發信的伺服器請確定設定正確
2. Get-WmiObject : 無法取得 RPC 伺服器,請確定有該伺服器的管理權限
-
2012年8月30日 上午 03:40
您好:請教您
我試著將computers.txt內容寫入以下IP資訊
192.168.100.233
以上IP為公司的Exchange 2003,安裝在Server 2003上,且此Server有安裝WindowsServer2003-KB926140-v5-x86-CHT
而notify.ps1檔案內容為
$computers=(Get-Content computers.txt)
$WARING='5000'
$SMTP='192.168.100.233'
$TO='mymail@xxxx.com'以下內容接您所提供的內文
再將此兩個檔案(computers.txt和notify.ps1)放在此Server(192.168.100.233)的C槽底下
因為此台Server的C槽只剩3G多
所以原則上我使用powershell執行notify.ps1
我應該會收到信件才對...
可是都沒有收到任何通知信.........還是我哪裡做錯了???
-
2013年2月7日 上午 03:59
執行過是有用,但是出現一堆錯誤,不知道為何?
Get-WmiObject : 無法驗證 'ComputerName' 參數上的引數。引數是 Null 或空的。請提供不是 Null 或空的引數,然後再試一次該命
令。
位於 C:\capacity.ps1:12 字元:32
+ Get-WmiObject -computer <<<< $COMPUTER win32_logicaldisk |? {$_.drivetype -eq '3'}|%{
+ CategoryInfo : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand
Get-WmiObject : 無法驗證 'ComputerName' 參數上的引數。引數是 Null 或空的。請提供不是 Null 或空的引數,然後再試一次該命
令。
位於 C:\capacity.ps1:12 字元:32
+ Get-WmiObject -computer <<<< $COMPUTER win32_logicaldisk |? {$_.drivetype -eq '3'}|%{
+ CategoryInfo : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand
Send-MailMessage : 無法使用信箱。 伺服器回應為: 5.7.1 Client does not have permissions to send as this sender
位於 C:\capacity.ps1:25 字元:25
+ Send-MailMessage <<<< -to $TO -from diskmonitor@example.com -subject "$N" -body "$N" -smtp $SMTP
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
ion
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
Send-MailMessage : 無法使用信箱。 伺服器回應為: 5.7.1 Client does not have permissions to send as this sender
位於 C:\capacity.ps1:25 字元:25
+ Send-MailMessage <<<< -to $TO -from diskmonitor@example.com -subject "$N" -body "$N" -smtp $SMTP
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
ion
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
Send-MailMessage : 無法使用信箱。 伺服器回應為: 5.7.1 Client does not have permissions to send as this sender
位於 C:\capacity.ps1:25 字元:25
+ Send-MailMessage <<<< -to $TO -from diskmonitor@example.com -subject "$N" -body "$N" -smtp $SMTP
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
ion
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

