question

beyondinfinity-7762 avatar image
0 Votes"
beyondinfinity-7762 asked MotoX80 answered

VBS script fails on 2012 R2 but works on 2008

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MotoX80 avatar image
0 Votes"
MotoX80 answered

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 
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.