?
Solved

Scripting Challenge, Automatic Server rRstart and Checking Services have started

Posted on 2011-10-04
2
Medium Priority
?
248 Views
Last Modified: 2012-08-14

I have 6 x Windows 2008 servers (lets call them s1, s2, s3, s4 s5, s6). I need to reboot all servers automatically using a script and ensure that specific application services are running .(sometimes these don't start correctly even though they are set to automatically start so if they havent srarted I need to start them).


So the script should look like:

stop all services accross S1, S2, S3, S4, S5, S6.
reboot S1
ensure all application services are started on S1 in the correct order: service1, service2, service3,


Reboot S4
ensure all application services are started on S4 in the correct order: service1, service2, service3,


Reboot S2, S3, S5, S6 in any order
ensure single application service service3 has been started on these servers.

It's a bit challenging but should be easy for the scripting gurus amongst you,

Cheers!
0
Comment
Question by:vourneenward
  • 2
2 Comments
 
LVL 5

Expert Comment

by:rov17
ID: 36914466
Try the script below, you will need to put the servers name in a txt (no empty lines), the 4 servers has to have the Firewall disabled

In the services section, you will need to replace the Computer1, computer2 with you first and second servers names and also replace  "wuauserv" with the Services 1……..

Hope that helps

Cheers
Dim oFSO, oTS, sClient, oWindows, oLocator, oConnection, oSys
Dim sUser, sPassword
 
'open list of client names
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTS = oFSO.OpenTextFile("C:\clients.txt")
 
Do Until oTS.AtEndOfStream
	'get next client name
	sClient = oTS.ReadLine
 
	'get WMI locator
	Set oLocator = CreateObject("WbemScripting.SWbemLocator")
 
	'Connect to remote WMI
	Set oConnection = oLocator.ConnectServer(sClient, "root\cimv2", sUser, sPassword)
 
	'issue shutdown to OS
	
	6 = force rebooot
		Set oWindows = oConnection.ExecQuery("Select " & "Name From Win32_OperatingSystem")
 
	For Each oSys In oWindows
		oSys.Win32ShutDown(5)
	Next
Loop
 
'close the text file
oTS.Close


“Services section 

OPTION EXPLICIT

DIM strComputer,strServiceName
strComputer = "Computer1" 
strServiceName = "wuauserv" ' Services 1

IF isServiceRunning(strComputer,strServiceName) THEN
	wscript.echo "The '" & strServiceName & "' service is running on '" & strcomputer & "'"
ELSE
	wscript.echo "The '" & strServiceName & "' service is NOT running on '" & strcomputer & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer,strServiceName)
	DIM objWMIService, strWMIQuery

	strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName & "' and state='Running'"

	SET objWMIService = GETOBJECT("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

	IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
		isServiceRunning = TRUE
	ELSE
		isServiceRunning = FALSE
	END IF

END FUNCTION


OPTION EXPLICIT

DIM strComputer,strServiceName
strComputer = "Computer1" 
strServiceName = "wuauserv" ' Services 2

IF isServiceRunning(strComputer,strServiceName) THEN
	wscript.echo "The '" & strServiceName & "' service is running on '" & strcomputer & "'"
ELSE
	wscript.echo "The '" & strServiceName & "' service is NOT running on '" & strcomputer & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer,strServiceName)
	DIM objWMIService, strWMIQuery

	strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName & "' and state='Running'"

	SET objWMIService = GETOBJECT("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

	IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
		isServiceRunning = TRUE
	ELSE
		isServiceRunning = FALSE
	END IF

END FUNCTION


OPTION EXPLICIT

DIM strComputer,strServiceName
strComputer = "Computer1" 
strServiceName = "wuauserv" ' Services 3

IF isServiceRunning(strComputer,strServiceName) THEN
	wscript.echo "The '" & strServiceName & "' service is running on '" & strcomputer & "'"
ELSE
	wscript.echo "The '" & strServiceName & "' service is NOT running on '" & strcomputer & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer,strServiceName)
	DIM objWMIService, strWMIQuery

	strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName & "' and state='Running'"

	SET objWMIService = GETOBJECT("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

	IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
		isServiceRunning = TRUE
	ELSE
		isServiceRunning = FALSE
	END IF

END FUNCTION


OPTION EXPLICIT

DIM strComputer,strServiceName
strComputer = "Computer1" 
strServiceName = "wuauserv" ' Services 4

IF isServiceRunning(strComputer,strServiceName) THEN
	wscript.echo "The '" & strServiceName & "' service is running on '" & strcomputer & "'"
ELSE
	wscript.echo "The '" & strServiceName & "' service is NOT running on '" & strcomputer & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer,strServiceName)
	DIM objWMIService, strWMIQuery

	strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName & "' and state='Running'"

	SET objWMIService = GETOBJECT("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

	IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
		isServiceRunning = TRUE
	ELSE
		isServiceRunning = FALSE
	END IF

END FUNCTION


“Server 2

OPTION EXPLICIT

DIM strComputer,strServiceName
strComputer = "Computer2" 
strServiceName = "wuauserv" ' Services 1

IF isServiceRunning(strComputer,strServiceName) THEN
	wscript.echo "The '" & strServiceName & "' service is running on '" & strcomputer & "'"
ELSE
	wscript.echo "The '" & strServiceName & "' service is NOT running on '" & strcomputer & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer,strServiceName)
	DIM objWMIService, strWMIQuery

	strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName & "' and state='Running'"

	SET objWMIService = GETOBJECT("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

	IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
		isServiceRunning = TRUE
	ELSE
		isServiceRunning = FALSE
	END IF

END FUNCTION


OPTION EXPLICIT

DIM strComputer,strServiceName
strComputer = "Computer2" 
strServiceName = "wuauserv" ' Services 2

IF isServiceRunning(strComputer,strServiceName) THEN
	wscript.echo "The '" & strServiceName & "' service is running on '" & strcomputer & "'"
ELSE
	wscript.echo "The '" & strServiceName & "' service is NOT running on '" & strcomputer & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer,strServiceName)
	DIM objWMIService, strWMIQuery

	strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName & "' and state='Running'"

	SET objWMIService = GETOBJECT("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

	IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
		isServiceRunning = TRUE
	ELSE
		isServiceRunning = FALSE
	END IF

END FUNCTION


OPTION EXPLICIT

DIM strComputer,strServiceName
strComputer = "Computer2" 
strServiceName = "wuauserv" ' Services 3

IF isServiceRunning(strComputer,strServiceName) THEN
	wscript.echo "The '" & strServiceName & "' service is running on '" & strcomputer & "'"
ELSE
	wscript.echo "The '" & strServiceName & "' service is NOT running on '" & strcomputer & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer,strServiceName)
	DIM objWMIService, strWMIQuery

	strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName & "' and state='Running'"

	SET objWMIService = GETOBJECT("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

	IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
		isServiceRunning = TRUE
	ELSE
		isServiceRunning = FALSE
	END IF

END FUNCTION


OPTION EXPLICIT

DIM strComputer,strServiceName
strComputer = "Computer2" 
strServiceName = "wuauserv" ' Services 4

IF isServiceRunning(strComputer,strServiceName) THEN
	wscript.echo "The '" & strServiceName & "' service is running on '" & strcomputer & "'"
ELSE
	wscript.echo "The '" & strServiceName & "' service is NOT running on '" & strcomputer & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer,strServiceName)
	DIM objWMIService, strWMIQuery

	strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName & "' and state='Running'"

	SET objWMIService = GETOBJECT("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

	IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
		isServiceRunning = TRUE
	ELSE
		isServiceRunning = FALSE
	END IF

END FUNCTION


“Server 3

OPTION EXPLICIT

DIM strComputer,strServiceName
strComputer = "Computer3" 
strServiceName = "wuauserv" ' Services 1

IF isServiceRunning(strComputer,strServiceName) THEN
	wscript.echo "The '" & strServiceName & "' service is running on '" & strcomputer & "'"
ELSE
	wscript.echo "The '" & strServiceName & "' service is NOT running on '" & strcomputer & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer,strServiceName)
	DIM objWMIService, strWMIQuery

	strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName & "' and state='Running'"

	SET objWMIService = GETOBJECT("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

	IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
		isServiceRunning = TRUE
	ELSE
		isServiceRunning = FALSE
	END IF

END FUNCTION


OPTION EXPLICIT

DIM strComputer,strServiceName
strComputer = "Computer3" 
strServiceName = "wuauserv" ' Services 2

IF isServiceRunning(strComputer,strServiceName) THEN
	wscript.echo "The '" & strServiceName & "' service is running on '" & strcomputer & "'"
ELSE
	wscript.echo "The '" & strServiceName & "' service is NOT running on '" & strcomputer & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer,strServiceName)
	DIM objWMIService, strWMIQuery

	strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName & "' and state='Running'"

	SET objWMIService = GETOBJECT("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

	IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
		isServiceRunning = TRUE
	ELSE
		isServiceRunning = FALSE
	END IF

END FUNCTION


OPTION EXPLICIT

DIM strComputer,strServiceName
strComputer = "Computer3" 
strServiceName = "wuauserv" ' Services 3

IF isServiceRunning(strComputer,strServiceName) THEN
	wscript.echo "The '" & strServiceName & "' service is running on '" & strcomputer & "'"
ELSE
	wscript.echo "The '" & strServiceName & "' service is NOT running on '" & strcomputer & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer,strServiceName)
	DIM objWMIService, strWMIQuery

	strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName & "' and state='Running'"

	SET objWMIService = GETOBJECT("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

	IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
		isServiceRunning = TRUE
	ELSE
		isServiceRunning = FALSE
	END IF

END FUNCTION


OPTION EXPLICIT

DIM strComputer,strServiceName
strComputer = "Computer3" 
strServiceName = "wuauserv" ' Services 4

IF isServiceRunning(strComputer,strServiceName) THEN
	wscript.echo "The '" & strServiceName & "' service is running on '" & strcomputer & "'"
ELSE
	wscript.echo "The '" & strServiceName & "' service is NOT running on '" & strcomputer & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer,strServiceName)
	DIM objWMIService, strWMIQuery

	strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName & "' and state='Running'"

	SET objWMIService = GETOBJECT("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

	IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
		isServiceRunning = TRUE
	ELSE
		isServiceRunning = FALSE
	END IF

END FUNCTION



“Server 4

OPTION EXPLICIT

DIM strComputer,strServiceName
strComputer = "Computer4" 
strServiceName = "wuauserv" ' Services 1

IF isServiceRunning(strComputer,strServiceName) THEN
	wscript.echo "The '" & strServiceName & "' service is running on '" & strcomputer & "'"
ELSE
	wscript.echo "The '" & strServiceName & "' service is NOT running on '" & strcomputer & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer,strServiceName)
	DIM objWMIService, strWMIQuery

	strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName & "' and state='Running'"

	SET objWMIService = GETOBJECT("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

	IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
		isServiceRunning = TRUE
	ELSE
		isServiceRunning = FALSE
	END IF

END FUNCTION


OPTION EXPLICIT

DIM strComputer,strServiceName
strComputer = "Computer4" 
strServiceName = "wuauserv" ' Services 2

IF isServiceRunning(strComputer,strServiceName) THEN
	wscript.echo "The '" & strServiceName & "' service is running on '" & strcomputer & "'"
ELSE
	wscript.echo "The '" & strServiceName & "' service is NOT running on '" & strcomputer & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer,strServiceName)
	DIM objWMIService, strWMIQuery

	strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName & "' and state='Running'"

	SET objWMIService = GETOBJECT("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

	IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
		isServiceRunning = TRUE
	ELSE
		isServiceRunning = FALSE
	END IF

END FUNCTION


OPTION EXPLICIT

DIM strComputer,strServiceName
strComputer = "Computer4" 
strServiceName = "wuauserv" ' Services 3

IF isServiceRunning(strComputer,strServiceName) THEN
	wscript.echo "The '" & strServiceName & "' service is running on '" & strcomputer & "'"
ELSE
	wscript.echo "The '" & strServiceName & "' service is NOT running on '" & strcomputer & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer,strServiceName)
	DIM objWMIService, strWMIQuery

	strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName & "' and state='Running'"

	SET objWMIService = GETOBJECT("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

	IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
		isServiceRunning = TRUE
	ELSE
		isServiceRunning = FALSE
	END IF

END FUNCTION


OPTION EXPLICIT

DIM strComputer,strServiceName
strComputer = "Computer4" 
strServiceName = "wuauserv" ' Services 4

IF isServiceRunning(strComputer,strServiceName) THEN
	wscript.echo "The '" & strServiceName & "' service is running on '" & strcomputer & "'"
ELSE
	wscript.echo "The '" & strServiceName & "' service is NOT running on '" & strcomputer & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer,strServiceName)
	DIM objWMIService, strWMIQuery

	strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName & "' and state='Running'"

	SET objWMIService = GETOBJECT("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

	IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
		isServiceRunning = TRUE
	ELSE
		isServiceRunning = FALSE
	END IF

END FUNCTION

Open in new window

0
 
LVL 5

Accepted Solution

by:
rov17 earned 2000 total points
ID: 36916468
Bloody stupid! Sorry just cleaned it a lot and adjust it… things you do while being at work :-)
-      If you going to run this form an admin server, I would put a sleeping command between the shutdown and the services section (to give the servers time to start) and it would be better to split them up to two scripts that runs via schedule task, the second schedule runs after 20 min of the first one..
Part of the clients list that you need to put for the servers
You will need to identify the following form line 38:
strComputer1 = "Computer1"
strComputer2 = "Computer2"
strComputer3 = "Computer3"
strComputer4 = "Computer4"
strServiceName1 = "wuauserv" ' Services 1
strServiceName2 = "wuauserv" ' Services 2
strServiceName3 = "wuauserv" ' Services 3
strServiceName4 = "wuauserv" ' Services 4

