Solved

Vb  script to restart the services

Posted on 2016-09-28
13
89 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
[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 34

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

Expert Comment

by:ste5an
ID: 41821733
Your problem sounds like you have two services, where the first service depends on the second service..
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:vishnu adithya
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 6

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 35

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:vishnu adithya
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 35

Accepted Solution

by:
Kimputer earned 500 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:vishnu adithya
ID: 41836841
How the alert system works ? Can you explains line no 20 21 ??
0
 
LVL 35

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:vishnu adithya
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 35

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:vishnu adithya
ID: 41837151
Thanks a lot Kim for your help, i have reset the counter now.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

751 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