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

寻找帮助设置从这个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 

而不是使用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对象。