HMCTech Posted December 15, 2011 Posted December 15, 2011 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?
Steve21 Posted December 15, 2011 Posted December 15, 2011 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 1
mac_shinobi Posted December 15, 2011 Posted December 15, 2011 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
Steve21 Posted December 15, 2011 Posted December 15, 2011 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 1
Arthur Posted December 15, 2011 Posted December 15, 2011 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 } 1
mac_shinobi Posted December 15, 2011 Posted December 15, 2011 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 ?
Arthur Posted December 16, 2011 Posted December 16, 2011 Can't edit my post above, but have just found a better way of restarting services on local and remote computers in PowerShell. Restart-Service -InputObject $(Get-Service -Computer Server1 -Name spooler)
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now