Solved

How to modify service.vbs to turn services on / off on another pc in a different domain?

Posted on 2008-10-08
16
1,737 Views
Last Modified: 2013-11-10
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

Open in new window

0
Comment
Question by:didba
  • 8
  • 6
16 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 22678334
strComputer = "." should be the only line needed...

strComputer = "ComputerA"

would allow you to connect to ComputerA's service tree...
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22683424
Hi, see this for using alternate credentials for WMI queries....
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

Open in new window

0
 

Author Comment

by:didba
ID: 22688993
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.

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22699141
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.
' 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

Open in new window

0
 

Author Comment

by:didba
ID: 22703426
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.



' 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

Open in new window

0
 

Author Comment

by:didba
ID: 22704049
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.




0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22706987
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:didba
ID: 22711038
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.

0
 

Author Comment

by:didba
ID: 22714249
Hi Rob,

I checked to see if DCOM is enabled on both computers and it is.




0
 

Author Comment

by:didba
ID: 22714899
I also turned off XP's Windows Firewall but I still get the  "SWbemLocator: The RPC server is unavailable" error message?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22717965
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.
' 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

Open in new window

0
 

Author Comment

by:didba
ID: 22725290
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.

0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 22725369
Sorry, I had forgot to mention that I had commented out these lines:
      'objService.StopService()
      'WSCript.Sleep intSleep
      'objService.StartService()


uncomment those, and the service will be stopped, the script will wait for the intSleep time period, then restart the service.

You could change those lines to this for more output.

      WScript.Echo "Stopping the " & strService & " service...."
      objService.StopService()
      WScript.Echo "Please wait..."
      WSCript.Sleep intSleep
      WScript.Echo "Starting the " & strService & " service..."
      objService.StartService()

Regards,

Rob.
0
 

Author Closing Comment

by:didba
ID: 31504381
Thank you for all your help Rob, I was able to modify the script to our needs to get the job done.  Thanks again!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22735269
That's great.  Thanks for the grade.

Regards,

Rob.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now