VBS script fails on 2012 R2 but works on 2008

beyond infinity 1 Reputation point
2021-06-24T11:59:30.96+00:00

Greetings,
I've migrated a VBS script from 2008 to 2012. The script shutdowns or starts Microsoft services. On Server 2008, I have no issues, but on 2012, I get the error "\VBS\MS_Services.vbs(192, 2) Microsoft VBScript runtime error: Object required: 'GetServiceObject(...)' "

The script's logic stops services on several servers.
It does stop the server on the first server on the list however yields the error "GetServiceObject" when jumping to the next server on the list.

I've seen in this forum the same type of issues when migrating from 2008 to 2012, but have not seen a resolution yet. I'm hoping this post will shed some light.

Main function:

Case "STOP"
    strMailBody = strMailBody & "Stop Services" & vbCrLf & vbCrLf
    For intServiceNumber = 1 To 10
        aServiceResponses(intServiceNumber) = StopSVC(aServices(intServiceNumber))
    Next

' ************ Function StopSVC
Function StopSVC(aService)
strServerName = aService(SERVERNAME)
strService = aService(SERVICENAME)
strServiceName = aService(SHORTNAME)

intSuccess = 0                                                                     ' flag to indicate the service stopped successfully
Set objService = GetServiceObject(aService, True)             ' Get Service Object

objService.StopService()                                                      ' send the service the command, then check its state
StopSVC = WaitForServiceState(aService, STOPWAITDELAY, "Stopped")

End Function

' ************ Function GetServiceObject
Function GetServiceObject(aService, boolShowCommandLineOutput)
strServerName = aService(SERVERNAME)
strService = aService(SERVICENAME)
strServiceName = aService(SHORTNAME)

' Output some niceness to the screen when running from a command prompt
If boolShowCommandLineOutput Then WScript.Echo strServerName & ": " & strServiceName

' Get services collection through WMI
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServerName & "\root\cimv2")
Set colServices = objWMIService.ExecQuery("SELECT * FROM Win32_Service where " & "DisplayName = '" & strService & "'")

' Get Service object (there should only ever be 1 object in the collection)
For each objService in colServices
    Set GetServiceObject = objService
Next

End Function

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,429 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 32,076 Reputation points
    2021-06-24T12:42:32.073+00:00

    You don't have any error handling in the GetServiceObject function.
    If you have a typo or the server is unavailable, you need to account for that.

    ' Get services collection through WMI
     on error resume next
     Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServerName & "\root\cimv2") 
     if err.number <> 0 then 
        wscript.echo "Unable to connect to " & strServerName
        wscript.echo err.number 
        wscript.echo err.description
        ' Do something else to flag that we cannot continue......?????"
        wscript.quit
     end if     
     Set colServices = objWMIService.ExecQuery("SELECT * FROM Win32_Service where " & "DisplayName = '" & strService & "'")
      if (colServices.count = 0) then 
        wscript.echo "Unable to find service " & strService & "  on " & strServerName
        ' Do something else to flag that we cannot continue......????"
        wscript.quit    
     end if     
     on error resume next 
    
    0 comments No comments