Solved

Vb  script to restart the services

Posted on 2016-09-28
13
82 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

789 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