Solved

Vb  script to restart the services

Posted on 2016-09-28
13
64 Views
Last Modified: 2016-10-10
I have wrote the script to stop application services. first it will stop SIA services and wait for 10 seconds & after that second services is stopped. Now i want to include a condition to check whether first SIA services is stopped or not and if stopped then only it can proceed to stopping the second services...can someone help me pls to modify this code

Option Explicit
Dim objWMIService, objService
Dim stpServicesia,stpServicetom,strComputer,waitTime
stpServicesia="SIA"
stpServicetom="BOE120Tomcat7"
strComputer = "samplesystemtname"
waitTime=10000
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
For Each objService In objWMIService.ExecQuery("Select * from Win32_Service Where Name = '"_
&stpServicesia&"'")
objService.StopService()
Next
WScript.Sleep 10000
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
For Each objService In objWMIService.ExecQuery("Select * from Win32_Service Where Name = '"_
&stpServicetom&"'")
objService.StopService()
Next
WScript.Quit
0
Comment
Question by:vishnu adithya
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
Just a comment: Why not making those services dependent?

Like using the sc config:

sc config [service name] depend= <Dependencies>

Open in new window

0
 

Author Comment

by:vishnu adithya
Comment Utility
Thanks Stefan for your response but how do i make the changes  ? what you mean by dependent.
0
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
Your problem sounds like you have two services, where the first service depends on the second service..
0
 

Author Comment

by:vishnu adithya
Comment Utility
yes, we need to stop the first services and once done then only  we can stop the second services, how can i make my code to check it. since i'm new to VB and as you can see my script is simple one. can please change the code to your idea "sc config [service name] depend= <Dependencies>"
0
 
LVL 6

Expert Comment

by:huacat
Comment Utility
I'd like to use bat to do it:
sc stop "service1"
if %errorlevel%==0 sc stop "service2"

Open in new window

0
 
LVL 35

Expert Comment

by:Kimputer
Comment Utility
Option Explicit
Dim objWMIService, objService
Dim stpServicesia,stpServicetom,strComputer,waitTime
stpServicesia="SIA"
stpServicetom="BOE120Tomcat7"
strComputer = "samplesystemtname"
waitTime=10000
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
For Each objService In objWMIService.ExecQuery("Select * from Win32_Service Where Name = '"_
&stpServicesia&"'")
	objService.StopService()
	While Not(objService.State = "Stopped")
		WScript.Sleep 10000
	Wend
	Set objWMIService = GetObject("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	For Each objService In objWMIService.ExecQuery("Select * from Win32_Service Where Name = '"_
	&stpServicetom&"'")
	objService.StopService()
	Next
Next

WScript.Quit

Open in new window


Obviously, if your service is buggy and WON'T stop (there are services that behave like that!), your script will also loop indefinitly, unless you want me to code something extra to jump out or message you.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:vishnu adithya
Comment Utility
Thanks Kim please do it and it will be really helpfull. Because i need to start and stop the services in particular order and so have to check whether one process stopped or not before stopping the second services. from below you can see i'm using same script to start services just by changing services function from stop to start

for sample
Option Explicit
Dim objWMIService, objService
Dim strServicesia,strServicetom,strComputer,waitTime
strServicesia="BOE120SIACACABI"
strServicetom="BOE120Tomcat7"
strComputer = "PWAUSWSDRP03"
waitTime=10000
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
For Each objService In objWMIService.ExecQuery("Select * from Win32_Service Where Name = '"_
&strServicesia&"'")
objService.StartService()
Next
WScript.Sleep 10000
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
For Each objService In objWMIService.ExecQuery("Select * from Win32_Service Where Name = '"_
&strServicetom&"'")
objService.StartService()
Next
WScript.Quit

Open in new window

0
 
LVL 35

Accepted Solution

by:
Kimputer earned 500 total points
Comment Utility
When stuck, alert, and quit script:

Option Explicit
Dim objWMIService, objService
Dim stpServicesia,stpServicetom,strComputer,waitTime
stpServicesia="SIA"
stpServicetom="BOE120Tomcat7"
strComputer = "samplesystemtname"
waitTime=10000
count = 0
maxcount = 100
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
For Each objService In objWMIService.ExecQuery("Select * from Win32_Service Where Name = '"_
&stpServicesia&"'")
	objService.StopService()
	While Not(objService.State = "Stopped")
		WScript.Sleep 10000
		count = count + 1
		if count > maxcount then
			'customize your own alert system
			Set WshShell = WScript.CreateObject ("WScript.Shell")
			WshShell.Run "C:\Users\kimputer\scripts\telegrambotsend.exe stopping service " & stpServicesia & " stuck!",0,True
			Set WshShell = Nothing
			WScript.Quit
		end if
	Wend
	Set objWMIService = GetObject("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	For Each objService In objWMIService.ExecQuery("Select * from Win32_Service Where Name = '"_
	&stpServicetom&"'")
	objService.StopService()
	Next