the script id calling wmi so as mentioned you need to disable firewall on the servers

Dim oFSO, oTS, sClient, oWindows, oLocator, oConnection, oSys
Dim sUser, sPassword

'open list of client names
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTS = oFSO.OpenTextFile("C:\apps\clients.txt")

Do Until oTS.AtEndOfStream
  'get next client name
  sClient = oTS.ReadLine

  'get WMI locator
  Set oLocator = CreateObject("WbemScripting.SWbemLocator")

  'Connect to remote WMI
  Set oConnection = oLocator.ConnectServer(sClient, "root\cimv2", sUser, sPassword)

  'issue shutdown to OS

 
    Set oWindows = oConnection.ExecQuery("Select " & "Name From Win32_OperatingSystem")

  For Each oSys In oWindows
    oSys.Win32ShutDown(5)
  Next
Loop

'close the text file
oTS.Close




'Services section

DIM strComputer1,strServiceName1,strComputer2,strComputer3,strComputer4,strServiceName2,strServiceName3,strServiceName4

strComputer1 = "Computer1"
strComputer2 = "Computer2"
strComputer3 = "Computer3"
strComputer4 = "Computer4"
strServiceName1 = "wuauserv" ' Services 1
strServiceName2 = "wuauserv" ' Services 2
strServiceName3 = "wuauserv" ' Services 3
strServiceName4 = "wuauserv" ' Services 4

