Jump to content
EduGeek EdSec 2026 is Go! 27th Oct in Derby! Join us for a day of EdTech security focused talks, networking, and an evening social ×

vbs script to check hostname and i fhostname is x y or z stop then start a service


Recommended Posts

Posted

Using SCOM 2007 and we have three servers that host a service which occasionally use up all the memory. This generates a critical memory useage error on SCOM and you can set it to run a script if this happens. Unfortunately we dont want to run this script on every host which runs out of memory as the service only exists on three.

 

Can anyone help in starting a script that checks if the hostname is server1, server2 or server3 then run a net stop then start on a service?

Posted

Try something like this:

 

Set wshshell = CreateObject("WScript.Shell")
Set ObjEnv = WshShell.Environment("Process")
COMPUTERNAME = ObjEnv("COMPUTERNAME")

If COMPUTERNAME="CompName" Then



strServiceName = "ServiceName"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery("Select * from Win32_Service Where Name ='" & strServiceName & "'")
For Each objService in colListOfServices
   objService.StopService()
Next

strServiceName = "ServiceName"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'")
For Each objService in colListOfServices
   objService.StartService()
Next


Else


End IF

 

Obviously change service name, and compname (Havent tested it)

 

Steve

  • Thanks 1
Posted
Try something like this:

 

Set wshshell = CreateObject("WScript.Shell")
Set ObjEnv = WshShell.Environment("Process")
COMPUTERNAME = ObjEnv("COMPUTERNAME")

If COMPUTERNAME="CompName" Then



strServiceName = "ServiceName"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery("Select * from Win32_Service Where Name ='" & strServiceName & "'")
For Each objService in colListOfServices
   objService.StopService()
Next

strServiceName = "ServiceName"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'")
For Each objService in colListOfServices
   objService.StartService()
Next


Else


End IF

 

Obviously change service name, and compname (Havent tested it)

 

Steve

 

can't you use an array and a loop as well so you don't have to do individual checks ie

 

Servers="NameA,NameB,NameC"

 

X=Split(Servers,",")

 

For Y = 0 to UBound(Servers)

compname = X(Y)

' WMI Code here

Next

 

Coding isn't correct as I know UBound(Servers) will not work but you get the jist

Posted
can't you use an array and a loop as well so you don't have to do individual checks ie

 

Servers="NameA,NameB,NameC"

 

X=Split(Servers,",")

 

For Y = 0 to UBound(Servers)

compname = X(Y)

' WMI Code here

Next

 

Coding isn't correct as I know UBound(Servers) will not work but you get the jist

 

Well I'm assuming each server has it's own scripts that run when mem full, but yes you could.

 

Even easier way would be just do, if compname = "" or compname = "" etc Then no loops etc needed.

 

Steve

  • Thanks 1
Posted

If you use PowerShell it's even easier. :)

 

gwmi Win32_Service -Filter "Name='Spooler'" -ComputerName Server1,Server2,Server3 | ForEach-Object {
   $_.StopService()
   $_.StartService()   
}

 

With remoting enabled you can restart your service with just a single command...

 

Invoke-Command -ComputerName Server1,Server2,Server3 { Restart-Service Spooler }

  • Thanks 1
Posted

why do the checking if you could just create a script that only runs on those servers in question using a scheduler or by setting it in GPO to only run on those servers , if that is the case then a bat file with

 

net stop spooler

net start spooler

 

would of been sufficient ?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...