Change Service Credentials Across Your Domain

By | December 14, 2007

I had a client that needed to change service credentials across 50 servers for Backup Exec at the same time.
So, I wrote this script to change the credentials for any service remotely.

 

 

———-COPY EVERYTHING BELOW THIS LINE for the Script———-
' This code configures the service account
' Created 11.15.07 By Cheyenne "Chey" Harden

On Error Resume Next

Const ForReading = 1

strUser = Inputbox("Enter the Service Account Login Here!" & vbCrLf & "e.g.,MyDomainName\Administrator")
strPassword = Inputbox("Enter the Service Account Password Here!")
strSvcName = Inputbox("Enter The Name Of The Service Here!" & vbCrLf & "e.g.,ClipSrv") 
strService = "'" & strSvcName & "'" 
intSleep = 2000
intLongSleep = 10000

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
Set objTextFile = objFSO.OpenTextFile(objArgs(0), ForReading)

Do Until objTextFile.AtEndOfStream
 strComputer = objTextFile.Readline
       
 If strComputer = "" Then
  WScript.Quit 
 Else
  EnableService()
  LogonAsService()

  Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
  Set objService = objWMI.Get("Win32_Service.Name='" & strSvcName & "'")
   intService = objService.Change(,,,,,,strUser,strPassword)

   If intService > 0 Then
    WScript.Echo "Unable to set the service account, " & intService
   Else
    WScript.Echo "Successfully set the service account for: " & strComputer
    WScript.Sleep intSleep
   End If
  RestartService()
  
 End If

Loop

 

Sub EnableService()
 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 Set colServiceList = objWMIService.ExecQuery("Select * from Win32_Service " & "where Name=" & strService & "and StartMode='Disabled'")

 For Each objService In colServiceList
  errReturnCode = objService.ChangeStartMode("Automatic")
 Next
 
End Sub

 

Function LogonAsService()
 Set objShell = CreateObject("WScript.Shell")
  objShell.Run "%COMSPEC% /c ntrights.exe " & "-m \\" & strComputer & " -u " & strUser & " +r SeServiceLogonRight",0,TRUE
 set objShell = Nothing
End Function

 

Function RestartService()
 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 intLongSleep
  objService.StartService()
  WScript.Echo "Service Restarted Successfully"
Next

End Function

———-COPY EVERYTHING ABOVE THIS LINE for the Script———-

 

 

PLEASE MAKE SURE NO WORD WRAPPING IS HAPPENING IN YOUR SCRIPT!!!

1. Drop a .txt file with the names of the target machines listed in it.
2. Make sure to be an admin on the target machines.
3. Make sure that you have the Windows Server 2003 Resource Kit Tools installed on the machine you are running this from. The utility ntrights.exe is needed to use the script. This can be installed on Windows XP.

 

 
This information is provided "AS IS" with no warranties expressed or implied.

Leave a Reply

Your email address will not be published. Required fields are marked *