开发者

vbs script - help adding standard output to be used in body of email

Looking for help setting the output from this vbs script and using it as a text in body of the email. I added a subroutine for sending email. What I need help with now, is capturing the standard output from the script execution and including it in the body of the email.

code:

'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


Instead of Using Echo to output data, just set it to a string variable or a series of string variables and then at the end send the emails.

'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

Assuming that else case is the only time you want to send an alert based on your comments. If you want to add more to the body, either use that variable and append to it, or use multiple variables and just tag them all onto that TextBody object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