didba
asked on
How to modify service.vbs to turn services on / off on another pc in a different domain?
How to modify the attached code to turn services on / off on another PC in a different domain? The 4 variables I think that need to be used are are servername, servicename, username, password of which the script is using servername and servicename. Username and password need to be added. Thank you.
' ReStartService.vbs
' Sample script to Stop or Start a Service
' -------------------------------------------------------'
Option Explicit
Dim objWMIService, objItem, objService
Dim colListOfServices, strComputer, strService, intSleep
strComputer = "."
intSleep = 15000
WScript.Echo " Click OK, then wait " & intSleep & " milliseconds"
'On Error Resume Next
' NB strService is case sensitive.
strService = " 'Alerter' "
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
' End of Example WMI script to Start / Stop services
Hi, see this for using alternate credentials for WMI queries....
http://msdn.microsoft.com/en-us/library/aa393720(VS.85).aspx
Regards,
Rob.
http://msdn.microsoft.com/en-us/library/aa393720(VS.85).aspx
Regards,
Rob.
' ReStartService.vbs
' Sample script to Stop or Start a Service
' -------------------------------------------------------'
Option Explicit
Dim strPath, strCommand, objShell
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
Dim objWMIService, objItem, objService, strDomain
Dim colListOfServices, strComputer, strService, intSleep
Dim strUser, objPassword, strPassword, objSWbemLocator
intSleep = 15000
strComputer = "RemotePC"
strDomain = "YourDomain"
Wscript.StdOut.Write "Please enter your user name:"
strUser = Wscript.StdIn.ReadLine
Set objPassword = CreateObject("ScriptPW.Password")
Wscript.StdOut.Write "Please enter your password:"
strPassword = objPassword.GetPassword()
WScript.Echo " Click OK, then wait " & intSleep & " milliseconds"
'On Error Resume Next
' NB strService is case sensitive.
strService = " 'Alerter' "
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = objSWbemLocator.ConnectServer(strComputer, _
"root\CIMV2", _
strUser, _
strPassword, _
"MS_409", _
"NTLMDomain:" & strDomain)
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
' End of Example WMI script to Start / Stop services
ASKER
Hello Rob,
When I run the script, here is the error I receive:
C:\scripts\RestartService. vbs(40, 1) SWbemLocator: The RPC server is unavailable
Basically I am trying to test this on my local machine using my credentials. This should be ok to use as a test scenario, correct? Thank you.
When I run the script, here is the error I receive:
C:\scripts\RestartService.
Basically I am trying to test this on my local machine using my credentials. This should be ok to use as a test scenario, correct? Thank you.
Hi, try this code, it's got some more error checking on the domain passing....
>> I am trying to test this on my local machine using my credentials. This should be ok to use as a test scenario, correct?
Actually, no.....you cannot connect to your own computer using alternate credentials like this. You must connect to another computer.
Regards,
Rob.
>> I am trying to test this on my local machine using my credentials. This should be ok to use as a test scenario, correct?
Actually, no.....you cannot connect to your own computer using alternate credentials like this. You must connect to another computer.
Regards,
Rob.
' ReStartService.vbs
' Sample script to Stop or Start a Service
' -------------------------------------------------------'
Option Explicit
Dim strPath, strCommand, objShell
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
Dim objWMIService, objItem, objService, strDomain
Dim colListOfServices, strComputer, strService, intSleep
Dim strUser, objPassword, strPassword, objSWbemLocator
intSleep = 15000
Dim objNetwork
Set objNetwork = CreateObject("WScript.Network")
strComputer = "D9DT9P1SRING"
strDomain = objNetwork.UserDomain
Wscript.StdOut.Write "Please enter your user name: "
strUser = Wscript.StdIn.ReadLine
If InStr(strUser, "\") > 0 Then
strDomain = Left(strUser, InStr(strUser, "\") - 1)
strUser = Mid(strUser, InStr(strUser, "\") + 1)
End If
Set objPassword = CreateObject("ScriptPW.Password")
Wscript.StdOut.Write "Please enter your password: "
strPassword = objPassword.GetPassword()
WScript.Echo " Click OK, then wait " & intSleep & " milliseconds"
'On Error Resume Next
' NB strService is case sensitive.
strService = " 'Alerter' "
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = objSWbemLocator.ConnectServer(strComputer, _
"root\CIMV2", _
strUser, _
strPassword, _
"MS_409", _
"NTLMDomain:" + strDomain)
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
' End of Example WMI script to Start / Stop services
ASKER
Hello Rob,
Ok, I tried testing the script on a different machine and I receive the following error:
C:\scripts\RestartService. vbs(36, 1) Microsoft VBScript runtime error: ActiveX component can't create object: 'ScriptPW.Password'
I haven't tried the new script yet as I think we are close with the previous one. The code snippet I used is attached below. Any thoughts? Thank you.
Ok, I tried testing the script on a different machine and I receive the following error:
C:\scripts\RestartService.
I haven't tried the new script yet as I think we are close with the previous one. The code snippet I used is attached below. Any thoughts? Thank you.
' ReStartService.vbs modified version 1
' Sample script to Stop or Start a Service
' -------------------------------------------------------'
Option Explicit
Dim strPath, strCommand, objShell
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
Dim objWMIService, objItem, objService, strDomain
Dim colListOfServices, strComputer, strService, intSleep
Dim strUser, objPassword, strPassword, objSWbemLocator
intSleep = 15000
WScript.StdOut.Write "Please enter the server name: "
strComputer = Wscript.StdIn.ReadLine
'WScript.StdOut.Write "Please enter the service name in quotes: "
'strService = Wscript.StdIn.ReadLine
'strService = " 'Messenger' "
'WScript.StdOut.Write strService
Wscript.StdOut.Write "Please enter the domain: "
strDomain = Wscript.StdIn.ReadLine
Wscript.StdOut.Write "Please enter the user name: "
strUser = Wscript.StdIn.ReadLine
Set objPassword = CreateObject("ScriptPW.Password")
Wscript.StdOut.Write "Please enter the password: "
strPassword = objPassword.GetPassword()
WScript.Echo " Click OK, then wait " & intSleep & " milliseconds "
'On Error Resume Next
' NB strService is case sensitive.
strService = " 'Messenger' "
'strService = " & "'" & strService & "'" & "
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = objSWbemLocator.ConnectServer(strComputer, _
"root\CIMV2", _
strUser, _
strPassword, _
"MS_409", _
"NTLMDomain:" & strDomain)
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 "The "& strService & " service has started. "
WScript.Quit
' End of Example WMI script to Start / Stop services
ASKER
Ok, I figured out the "ActiveX component can't create object" error. It was because I didn't have the scriptpw.dll file on the machine. So I was able to enter the password and get past this error. However I am getting the following error: SWbemLocator: The RPC server is unavailable. Same code as above.
OK, there could be a few issues here. It could be that DCOM is not enabled, or that you are not a member the local Administrators group of both machines, or that a firewall is blocking the requests.....
Just to test, from machine A, can you right-click My Computer, click Manage, then right-click the Computer Management (Local) icon at the top, and click "Connect to another computer..."
Enter the hostname of machine B, and click OK. That should connect fine if there's no connectivity issues. See if you can view the Event Log as well.
Let me know how that goes....
Regards,
Rob.
Just to test, from machine A, can you right-click My Computer, click Manage, then right-click the Computer Management (Local) icon at the top, and click "Connect to another computer..."
Enter the hostname of machine B, and click OK. That should connect fine if there's no connectivity issues. See if you can view the Event Log as well.
Let me know how that goes....
Regards,
Rob.
ASKER
Hi Rob,
I was able to connect to the other computer using the above method no problem. I was also able to view the event log of the other computer. This leads me to believe there are no connectivity issues. Any other ideas? Thank you.
I was able to connect to the other computer using the above method no problem. I was also able to view the event log of the other computer. This leads me to believe there are no connectivity issues. Any other ideas? Thank you.
ASKER
Hi Rob,
I checked to see if DCOM is enabled on both computers and it is.
I checked to see if DCOM is enabled on both computers and it is.
ASKER
I also turned off XP's Windows Firewall but I still get the "SWbemLocator: The RPC server is unavailable" error message?
Hi, you will get that error is the remote machine cannot be connected to. Please try this....I have also added a Ping function to test basic connectivity before trying to connect....
Regards,
Rob.
Regards,
Rob.
' ReStartService.vbs
' Sample script to Stop or Start a Service
' -------------------------------------------------------'
Option Explicit
Dim strPath, strCommand, objShell
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
Dim objWMIService, objItem, objService, strDomain
Dim colListOfServices, strComputer, strService, intSleep
Dim strUser, objPassword, strPassword, objSWbemLocator
intSleep = 15000
Dim objNetwork
Set objNetwork = CreateObject("WScript.Network")
'strComputer = "D9DT9P1SRING"
WScript.StdOut.Write "Please enter the server name: "
strComputer = Wscript.StdIn.ReadLine
strDomain = objNetwork.UserDomain
Wscript.StdOut.Write "Please enter your user name: "
strUser = Wscript.StdIn.ReadLine
If InStr(strUser, "\") > 0 Then
strDomain = Left(strUser, InStr(strUser, "\") - 1)
strUser = Mid(strUser, InStr(strUser, "\") + 1)
End If
Set objPassword = CreateObject("ScriptPW.Password")
Wscript.StdOut.Write "Please enter your password: "
strPassword = objPassword.GetPassword()
'On Error Resume Next
' NB strService is case sensitive.
strService = " 'Alerter' "
If Ping(strComputer) = True Then
WScript.Echo VbCrLf & "Connecting to " & strComputer & "..." & VbCrLf
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = objSWbemLocator.ConnectServer(strComputer, _
"root\CIMV2", _
strUser, _
strPassword, _
"MS_409", _
"NTLMDomain:" + strDomain)
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"
Else
WScript.Echo VbCrLf & "Failed to ping " & strComputer & VbCrLf
End If
WScript.Quit
' End of Example WMI script to Start / Stop services
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
ASKER
Hi Rob,
I was able to get past the "SWbemLocator: The RPC server is unavailable" error with the above code. The PC was able to ping the remote server and it got to "your service has started" part of the code. But when I checked the remote server, the Alerter service wasn't started / stopped. I also made sure the Alerter service is set to manual on the remote server so it can be turned on / off. Any ideas? I think we are getting close. Thank you.
I was able to get past the "SWbemLocator: The RPC server is unavailable" error with the above code. The PC was able to ping the remote server and it got to "your service has started" part of the code. But when I checked the remote server, the Alerter service wasn't started / stopped. I also made sure the Alerter service is set to manual on the remote server so it can be turned on / off. Any ideas? I think we are getting close. Thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for all your help Rob, I was able to modify the script to our needs to get the job done. Thanks again!
That's great. Thanks for the grade.
Regards,
Rob.
Regards,
Rob.
strComputer = "ComputerA"
would allow you to connect to ComputerA's service tree...