vbs脚本 - 帮助添加要在电子邮件正文中使用的标准输出

时间:2011-08-03 01:25:56

标签: windows windows-services vbscript wmi

寻找帮助设置此vbs脚本的输出并将其用作电子邮件正文中的文本。 我添加了一个发送电子邮件的子程序。我现在需要帮助的是从脚本执行中捕获标准输出并将其包含在电子邮件正文中。

代码:

'Declare Variables
Dim objWMIService, objProcess, colProcess, Status, strComputer, strService, objMessage

'Assign Arguments
strComputer = WScript.Arguments(0)
strService = WScript.Arguments(1) 
Status= false

'Check For Arguments - Quit If None Found
If Len(strService) < 1 Then
    Wscript.echo "No Arguments Entered - Exiting Script"
    WScript.Quit
End If

'Setup WMI Objects
Set objWMIService = GetObject("winmgmts:"& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colProcess = objWMIService.ExecQuery ("SELECT DisplayName, Status, State FROM Win32_Service WHERE DisplayName = '" & strService & "'")

'Send Alerts
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Alert Notice"
objMessage.From = "myalerts@myalerts.com"
objMessage.To = "test@myalerts.com"
objMessage.TextBody = "Place holder for alerts. Capture stdout here and send as part of msg"
objMessage.Send

'Check For Running Service
For Each objProcess in colProcess
    If InStr(objProcess.DisplayName,strService) > 0 And objProcess.State = "Running" Then
    Status = true
    End If
Next

If Status = true Then
    Wscript.echo "Service: " & UCase(strComputer) & " " & strService & " Running"
    'Perform Some Pass Logic Here
Else
    Wscript.echo "Service: " & UCase(strComputer) & " " & strService & " Not Running"
    'Send Alerts
End If

1 个答案:

答案 0 :(得分:1)

而不是使用Echo输出数据,只需将其设置为字符串变量或一系列字符串变量,然后在最后发送电子邮件。

'Declare Variables
Dim objWMIService, objProcess, colProcess, Status, strComputer, strService, objMessage
dim alert_body

'Assign Arguments
strComputer = WScript.Arguments(0)
strService = WScript.Arguments(1) 
Status= false

'Check For Arguments - Quit If None Found
If Len(strService) < 1 Then
    Wscript.echo "No Arguments Entered - Exiting Script"
    WScript.Quit
End If

'Setup WMI Objects
Set objWMIService = GetObject("winmgmts:"& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colProcess = objWMIService.ExecQuery ("SELECT DisplayName, Status, State FROM Win32_Service WHERE DisplayName = '" & strService & "'")

'Check For Running Service
For Each objProcess in colProcess
    If InStr(objProcess.DisplayName,strService) > 0 And objProcess.State = "Running" Then
    Status = true
    End If
Next

If Status = true Then
    Wscript.echo "Service: " & UCase(strComputer) & " " & strService & " Running"
    'Perform Some Pass Logic Here
Else
    'Wscript.echo "Service: " & UCase(strComputer) & " " & strService & " Not Running"
    alert_body = "Service: " & UCase(strComputer) & " " & strService & " Not Running"
    'Send Alerts
End If

'Send Alerts
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Alert Notice"
objMessage.From = "myalerts@myalerts.com"
objMessage.To = "test@myalerts.com"
objMessage.TextBody = alert_body
objMessage.Send

假设其他情况是您希望根据您的评论发送提醒的唯一时间。如果要向主体添加更多内容,请使用该变量并附加到该主体,或者使用多个变量并将它们全部标记到该TextBody对象上。