Windows Server TechCenter > Windows Server Forums > Management > VBScript to check for service stoppage
Ask a questionAsk a question
 

AnswerVBScript to check for service stoppage

  • Friday, October 30, 2009 4:21 PMBadpig Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    I am working on a VBscript to check for service stops in windows server 2000 and 2003.  When I stop a service to test the script, it will run and send me an email but doesnt show the service that has stopped.  What am I doing wrong?

    Here is the script

    on error resume next
    Set iFSO = CreateObject("Scripting.FilesyStemObject")
    Set oFSO = CreateObject("Scripting.FilesyStemObject")
     
    'Note: change the email ID to your email ID
    NotificationEmail=""
    SMTPServer = ""
     
    InputFile="C:\Documents and Settings\jh31967\My Documents\Noitfy Script\Servicelist.txt"
    Outputfile="C:\Documents and Settings\jh31967\My Documents\Noitfy Script\ServiceStatus.txt"
     
    Set ifile = iFSO.OpenTextFile(inputfile)
    Set ofile = ofso.createTextFile(OutputFile, True)
    ofile.writeline "Check Server Service Status Started"
    ofile.writeline Now()
    ofile.writeline ""
     
    Do until ifile.AtEndOfLine
    	servicelist= ifile.ReadLine
    	strcomputer=left(servicelist,instr(servicelist,",")-1)
    	Service =right(servicelist,len(servicelist)-instr(servicelist,","))
     
    	Set objWMIService =nothing
    	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
     
    	if err.number<>0 then
    		ofile.writeline "ServerName: " & strcomputer
    		ofile.writeline "ServiceName: " & Service
    		ofile.writeline "Error: " & err.number
    		ofile.writeline "Error: " & err.description
    		ofile.writeline ""	
    	else
     
    		Set colItems = nothing
    		query=""
    		query = "Select * from Win32_Service where name = '" & Service & "'"
     
    		Set colItems = objWMIService.ExecQuery(query)
    			if err.number<>0 then
    				ofile.writeline "ServerName: " & strcomputer
    				ofile.writeline "ServiceName: " & Service
    				ofile.writeline "Error: " & err.number
    				ofile.writeline "Error: " & err.description
    				ofile.writeline ""
    			else
    				
    				For Each objItem in colItems
    					if objItem.State <> "Running" then
    					ofile.writeline "ServerName: " & strcomputer
    					ofile.writeline "ServiceName: " & Service
    					ofile.writeline "Status: " & objItem.Status
    					ofile.writeline "State: " & objItem.State
    					ofile.writeline ""
    					end if
    				next
    			end if
    	end if
     
    Loop
     
    ofile.writeline "Check Server Service Status Completed"
    ofile.writeline Now()
     
    ofile.Close
    ofile = Nothing
     
    Set objMessage = CreateObject("CDO.Message") 
    	objMessage.Subject = "SQL Server Service Status Report" 
    	objMessage.From = NoitificationEmail
    	objMessage.To = NotificationEmail
    	objMessage.TextBody = "SQL Server Service Status Report Attached"
    	objMessage.AddAttachment(Outputfile)
     
    	'The line below shows how to send a webpage from a remote site 
    		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") = SMTPServer
    	'Server port (typically 25) 
    		objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
     
    	objMessage.Configuration.Fields.Update 
    	objMessage.Send
    

Answers

  • Sunday, November 01, 2009 3:10 PMGunner999 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    You have several prgramming errors that the use of  on error resume next is covering up.

    Read the Simplifying Error Handling and Troubleshooting section of this this article on how to best use on error resume next
    This articles will also help you write more procedures and functions that can be tested individually.
    http://networkadminkb.com/Shared%20Documents/Programming%20and%20Scripting%20Techniques%20for%20Windows%20Administrators.aspx

    Most likely you are using the "Display Name" as the service name, when you really need the Registry Key Name of the service.  No error is returned if the service name is incorrect.

    Using Regedit.exe search the following Registry Key for each Service Display Name, and use the registry key name in your ServiceList.txt file.

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services

    Example: Window Time is really w32Time

    Sample Output...my modified version showing how quering "Windows Time" fails without error but "w32time" works.

    Check Server Service Status Started
    11/1/2009 9:02:37 AM

    .,Windows Time
    Select * from Win32_Service where name = 'Windows Time'


    .,w32time
    Select * from Win32_Service where name = 'w32time'
    Stopped
    ServerName: .
    ServiceName: w32time
    Status: OK
    State: Stopped

    Check Server Service Status Completed
    11/1/2009 9:02:38 AM

  • Monday, November 02, 2009 1:14 AMJoson ZhouMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi,

     

    If you need further assistance, please post to scripting forum. The support professionals there are more experienced in scripting and should be able to provide you with more suggestions.

     

    The Official Scripting Guys Forum!

    http://social.technet.microsoft.com/Forums/en-US/ITCG/threads

     

    Thanks.


    This posting is provided "AS IS" with no warranties, and confers no rights.

All Replies

  • Saturday, October 31, 2009 7:09 PMDave PatrickMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I'd start by checking the contents of the two text files. Also try commenting out the on error resume next statement to see what happens.






    Regards, Dave Patrick .... Microsoft Certified Professional Microsoft MVP [Windows]
  • Sunday, November 01, 2009 3:10 PMGunner999 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    You have several prgramming errors that the use of  on error resume next is covering up.

    Read the Simplifying Error Handling and Troubleshooting section of this this article on how to best use on error resume next
    This articles will also help you write more procedures and functions that can be tested individually.
    http://networkadminkb.com/Shared%20Documents/Programming%20and%20Scripting%20Techniques%20for%20Windows%20Administrators.aspx

    Most likely you are using the "Display Name" as the service name, when you really need the Registry Key Name of the service.  No error is returned if the service name is incorrect.

    Using Regedit.exe search the following Registry Key for each Service Display Name, and use the registry key name in your ServiceList.txt file.

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services

    Example: Window Time is really w32Time

    Sample Output...my modified version showing how quering "Windows Time" fails without error but "w32time" works.

    Check Server Service Status Started
    11/1/2009 9:02:37 AM

    .,Windows Time
    Select * from Win32_Service where name = 'Windows Time'


    .,w32time
    Select * from Win32_Service where name = 'w32time'
    Stopped
    ServerName: .
    ServiceName: w32time
    Status: OK
    State: Stopped

    Check Server Service Status Completed
    11/1/2009 9:02:38 AM

  • Monday, November 02, 2009 1:14 AMJoson ZhouMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi,

     

    If you need further assistance, please post to scripting forum. The support professionals there are more experienced in scripting and should be able to provide you with more suggestions.

     

    The Official Scripting Guys Forum!

    http://social.technet.microsoft.com/Forums/en-US/ITCG/threads

     

    Thanks.


    This posting is provided "AS IS" with no warranties, and confers no rights.