Link to home
Create AccountLog in
Avatar of bsharath
bsharathFlag for India

asked on

Vb script errors while execution

Vb script errors while execution


This script stops and starts a service
But errors


E:\Service_Check.vbs(23, 19) Microsoft VBScript runtime error: The remote server
 machine does not exist or is unavailable: 'GetObject'


'================================
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /k cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If
 
' Note: strService is case sensitive.
strInputFile = "servers.txt"
strServices = "camso"
arrServices = Split(strServices, ",")
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading, False)
 
While Not objInputFile.AtEndOfStream
      strServer1 = objInputFile.ReadLine
      If Ping(strServer1) = True Then
            For Each strService In arrServices
                  Set objWMIService = GetObject("winmgmts:" _
                        & "{impersonationLevel=impersonate}!\\" _
                        & strServer1 & "\root\cimv2")
                  Set colListOfServices = objWMIService.ExecQuery _
                        ("Select * from Win32_Service Where DisplayName='"& strService & "'")
                  On Error Resume Next
                  For Each objService In colListOfServices
                        If Err.Number = 0 Then
                              On Error GoTo 0
                              objService.StopService()
                              WScript.Echo ""
                              WScript.Echo strService & " service stopped on " & strServer1
                              WScript.Sleep 5000
                              objService.StartService()
                              WScript.Echo strService & " service started on " & strServer1
                        Else
                              Err.Clear
                              On Error GoTo 0
                              WScript.Echo strService & " service was not found on " & strServer1
                        End If
                  Next
            Next
      Else
            WScript.Echo ""
            WScript.Echo strServer1 & " could not be pinged."
      End If
Wend
objInputFile.Close
Set objInputFile = Nothing
 
WScript.Echo "" & VbCrLf & "Finished."
 
Function Ping(strComputer)
      Dim objShell, boolCode
      Set objShell = CreateObject("WScript.Shell")
      boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
      If boolCode = 0 Then
            Ping = True
      Else
            Ping = False
      End If
End Function
'================================
Avatar of Qlemo
Qlemo
Flag of Germany image

The message tells you that WMI is not available at the server you wanted to access.

You are pinging the target, and it seems to answer - but testing ping for success and fail does not work all time (see http://ss64.com/nt/ping.html ), so the remote machine might not be available at all. http://larsmichelsen.com/vbs/quickie-how-to-ping-a-host-in-vbs-i-got-two-ways/ shows an improvement of the method you use, and an alternative using WMI Win32_PingStatus.
Avatar of bsharath

ASKER

Thanks
Can you please change the code as needed and give me the full code. As i am not good at scripting please
The latter link has a complete replacement for the Ping function. Just use that instead of your Ping function. I have slightly modified and drastically shortened that to:
Function Ping(strHost)
    Dim oPing, oRetStatus
    Set oPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_PingStatus where address='" & strHost & "'")
 
    For Each oRetStatus In oPing
        Ping = !IsNull(oRetStatus.StatusCode) And oRetStatus.StatusCode = 0
        Set oRetStatus = Nothing
    Next
    Set oPing = Nothing
End Function

Open in new window

I get this
---------------------------
Windows Script Host
---------------------------
Script:      E:\Service_Check.vbs
Line:      60
Char:      16
Error:      Syntax error
Code:      800A03EA
Source:       Microsoft VBScript compilation error

---------------------------
OK  
---------------------------
If you provide error messages containing an error position, you need to show the corresponding line of your code at least - you might have different line numbers than we do.

I assume the error is in my line 6. Please use this corrected function instead (replaced ! with not):
Function Ping(strHost)
    Dim oPing, oRetStatus
    Set oPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_PingStatus where address='" & strHost & "'")
 
    For Each oRetStatus In oPing
        Ping = not IsNull(oRetStatus.StatusCode) And oRetStatus.StatusCode = 0
        Set oRetStatus = Nothing
    Next
    Set oPing = Nothing
End Function

Open in new window

This ran on 70+ systems and then got this error

E:\Service_Check.vbs(23, 19) Microsoft VBScript runtime error: The remote server
 machine does not exist or is unavailable: 'GetObject'
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account