Urgent requirement Please.. VB Script to Check status of Services in Windows 2003 64 bit edition Server
- Hello everybody,
Am very new to VB Scripting. I needed one VB Script which has to check in our server for a list of services and if the services are stopped then it has to send an alert mail to our e-mail id.
Like it has to accept service names from one text file and check the service status and if its stopped then have to send an alert mail saying the service has stopped with server name and service name displayed..
Please give me a working script .
Thanks in advance
Madhu adavi
Madhu Adavi- Edited bymadhuadavi Friday, November 06, 2009 10:34 AMMis typed
- Edited bymadhuadavi Friday, November 13, 2009 1:40 PM
- Edited bymadhuadavi Friday, November 13, 2009 4:16 PM
Answers
A VBScript program can connect to the computer (remote or local) using WMI and query the services to see if they are stopped. The script can also attempt to start the services. The example below includes a subroutine to email messages. It assumes that CDO is installed on the computer where the script runs:
===========
Option Explicit
Dim strComputer, objWMIService, colServices, objService, strService
Dim objFSO, strFile, objFileConst ForReading = 1
' Specify text file with service names.
strFile = "c:\Scripts\Services.txt"' Open the text file for reading.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFile, ForReading)' Specify the computer.
strComputer = "MyServer"' Connect to the computer with WMI.
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,authenticationLevel=Pkt}!\\" _
& strComputer & "\root\cimv2")' Read service names from the file.
Do Until objFile.AtEndOfStream
strService = Trim(objFile.ReadLine)
' Skip blank lines.
If (strService <> "") Then
' Retrieve information on the service.
Set colServices = objWMIService.ExecQuery _
("SELECT * FROM Win32_Service WHERE Name = '" & strService & "'")
For Each objService In colServices
If (objService.State = "Stopped" And objService.StartMode = "Auto") Then
' If you want to attempt to start the service, uncomment the next line.
' objService.StartService()
' Send email message.
Call SendEmailMessage(strComputer, strService)
End If
Next
End If
Loop' Clean up.
objFile.CloseSub SendEmailMessage(ByVal strComputer, ByVal strService)
' Send email message.
' Sender and To email addresses are hard coded.
Dim objMessageSet objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Service Stopped"
objMessage.Sender = "jimsmith@mycompany.com"
objMessage.To = "jimsmith@mycompany.com"
objMessage.TextBody = "The service " & strService _
& " has stopped on computer " & strComputer
objMessage.Send
End Sub
=====
Richard Mueller
MVP ADSI- Marked As Answer bymadhuadavi Tuesday, November 17, 2009 9:13 AM
- Hello Sir,
Thanks for the Script. but i just checked, it is not sending mail..
and i tried to install CDO :Microsoft Exchange Server MAPI Client and Collaboration Data Objects 1.2.1
in our server but i got the error and it said u cant install without installing outlook 2007.
But in our server we installed Outlook 2003 licensed. we cant install 2007 bec we dont have license for that. can it be possible to modify the same code which supports 2003 exchange server objects..?
Please help me..
or Please help me in the bello task..
Here am having one VB Script which will check the services and sends email and its working but it is checking all services and it is giving o/p as some numbers for the services stopped and services started..
am attaching the code.
Plz modify it or combine the above code which u suggested me to this code to send the same mail..
=====================================================
Dim objComputer, objMessage
Dim strEmail
' If there is an error getting the status of a service it will attempt to move on to the next one
On Error Resume Next
' Email Setup
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Service Status Report"
objMessage.From = "mahesh.babu@hds.com"
objMessage.To = "datamart.support@hds.com"
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.hds.com"
'Server port (typically 25)
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
Set objComputer = GetObject("WinNT://localhost")
objComputer.Filter = Array("Service")
For Each aService In objComputer
strEmail = strEmail &chr(10) & aService.Name & "=" & aService.Status
Next
objMessage.TextBody = strEmail
objMessage.Configuration.Fields.Update
objMessage.Send
======================================================
Madhu Adavi- Marked As Answer bymadhuadavi Tuesday, November 17, 2009 9:12 AM
- Sending email messages from a script is always problematic. It depends on what is installed on the computer. What works on one computer won't on another.
It is probably possible to use the "Outlook.Application" object to send messages. I use it to read messages in scripts, but cannot figure out by trial and error how to send. Perhaps you should post a question in an Outlook programming newsgroup. In fact, that's how I learned how to read messages in an Outlook Inbox.
Otherwise, you can have the script echo results to the command console. In place of:
Call SendEmailMessage(strComputer, strService)
use:
Wscript.Echo strComputer & "," & strService
And. remember that the script I posted can be run on any client that is joined to the domain.
Richard Mueller
MVP ADSI- Marked As Answer bymadhuadavi Tuesday, November 17, 2009 9:12 AM
- Hello Richard Mueller,
As you told i added line Wscript.Echo strComputer & "," & strService . it is displaying the computer name and the service name. but mail is not coming ..
and i installed ExchangeMapiCdo.MSI in our server.. but still mail is not coming..
it is displaying the computer name and service name if i put that echo line. and if i comment it and if i put the line Call SendEmailMessage(strComputer, strService) still mail is not coming..
Please help me.. and usually the vb scripts which sends mail requires exchange server id and port..
if that is the condition please add that exchange server id also.. as i see in the code it is asking only from and to id.. Please help me..
Madhu Adavi- Marked As Answer bymadhuadavi Tuesday, November 17, 2009 9:12 AM
- Hello Richard,
Thanks a lot..
Finally i got the solution and its working fine now..
I modified some of the things..
and one more thing Please..
what statement should we need to include if the mail should come with high importance and in HTML Format..?
Presently its coming in plane text format..
Thanks in Advance.
========================================= Option Explicit Dim strComputer, objWMIService, colServices, objService, strService Dim objFSO, strFile, objFile Const ForReading = 1 ' Specify text file with service names. strFile = "c:\Services.txt" ' Open the text file for reading. Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFile, ForReading) ' Specify the computer. strComputer = "usinddm02.corp.hds.com" ' Connect to the computer with WMI. Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate,authenticationLevel=Pkt}!\\" _ & strComputer & "\root\cimv2") ' Read service names from the file. Do Until objFile.AtEndOfStream strService = Trim(objFile.ReadLine) ' Skip blank lines. If (strService <> "") Then ' Retrieve information on the service. Set colServices = objWMIService.ExecQuery _ ("SELECT * FROM Win32_Service WHERE Name = '" & strService & "'") For Each objService In colServices If (objService.State = "Stopped" And objService.StartMode = "Auto") Then ' If you want to attempt to start the service, uncomment the next line. ' objService.StartService() ' Send email message. Call SendEmailMessage(strComputer, strService) 'Wscript.Echo strComputer & "," & strService End If Next End If Loop ' Clean up. objFile.Close Sub SendEmailMessage(ByVal strComputer, ByVal strService) ' Send email message. ' Sender and To email addresses are hard coded. Dim objMessage Set objMessage = CreateObject("CDO.Message") objMessage.Subject = "Service Stopped" objMessage.Sender = "suresh.babu.ps@hds.com" objMessage.To = "suresh.babu.ps@hds.com" objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'Name or IP of Remote SMTP Server objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.hds.com" 'Server port (typically 25) objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 objMessage.Configuration.Fields.Update objMessage.TextBody = "The service " & strService _ & " has stopped on computer " & strComputer objMessage.Send End Sub =============================================
Madhu Adavi- Marked As Answer bymadhuadavi Tuesday, November 17, 2009 9:12 AM
You can use HTMLBody in place of TextBody. I cannot find out to send the message with high importance. Maybe someone else knows how.
Richard Mueller
MVP ADSI- Marked As Answer bymadhuadavi Tuesday, November 17, 2009 9:12 AM
- Try
objMessage.Fields.Item("urn:schemas:mailheader:importance").Value = "High"
objMessage.Fields.Update()
Bill- Marked As Answer bymadhuadavi Tuesday, November 17, 2009 9:12 AM
- Hello everybody,
A bunch of thanks for your support.. Its Working fine now..
Thanks a lot..
Madhu Adavi- Marked As Answer bymadhuadavi Wednesday, November 11, 2009 12:08 PM
- Unmarked As Answer bymadhuadavi Wednesday, November 11, 2009 12:08 PM
- Marked As Answer bymadhuadavi Tuesday, November 17, 2009 9:12 AM
- You can add a variable with the names of the services that are stopped. You would add stopped service names to the variable in the loop, then only send the email message if the variable is not blank. For example:
Option Explicit Dim strComputer, objWMIService, colServices, objService, strService Dim objFSO, strFile, objFile, strStopped Const ForReading = 1 ' Specify text file with service names. strFile = "c:\Services.txt" ' Open the text file for reading. Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFile, ForReading) ' Specify the computer. strComputer = "usinddm02.corp.hds.com" ' Connect to the computer with WMI. Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate,authenticationLevel=Pkt}!\\" _ & strComputer & "\root\cimv2") ' Read service names from the file. strStopped = "" Do Until objFile.AtEndOfStream strService = Trim(objFile.ReadLine) ' Skip blank lines. If (strService <> "") Then ' Retrieve information on the service. Set colServices = objWMIService.ExecQuery _ ("SELECT * FROM Win32_Service WHERE Name = '" & strService & "'") For Each objService In colServices If (objService.State = "Stopped" And objService.StartMode = "Auto") Then ' If you want to attempt to start the service, uncomment the next line. ' objService.StartService() ' Flag service as stopped. If (strStopped = "") Then strStopped = strService Else strStopped = strStopped & ";" & strService End If End If Next End If Loop ' Clean up. objFile.Close If (strStopped <> "") Then Call SendEmailMessage(strComputer, strStopped) End If
MVP ADSI- Marked As Answer bymadhuadavi Tuesday, November 17, 2009 9:12 AM
- Hello MVP ADSI,
Thanks a lot. Its working fine..
Regds
Madhu
Madhu Adavi- Marked As Answer bymadhuadavi Tuesday, November 17, 2009 9:12 AM
All Replies
A VBScript program can connect to the computer (remote or local) using WMI and query the services to see if they are stopped. The script can also attempt to start the services. The example below includes a subroutine to email messages. It assumes that CDO is installed on the computer where the script runs:
===========
Option Explicit
Dim strComputer, objWMIService, colServices, objService, strService
Dim objFSO, strFile, objFileConst ForReading = 1
' Specify text file with service names.
strFile = "c:\Scripts\Services.txt"' Open the text file for reading.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFile, ForReading)' Specify the computer.
strComputer = "MyServer"' Connect to the computer with WMI.
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,authenticationLevel=Pkt}!\\" _
& strComputer & "\root\cimv2")' Read service names from the file.
Do Until objFile.AtEndOfStream
strService = Trim(objFile.ReadLine)
' Skip blank lines.
If (strService <> "") Then
' Retrieve information on the service.
Set colServices = objWMIService.ExecQuery _
("SELECT * FROM Win32_Service WHERE Name = '" & strService & "'")
For Each objService In colServices
If (objService.State = "Stopped" And objService.StartMode = "Auto") Then
' If you want to attempt to start the service, uncomment the next line.
' objService.StartService()
' Send email message.
Call SendEmailMessage(strComputer, strService)
End If
Next
End If
Loop' Clean up.
objFile.CloseSub SendEmailMessage(ByVal strComputer, ByVal strService)
' Send email message.
' Sender and To email addresses are hard coded.
Dim objMessageSet objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Service Stopped"
objMessage.Sender = "jimsmith@mycompany.com"
objMessage.To = "jimsmith@mycompany.com"
objMessage.TextBody = "The service " & strService _
& " has stopped on computer " & strComputer
objMessage.Send
End Sub
=====
Richard Mueller
MVP ADSI- Marked As Answer bymadhuadavi Tuesday, November 17, 2009 9:13 AM
- Hello Sir,
Thanks for the Script. but i just checked, it is not sending mail..
and i tried to install CDO :Microsoft Exchange Server MAPI Client and Collaboration Data Objects 1.2.1
in our server but i got the error and it said u cant install without installing outlook 2007.
But in our server we installed Outlook 2003 licensed. we cant install 2007 bec we dont have license for that. can it be possible to modify the same code which supports 2003 exchange server objects..?
Please help me..
or Please help me in the bello task..
Here am having one VB Script which will check the services and sends email and its working but it is checking all services and it is giving o/p as some numbers for the services stopped and services started..
am attaching the code.
Plz modify it or combine the above code which u suggested me to this code to send the same mail..
=====================================================
Dim objComputer, objMessage
Dim strEmail
' If there is an error getting the status of a service it will attempt to move on to the next one
On Error Resume Next
' Email Setup
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Service Status Report"
objMessage.From = "mahesh.babu@hds.com"
objMessage.To = "datamart.support@hds.com"
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.hds.com"
'Server port (typically 25)
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
Set objComputer = GetObject("WinNT://localhost")
objComputer.Filter = Array("Service")
For Each aService In objComputer
strEmail = strEmail &chr(10) & aService.Name & "=" & aService.Status
Next
objMessage.TextBody = strEmail
objMessage.Configuration.Fields.Update
objMessage.Send
======================================================
Madhu Adavi- Marked As Answer bymadhuadavi Tuesday, November 17, 2009 9:12 AM
- Sending email messages from a script is always problematic. It depends on what is installed on the computer. What works on one computer won't on another.
It is probably possible to use the "Outlook.Application" object to send messages. I use it to read messages in scripts, but cannot figure out by trial and error how to send. Perhaps you should post a question in an Outlook programming newsgroup. In fact, that's how I learned how to read messages in an Outlook Inbox.
Otherwise, you can have the script echo results to the command console. In place of:
Call SendEmailMessage(strComputer, strService)
use:
Wscript.Echo strComputer & "," & strService
And. remember that the script I posted can be run on any client that is joined to the domain.
Richard Mueller
MVP ADSI- Marked As Answer bymadhuadavi Tuesday, November 17, 2009 9:12 AM
- Hello Richard Mueller,
As you told i added line Wscript.Echo strComputer & "," & strService . it is displaying the computer name and the service name. but mail is not coming ..
and i installed ExchangeMapiCdo.MSI in our server.. but still mail is not coming..
it is displaying the computer name and service name if i put that echo line. and if i comment it and if i put the line Call SendEmailMessage(strComputer, strService) still mail is not coming..
Please help me.. and usually the vb scripts which sends mail requires exchange server id and port..
if that is the condition please add that exchange server id also.. as i see in the code it is asking only from and to id.. Please help me..
Madhu Adavi- Marked As Answer bymadhuadavi Tuesday, November 17, 2009 9:12 AM
- Hello Richard,
Thanks a lot..
Finally i got the solution and its working fine now..
I modified some of the things..
and one more thing Please..
what statement should we need to include if the mail should come with high importance and in HTML Format..?
Presently its coming in plane text format..
Thanks in Advance.
========================================= Option Explicit Dim strComputer, objWMIService, colServices, objService, strService Dim objFSO, strFile, objFile Const ForReading = 1 ' Specify text file with service names. strFile = "c:\Services.txt" ' Open the text file for reading. Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFile, ForReading) ' Specify the computer. strComputer = "usinddm02.corp.hds.com" ' Connect to the computer with WMI. Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate,authenticationLevel=Pkt}!\\" _ & strComputer & "\root\cimv2") ' Read service names from the file. Do Until objFile.AtEndOfStream strService = Trim(objFile.ReadLine) ' Skip blank lines. If (strService <> "") Then ' Retrieve information on the service. Set colServices = objWMIService.ExecQuery _ ("SELECT * FROM Win32_Service WHERE Name = '" & strService & "'") For Each objService In colServices If (objService.State = "Stopped" And objService.StartMode = "Auto") Then ' If you want to attempt to start the service, uncomment the next line. ' objService.StartService() ' Send email message. Call SendEmailMessage(strComputer, strService) 'Wscript.Echo strComputer & "," & strService End If Next End If Loop ' Clean up. objFile.Close Sub SendEmailMessage(ByVal strComputer, ByVal strService) ' Send email message. ' Sender and To email addresses are hard coded. Dim objMessage Set objMessage = CreateObject("CDO.Message") objMessage.Subject = "Service Stopped" objMessage.Sender = "suresh.babu.ps@hds.com" objMessage.To = "suresh.babu.ps@hds.com" objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'Name or IP of Remote SMTP Server objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.hds.com" 'Server port (typically 25) objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 objMessage.Configuration.Fields.Update objMessage.TextBody = "The service " & strService _ & " has stopped on computer " & strComputer objMessage.Send End Sub =============================================
Madhu Adavi- Marked As Answer bymadhuadavi Tuesday, November 17, 2009 9:12 AM
You can use HTMLBody in place of TextBody. I cannot find out to send the message with high importance. Maybe someone else knows how.
Richard Mueller
MVP ADSI- Marked As Answer bymadhuadavi Tuesday, November 17, 2009 9:12 AM
- Try
objMessage.Fields.Item("urn:schemas:mailheader:importance").Value = "High"
objMessage.Fields.Update()
Bill- Marked As Answer bymadhuadavi Tuesday, November 17, 2009 9:12 AM
- Hello everybody,
A bunch of thanks for your support.. Its Working fine now..
Thanks a lot..
Madhu Adavi- Marked As Answer bymadhuadavi Wednesday, November 11, 2009 12:08 PM
- Unmarked As Answer bymadhuadavi Wednesday, November 11, 2009 12:08 PM
- Marked As Answer bymadhuadavi Tuesday, November 17, 2009 9:12 AM
- Hello Everybody,
one more modification with the previous code please..
Suppose if i give 4 service names in the text file (Say among 4 services 3 services are stopped and 1 service is running)
as input and if i run the script in my server it is sending 3 mails with individual mail containing a service name and computer name.
wanted to modify : If i don't want to receive 3 mails for each service name and i wanted to receive only single mail with all three service name how i can modify the code..? Please suggest me..
Thanks in advance.
Madhu Adavi - Hello Please Some one suggest me the solution.. Its bit urgent ..
Thanks in advance.
Madhu
Madhu Adavi - You can add a variable with the names of the services that are stopped. You would add stopped service names to the variable in the loop, then only send the email message if the variable is not blank. For example:
Option Explicit Dim strComputer, objWMIService, colServices, objService, strService Dim objFSO, strFile, objFile, strStopped Const ForReading = 1 ' Specify text file with service names. strFile = "c:\Services.txt" ' Open the text file for reading. Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFile, ForReading) ' Specify the computer. strComputer = "usinddm02.corp.hds.com" ' Connect to the computer with WMI. Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate,authenticationLevel=Pkt}!\\" _ & strComputer & "\root\cimv2") ' Read service names from the file. strStopped = "" Do Until objFile.AtEndOfStream strService = Trim(objFile.ReadLine) ' Skip blank lines. If (strService <> "") Then ' Retrieve information on the service. Set colServices = objWMIService.ExecQuery _ ("SELECT * FROM Win32_Service WHERE Name = '" & strService & "'") For Each objService In colServices If (objService.State = "Stopped" And objService.StartMode = "Auto") Then ' If you want to attempt to start the service, uncomment the next line. ' objService.StartService() ' Flag service as stopped. If (strStopped = "") Then strStopped = strService Else strStopped = strStopped & ";" & strService End If End If Next End If Loop ' Clean up. objFile.Close If (strStopped <> "") Then Call SendEmailMessage(strComputer, strStopped) End If
MVP ADSI- Marked As Answer bymadhuadavi Tuesday, November 17, 2009 9:12 AM
- Hello MVP ADSI,
Thanks a lot. Its working fine..
Regds
Madhu
Madhu Adavi- Marked As Answer bymadhuadavi Tuesday, November 17, 2009 9:12 AM

