Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Vb  script to restart the services

Posted on 2016-09-28
13
Medium Priority
?
111 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:V Thrusher
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 35

Expert Comment

by:ste5an
ID: 41819475
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:V Thrusher
ID: 41821605
Thanks Stefan for your response but how do i make the changes  ? what you mean by dependent.
0
 
LVL 35

Expert Comment

by:ste5an
ID: 41821733
Your problem sounds like you have two services, where the first service depends on the second service..
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:V Thrusher
ID: 41821754
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 7

Expert Comment

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

Open in new window

0
 
LVL 36

Expert Comment

by:Kimputer
ID: 41831933
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
 

Author Comment

by:V Thrusher
ID: 41834020
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 36

Accepted Solution

by:
Kimputer earned 2000 total points
ID: 41836821
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:V Thrusher
ID: 41836841
How the alert system works ? Can you explains line no 20 21 ??
0
 
LVL 36

Expert Comment

by:Kimputer
ID: 41836848
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:V Thrusher
ID: 41836945
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 36

Expert Comment

by:Kimputer
ID: 41837082
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:V Thrusher
ID: 41837151
Thanks a lot Kim for your help, i have reset the counter now.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

705 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