Solved

Vb  script to restart the services

Posted on 2016-09-28
13
73 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 33

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 33

Expert Comment

by:ste5an
ID: 41821733
Your problem sounds like you have two services, where the first service depends on the second service..
0
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 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

19 Experts available now in Live!

Get 1:1 Help Now