Created
May 28, 2010 02:09
-
-
Save okhiroyuki/416654 to your computer and use it in GitHub Desktop.
Windows PC の電源供給状況をメール監視
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dim b_bool | |
Dim oMsg | |
Dim filename | |
Dim strNum | |
Dim message | |
strComputer = "." | |
filename = "status.txt" | |
Set objFSO = CreateObject("Scripting.FileSystemObject") | |
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\wmi") | |
Set colItems = objWMIService.ExecQuery("Select * From BatteryStatus Where Voltage > 0") | |
strNum = get_num(filename) | |
For Each objItem in colItems | |
b_bool = objItem.PowerOnline | |
Next | |
'battery status: online = -1 , offline = 0 | |
If strNum = 0 Then | |
If b_bool = 0 Then | |
message = "電源停止" | |
Call send_mail(message) | |
Call rewrite_sub(filename,1) | |
End If | |
Else | |
If b_bool = -1 Then | |
message = "電源回復" | |
Call send_mail(message) | |
Call rewrite_sub(filename,0) | |
End If | |
End If | |
'メール送信 | |
'携帯にメールを送る場合は,送信ドメイン認証に注意! | |
Sub send_mail(message) | |
Dim oMsg | |
Set oMsg = CreateObject("CDO.Message") | |
oMsg.From = "****@****.co.jp" | |
oMsg.To = "****@****.co.jp" | |
oMsg.Subject = "電源状況" | |
oMsg.TextBody = message | |
oMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 | |
oMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "fujiengi.co.jp" | |
oMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 | |
oMsg.Configuration.Fields.Update | |
oMsg.Send | |
End Sub | |
'更新状況の確認 | |
Function get_num(filename) | |
Dim objFile | |
Set objFile = objFSO.OpenTextFile(filename, 1, False) | |
If Err.Number > 0 Then | |
WScript.Echo "Open Error" | |
Else | |
Do Until objFile.AtEndOfStream | |
get_num = objFile.ReadLine | |
Loop | |
End If | |
objFile.Close | |
Set objFile = Nothing | |
End Function | |
'テキストの上書き | |
Sub rewrite_sub(filename,strNum) | |
Dim objFile | |
Set objFile = objFSO.OpenTextFile(filename, 2, False) | |
If Err.Number > 0 Then | |
WScript.Echo "Open Error" | |
Else | |
objFile.WriteLine strNum | |
End If | |
objFile.Close | |
Set objFile = Nothing | |
End Sub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment