Scripting Challenge, Automatic Server rRstart and Checking Services have started


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!
vourneenwardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rov17Commented:
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
rov17Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.