寻找帮助来设置此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 = "[email protected]"
objMessage.To = "[email protected]"
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

最佳答案

无需使用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 = "[email protected]"
objMessage.To = "[email protected]"
objMessage.TextBody = alert_body
objMessage.Send

假设其他情况是您唯一想根据评论发送警报的情况。如果要向正文中添加更多内容,请使用该变量并将其追加到正文中,或者使用多个变量并将它们全部标记到该TextBody对象上。

关于windows - vbs脚本-帮助添加要在电子邮件正文中使用的标准输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6920853/

10-11 17:21