IF isServiceRunning(strComputer1,strServiceName1) THEN
  wscript.echo "The '" & strServiceName1 & "' service is running on '" & strcomputer1 & "'"
ELSE
  wscript.echo "The '" & strServiceName1 & "' service is NOT running on '" & strcomputer1 & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer1,strServiceName1)
  DIM objWMIService, strWMIQuery

  strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName1 & "' and state='Running'"

  SET objWMIService = GETOBJECT("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer1 & "\root\cimv2")

  IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
    isServiceRunning = TRUE
  ELSE
    isServiceRunning = FALSE
  END IF

END FUNCTION



IF isServiceRunning(strComputer1,strServiceName2) THEN
  wscript.echo "The '" & strServiceName2 & "' service is running on '" & strcomputer1 & "'"
ELSE
  wscript.echo "The '" & strServiceName2 & "' service is NOT running on '" & strcomputer1 & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer1,strServiceName2)
  DIM objWMIService, strWMIQuery

  strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName2 & "' and state='Running'"

  SET objWMIService = GETOBJECT("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer1 & "\root\cimv2")

  IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
    isServiceRunning = TRUE
  ELSE
    isServiceRunning = FALSE
  END IF

END FUNCTION


IF isServiceRunning(strComputer1,strServiceName3) THEN
  wscript.echo "The '" & strServiceName3 & "' service is running on '" & strcomputer1 & "'"
ELSE
  wscript.echo "The '" & strServiceName3 & "' service is NOT running on '" & strcomputer1 & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer1,strServiceName3)
  DIM objWMIService, strWMIQuery

  strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName3 & "' and state='Running'"

  SET objWMIService = GETOBJECT("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer1 & "\root\cimv2")

  IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
    isServiceRunning = TRUE
  ELSE
    isServiceRunning = FALSE
  END IF

END FUNCTION


IF isServiceRunning(strComputer1,strServiceName4) THEN
  wscript.echo "The '" & strServiceName4 & "' service is running on '" & strcomputer1 & "'"
ELSE
  wscript.echo "The '" & strServiceName4 & "' service is NOT running on '" & strcomputer1 & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer1,strServiceName4)
  DIM objWMIService, strWMIQuery

  strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName4 & "' and state='Running'"

  SET objWMIService = GETOBJECT("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer1 & "\root\cimv2")

  IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
    isServiceRunning = TRUE
  ELSE
    isServiceRunning = FALSE
  END IF

END FUNCTION


'Server 2

IF isServiceRunning(strComputer2,strServiceName1) THEN
  wscript.echo "The '" & strServiceName1 & "' service is running on '" & strcomputer2 & "'"
ELSE
  wscript.echo "The '" & strServiceName1 & "' service is NOT running on '" & strcomputer2 & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer2,strServiceName1)
  DIM objWMIService, strWMIQuery

  strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName1 & "' and state='Running'"

  SET objWMIService = GETOBJECT("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer2 & "\root\cimv2")

  IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
    isServiceRunning = TRUE
  ELSE
    isServiceRunning = FALSE
  END IF

END FUNCTION


IF isServiceRunning(strComputer2,strServiceName2) THEN
  wscript.echo "The '" & strServiceName2 & "' service is running on '" & strcomputer2 & "'"
ELSE
  wscript.echo "The '" & strServiceName2 & "' service is NOT running on '" & strcomputer2 & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer2,strServiceName2)
  DIM objWMIService, strWMIQuery

  strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName2 & "' and state='Running'"

  SET objWMIService = GETOBJECT("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer2 & "\root\cimv2")

  IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
    isServiceRunning = TRUE
  ELSE
    isServiceRunning = FALSE
  END IF

END FUNCTION


IF isServiceRunning(strComputer2,strServiceName3) THEN
  wscript.echo "The '" & strServiceName3 & "' service is running on '" & strcomputer2 & "'"
ELSE
  wscript.echo "The '" & strServiceName3 & "' service is NOT running on '" & strcomputer2 & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer2,strServiceName3)
  DIM objWMIService, strWMIQuery

  strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName3 & "' and state='Running'"

  SET objWMIService = GETOBJECT("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer2 & "\root\cimv2")

  IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
    isServiceRunning = TRUE
  ELSE
    isServiceRunning = FALSE
  END IF

END FUNCTION


IF isServiceRunning(strComputer2,strServiceName4) THEN
  wscript.echo "The '" & strServiceName4 & "' service is running on '" & strcomputer2 & "'"
ELSE
  wscript.echo "The '" & strServiceName4 & "' service is NOT running on '" & strcomputer2 & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer2,strServiceName4)
  DIM objWMIService, strWMIQuery

  strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName4 & "' and state='Running'"

  SET objWMIService = GETOBJECT("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer2 & "\root\cimv2")

  IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
    isServiceRunning = TRUE
  ELSE
    isServiceRunning = FALSE
  END IF

END FUNCTION


'Server 3

IF isServiceRunning(strComputer3,strServiceName1) THEN
  wscript.echo "The '" & strServiceName1 & "' service is running on '" & strcomputer3 & "'"
ELSE
  wscript.echo "The '" & strServiceName1 & "' service is NOT running on '" & strcomputer3 & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer3,strServiceName1)
  DIM objWMIService, strWMIQuery

  strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName1 & "' and state='Running'"

  SET objWMIService = GETOBJECT("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer3 & "\root\cimv2")

  IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
    isServiceRunning = TRUE
  ELSE
    isServiceRunning = FALSE
  END IF

END FUNCTION


IF isServiceRunning(strComputer3,strServiceName2) THEN
  wscript.echo "The '" & strServiceName2 & "' service is running on '" & strcomputer3 & "'"
ELSE
  wscript.echo "The '" & strServiceName2 & "' service is NOT running on '" & strcomputer3 & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer3,strServiceName2)
  DIM objWMIService, strWMIQuery

  strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName2 & "' and state='Running'"

  SET objWMIService = GETOBJECT("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer3 & "\root\cimv2")

  IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
    isServiceRunning = TRUE
  ELSE
    isServiceRunning = FALSE
  END IF

END FUNCTION

IF isServiceRunning(strComputer3,strServiceName3) THEN
  wscript.echo "The '" & strServiceName3 & "' service is running on '" & strcomputer3 & "'"
ELSE
  wscript.echo "The '" & strServiceName3 & "' service is NOT running on '" & strcomputer3 & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer3,strServiceName3)
  DIM objWMIService, strWMIQuery

  strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName3 & "' and state='Running'"

  SET objWMIService = GETOBJECT("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer3 & "\root\cimv2")

  IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
    isServiceRunning = TRUE
  ELSE
    isServiceRunning = FALSE
  END IF

END FUNCTION


IF isServiceRunning(strComputer3,strServiceName4) THEN
  wscript.echo "The '" & strServiceName4 & "' service is running on '" & strcomputer3 & "'"
ELSE
  wscript.echo "The '" & strServiceName4 & "' service is NOT running on '" & strcomputer3 & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer3,strServiceName4)
  DIM objWMIService, strWMIQuery

  strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName4 & "' and state='Running'"

  SET objWMIService = GETOBJECT("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer3 & "\root\cimv2")

  IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
    isServiceRunning = TRUE
  ELSE
    isServiceRunning = FALSE
  END IF

END FUNCTION


'Server 4

IF isServiceRunning(strComputer4,strServiceName1) THEN
  wscript.echo "The '" & strServiceName1 & "' service is running on '" & strcomputer4 & "'"
ELSE
  wscript.echo "The '" & strServiceName1 & "' service is NOT running on '" & strcomputer4 & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer4,strServiceName1)
  DIM objWMIService, strWMIQuery

  strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName1 & "' and state='Running'"

  SET objWMIService = GETOBJECT("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer4 & "\root\cimv2")

  IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
    isServiceRunning = TRUE
  ELSE
    isServiceRunning = FALSE
  END IF

END FUNCTION


IF isServiceRunning(strComputer4,strServiceName2) THEN
  wscript.echo "The '" & strServiceName2 & "' service is running on '" & strcomputer4 & "'"
ELSE
  wscript.echo "The '" & strServiceName2 & "' service is NOT running on '" & strcomputer4 & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer4,strServiceName2)
  DIM objWMIService, strWMIQuery

  strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName2 & "' and state='Running'"

  SET objWMIService = GETOBJECT("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer4 & "\root\cimv2")

  IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
    isServiceRunning = TRUE
  ELSE
    isServiceRunning = FALSE
  END IF

END FUNCTION

IF isServiceRunning(strComputer4,strServiceName3) THEN
  wscript.echo "The '" & strServiceName3 & "' service is running on '" & strcomputer4 & "'"
ELSE
  wscript.echo "The '" & strServiceName3 & "' service is NOT running on '" & strcomputer4 & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer4,strServiceName3)
  DIM objWMIService, strWMIQuery

  strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName3 & "' and state='Running'"

  SET objWMIService = GETOBJECT("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer4 & "\root\cimv2")

  IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
    isServiceRunning = TRUE
  ELSE
    isServiceRunning = FALSE
  END IF

END FUNCTION


IF isServiceRunning(strComputer4,strServiceName4) THEN
  wscript.echo "The '" & strServiceName4 & "' service is running on '" & strcomputer4 & "'"
ELSE
  wscript.echo "The '" & strServiceName4 & "' service is NOT running on '" & strcomputer4 & "'"
END IF

' Function to check if a service is running on a given computer
FUNCTION isServiceRunning(strComputer4,strServiceName4)
  DIM objWMIService, strWMIQuery

  strWMIQuery = "Select * from Win32_Service Where Name = '" & strServiceName4 & "' and state='Running'"

  SET objWMIService = GETOBJECT("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer4 & "\root\cimv2")

  IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
    isServiceRunning = TRUE
  ELSE
    isServiceRunning = FALSE
  END IF

  END FUNCTION

Open in new window

0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline

850 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