+ Post New Thread
Results 1 to 8 of 8
Scripts Thread, vbs script to check hostname and i fhostname is x y or z stop then start a service in Coding and Web Development; Using SCOM 2007 and we have three servers that host a service which occasionally use up all the memory. This ...
  1. #1
    HMCTech's Avatar
    Join Date
    Apr 2008
    Posts
    618
    Thank Post
    37
    Thanked 48 Times in 35 Posts
    Rep Power
    22

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

    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?

  2. #2

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,686
    Thank Post
    333
    Thanked 513 Times in 481 Posts
    Rep Power
    178
    Try something like this:

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

  3. Thanks to Steve21 from:

    HMCTech (15th December 2011)

  4. #3
    HMCTech's Avatar
    Join Date
    Apr 2008
    Posts
    618
    Thank Post
    37
    Thanked 48 Times in 35 Posts
    Rep Power
    22
    You are a star! Works perfectly!

  5. #4

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,677
    Thank Post
    3,209
    Thanked 1,030 Times in 955 Posts
    Rep Power
    361
    Quote Originally Posted by Steve21 View Post
    Try something like this:

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

  6. #5

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,686
    Thank Post
    333
    Thanked 513 Times in 481 Posts
    Rep Power
    178
    Quote Originally Posted by mac_shinobi View Post
    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

  7. Thanks to Steve21 from:

    mac_shinobi (15th December 2011)

  8. #6


    Join Date
    Feb 2007
    Location
    51.405546, -0.510212
    Posts
    8,706
    Thank Post
    220
    Thanked 2,615 Times in 1,926 Posts
    Rep Power
    777
    If you use PowerShell it's even easier.

    Code:
    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...

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

  9. Thanks to Arthur from:

    mac_shinobi (15th December 2011)

  10. #7

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,677
    Thank Post
    3,209
    Thanked 1,030 Times in 955 Posts
    Rep Power
    361
    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 ?

  11. #8


    Join Date
    Feb 2007
    Location
    51.405546, -0.510212
    Posts
    8,706
    Thank Post
    220
    Thanked 2,615 Times in 1,926 Posts
    Rep Power
    777
    Can't edit my post above, but have just found a better way of restarting services on local and remote computers in PowerShell.

    Code:
    Restart-Service -InputObject $(Get-Service -Computer Server1 -Name spooler)

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 5
    Last Post: 1st July 2014, 08:34 AM
  2. Replies: 5
    Last Post: 14th June 2008, 12:18 AM
  3. Replies: 4
    Last Post: 25th December 2006, 09:15 PM
  4. VB Script ti check for folder and then copy contents to PC
    By tosca925 in forum How do you do....it?
    Replies: 4
    Last Post: 2nd October 2006, 08:15 PM
  5. Replies: 3
    Last Post: 1st March 2006, 12:08 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •