1.

Solve : VBSCRIPT / WMI - Remote Machine?

Answer»

Code: [Select]Option Explicit
Dim objWMIService, objItem, objService
Dim colListOfServices, strComputer, strService, intSleep
'strComputer = "."
strService = " 'winvnc' "

strComputer = InputBox ("Restart Service: " & strService & " COMPUTERNAME?:" , "Restart Service" , ".")

intSleep = 15000
'WScript.Echo " Click OK, then wait " & intSleep & " milliseconds"

'On Error Resume Next

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name =" & strService & " ")

For Each objService in colListOfServices
objService.StopService()
WScript.Sleep intSleep
objService.StartService()
Next

WScript.Echo "Your "& strService & " service has Started"
WScript.Quit
Pretty simple script and it works. I am able to restart the VNC service on a remote machine, however I have come across some machines using Windows 2003 server where this doesn't work. I am an administrator on the machine. The Administrator group has remote control over WMI but when ever I try to use the script on those machines I get an error at this line...

Code: [Select]Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Error Message

Line: 15
Column: 1
Error: The Remote server machine does not exist or is unavailable:
'GetObject'
Code 800A01CE - Searching on this tells me to TURN my machine on.


My machine is on, I am able to on my local machine run 'services.msc' connect to the remote machine and restart the service manually. The script if moved locally to the machine will work without a hitch, stops then starts the service again. So I am not sure what to do next or what this error might be trying to tell me.

Thoughts Questions, comments? Thanks for your help.



Code: [Select]sc \\SERVERname stop winvnc

ping -n 15 127.0.0.1 > nul

sc \\SERVERname start winvnc

The above code works. Well I'm confused. I have been trying to use VBSCRIPT more and more and this time it looks like I might have to go back to a good ol' batch file. Ha Ha. Comes full CIRCLE doesn't it.

If anyone has any ideas for the VBSCRIPT solution by all means, but I thought I would share that bit of code will work.
Code: [Select]ECHO OFF
Title Restart Service Remotely
REM %strService% is a case sentivite value from server
set strService=winvnc
rem set intSleep=6

:setCompName
cls
set /p strComputer=Restart Service: %strService% Computername?: %=%
IF "%strComputer%"=="" (
GOTO setCompName
) ELSE (
Title Restart Service:%strService% Remotely on %strComputer%
)
REM ENDIF
REM Echo "%strComputer%" & pause
Rem Test Connectivity
GOTO OnlineTest
:EndOnlineTest
REM ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
:RestartService
sc \\%strComputer% stop %strService%
IF %ErrorLevel% NEQ 0 (
CLS & ECHO Service:"%strService%" failed to stop on "%strComputer%" & PAUSE
GOTO EOF
) ELSE (
CLS & ECHO Service:"%strService%" stopped on "%strComputer%" & PAUSE
)

REM ping -n %intSleep% 127.0.0.1 > nul

sc \\%strComputer% start %strService%
IF %ErrorLevel% NEQ 0 (
CLS & ECHO Service:"%strService%" failed to start on "%strComputer%" & PAUSE
GOTO EndRestartService
) ELSE (
CLS & ECHO Service:"%strService%" started on "%strComputer%" & PAUSE
GOTO EndRestartService
)

REM ping -n %intSleep% 127.0.0.1 > nul

:EndRestartService
GOTO EOF
REM ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
:OnlineTest
Ping -n 1 -w 1000 %strComputer% > nul
REM Echo PingErrLvl:%Errorlevel% & Pause
IF %ErrorLevel% NEQ 0 (
ECHO CONNECTION test to %strComputer% failed & PAUSE
GOTO EOF
) ELSE (
REM ErrLvl 0 means SUCCESSFULLY PING
GOTO EndOnlineTest
)
REM ENDIF
GOTO EndOnlineTest
REM ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
:EOF
EXIT
Final update. Above is what I came up with as a final solution for a batch/script version just encase anyone else wants it. It does have one bug with the "SC" command where if the server doesn't have the service you are restarting it will still have an errorlevel of zero for my purposes this isn't that important but just encase you need to know.



Discussion

No Comment Found