Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 449
  • Last Modified:

Vbscript to change domain account settings on windows service

I have 4 services that use a domain account.  I need to query a list of servers from a text file, stop each service, change username and password, then start the service.

I've searched and can't seem to find exactly what I'm looking for and don't have the experience to write it myself.  Thanks.
0
56MDG
Asked:
56MDG
  • 9
  • 8
1 Solution
 
rlandquistCommented:
Give this a try.
Change the services listed in the arrServices line
Change the username and password on line 20
'open a text file of computer names
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTS = oFSO.OpenTextFile("c:\computers.txt")
arrServices = Array("Messenger", "Alerter", "wuauserv")

Do Until oTS.AtEndOfStream
        
    'get computer name
    sComputer = oTS.ReadLine
        
    'connect to the computer's WMI provider
    Set oWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
            
    For Each strService In arrServices
        Set colServices = oWMIService.ExecQuery _
        ( "Select * from Win32_Service where Name= '" & strService & "'" )
        For Each objService In colServices
            errReturn = objService.StopService()
            errServiceChange = objService.Change(,,,,,, ".\NEWUSER", "NeWP@ss#0rD")
            errReturn = objService.StartService()
        Next
    Next
    oTS.Close

Open in new window

0
 
56MDGAuthor Commented:
I get an expected loop on the last line.
0
 
rlandquistCommented:
Sorry. Put loop on a new line between the second next and the close
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
56MDGAuthor Commented:
It runs through the script now, however it does not show any changes on the server.  Is there a way to enable logging to show whether or not it actually completed?

Also, should it point to the service name or the executable?
0
 
rlandquistCommented:
You should use the shortname that is listed in the service properties, which is also the name of the registry key in HKLM\System\currentcontrolset\services
You could also put in pauses between the commands by using
wscript.sleep 2000

For the sleep time 1000 = 1 second

Make sure you are using an administrator account.

What OS is this for?
0
 
56MDGAuthor Commented:
Server 2003 SP2.  I'll verify the service names.
0
 
rlandquistCommented:
Have you verified the service names?
0
 
56MDGAuthor Commented:
Service name is SWSvc.  Still no change in the services console.
0
 
rlandquistCommented:
Can you post the current script you are using?
0
 
56MDGAuthor Commented:
Here you go.
'open a text file of computer names
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTS = oFSO.OpenTextFile("c:\computers.txt")
arrServices = Array("SWSvc")

Do Until oTS.AtEndOfStream
        
    'get computer name
    sComputer = oTS.ReadLine
        
    'connect to the computer's WMI provider
    Set oWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
            
    For Each strService In arrServices
        Set colServices = oWMIService.ExecQuery _
        ( "Select * from Win32_Service where Name= '" & strService & "'" )
        For Each objService In colServices
            errReturn = objService.StopService()
            errServiceChange = objService.Change(,,,,,, ".\domain\user", "NeWP@ss#0rD")
            errReturn = objService.StartService()
        Next
    Next
 Loop
oTS.Close

Open in new window

0
 
rlandquistCommented:
You do not use both the . and a domain for the username.
The . is for local accounts, so if your account is a domain user just use "domain\user"
0
 
56MDGAuthor Commented:
Still no luck.  There are no errors, just completes and then nothing.  Is there a way to enable logging to show each step; such as connection to the remote server, stop service, etc.?
0
 
rlandquistCommented:
Just for fun try running the script from the server you are trying to make the change on.
I am wondering if the issue is making the change remotely.
Is there anything showing in the event log?
0
 
56MDGAuthor Commented:
Ran the script directly from the server and it did not generate any logs or make any changes.
0
 
rlandquistCommented:
Ok, I have added a some error testing.  
Please run this on the server.  Instead of double clicking on it, please run it from a command prompt with the following syntax

cscript "C:\scriptname.vbs"

Please post the output here.
'open a text file of computer names
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTS = oFSO.OpenTextFile("c:\computers.txt")
arrServices = Array("SWSvc")

Do Until oTS.AtEndOfStream
                
    'get computer name
    sComputer = oTS.ReadLine
            
    WScript.Echo "Computer name: " & sComputer
                
    'connect to the computer's WMI provider
    Set oWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
                    
    For Each strService In arrServices
        WScript.Echo "Service to change: " & strService
        Set colServices = oWMIService.ExecQuery _
        ( "Select * from Win32_Service where Name= '" & strService & "'" )
        For Each objService In colServices
            WScript.Echo "Service Display name " & objService.displayname
            WScript.Echo "Service Current State: " & objService.state
            errReturnStop = objService.StopService()
            WScript.Echo "Stop Return Value: " & errReturnStop
            WScript.Echo "Service Current State: " & objService.state
            errServiceChange = objService.Change(,,,,,, ".\domain\user", "NeWP@ss#0rD")
            WScript.Echo "Change Return Value: " & errServiceChange
            errReturnStart = objService.StartService()
            WScript.Echo "Start Return Value: " & errReturnStart
        Next
    Next
Loop
oTS.Close

Open in new window

0
 
56MDGAuthor Commented:
(ran script while service was already stopped)
Computer name: XXXXXXXXXX
Service to change: SWSvc
Service Display name SnapDrive
Service Current State: Stopped
Stop Return Value: 5
Service Current State: Stopped
Change Return Value: 0
Start Return Value: 0


(ran script while service was started)
Computer name: XXXXXXXXXX
Service to change: SWSvc
Service Display name SnapDrive
Service Current State: Started
Stop Return Value: 0
Service Current State: Stopped
Change Return Value: 0
Start Return Value: 10
0
 
rlandquistCommented:
I just want to verify that the change line is:
errServiceChange = objService.Change(,,,,,, "domain\user", "NeWP@ss#0rD")
Because I had it wrong in the last response

Lets try changing it to:

errServiceChange = objService.Change(Null ,Null ,Null ,Null ,Null ,Null ,"domain\user", "password")

Dumb question, but you are positive the password is correct for the user account you are listing?



The return values look good, 0 is completed successfully
The 10 is that the service is already started, which is odd.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now