Next

WScript.Quit

Open in new window

0
 

Author Comment

by:vishnu adithya
Comment Utility
How the alert system works ? Can you explains line no 20 21 ??
0
 
LVL 35

Expert Comment

by:Kimputer
Comment Utility
Everyone has their own alert systems. I have mine based on Telegram, written my own small Windows app.
Yours should be what you prefer, email or whatever.
0
 

Author Comment

by:vishnu adithya
Comment Utility
i modified the code a bit, so kindly do check and let me know your thoughts
Option Explicit
Dim objWMIService, objService
Dim stpServicesia,stpServicetom,strComputer,waitTime,count,maxcount
stpServicesia="SIA"
stpServicetom="BOE120Tomcat7"
strComputer = "samplesystemtname"
waitTime=10000
count = 0
maxcount = 100
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
For Each objService In objWMIService.ExecQuery("Select * from Win32_Service Where Name = '"_
&stpServicesia&"'")
	objService.StopService()
	While Not(objService.State = "Stopped")
		WScript.Sleep 10000
		count = count + 1
		if count > maxcount then
			Set objEmail = CreateObject("CDO.Message")
			objEmail.From = "@.com"
			objEmail.To = "@.com"
			objEmail.Subject = "Boxi Serviecs stop activity"
			objEmail.Textbody = "Hi Team"  & vbCRLF
			objEmail.Textbody = objEmail.Textbody & "" & vbCRLF
			objEmail.Textbody = objEmail.Textbody & "'stopping service " & stpServicesia & " stuck! Kindly do look into it" & vbCRLF
			objEmail.Textbody = objEmail.Textbody & "" & vbCRLF
			objEmail.Textbody = objEmail.Textbody & "" & vbCRLF
			objEmail.Textbody = objEmail.Textbody & "Thanks," & vbCRLF
			objEmail.Textbody = objEmail.Textbody & "team" & vbCRLF
			objEmail.Configuration.Fields.Item _
			("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
			objEmail.Configuration.Fields.Item _
			("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
			"relay.astrazeneca.net"
			objEmail.Configuration.Fields.Item _
			("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
			objEmail.Configuration.Fields.Update
			objEmail.Send
			WScript.Quit
		end if
	Wend
	
Next
Set objWMIService = GetObject("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	For Each objService In objWMIService.ExecQuery("Select * from Win32_Service Where Name = '"_
	&stpServicetom&"'")
	objService.StopService()
		While Not(objService.State = "Stopped")
		WScript.Sleep 10000
		count = count + 1
		if count > maxcount then
			Set objEmail = CreateObject("CDO.Message")
			objEmail.From = "@.com"
			objEmail.To = "@.com"
			objEmail.Subject = "Boxi Serviecs stop activity"
			objEmail.Textbody = "Hi Team"  & vbCRLF
			objEmail.Textbody = objEmail.Textbody & "" & vbCRLF
			objEmail.Textbody = objEmail.Textbody & "'stopping service " & stpServicetom & " stuck! Kindly do look into it" & vbCRLF
			objEmail.Textbody = objEmail.Textbody & "" & vbCRLF
			objEmail.Textbody = objEmail.Textbody & "" & vbCRLF
			objEmail.Textbody = objEmail.Textbody & "Thanks," & vbCRLF
			objEmail.Textbody = objEmail.Textbody & "team" & vbCRLF
			objEmail.Configuration.Fields.Item _
			("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
			objEmail.Configuration.Fields.Item _
			("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
			"relay.astrazeneca.net"
			objEmail.Configuration.Fields.Item _
			("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
			objEmail.Configuration.Fields.Update
			objEmail.Send
			WScript.Quit
		end if
	Wend

	Next
WScript.Quit

Open in new window

0
 
LVL 35

Expert Comment

by:Kimputer
Comment Utility
Sure, if you tested the email part seperately, and it works, this script should also work.
Between the two blocks of code though it needs a counter reset

count = 0

Open in new window

0
 

Author Comment

by:vishnu adithya
Comment Utility
Thanks a lot Kim for your help, i have reset the counter now.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now