Thursday, 18 June 2015

Change Service Account Information using VBScript

We can easily manage Windows Service account through vbscript by using the WMI class Win32_Service. Use the below vbscript code to change windows service account

1. Copy the below vbscript code and paste it in notepad or a VBScript editor.
2. Change the value strService into your own windows service name which you want to change service account.
3. Replace your new user account and password in the variables strUser and strPwd.
3. If you want to modify a service account in Remote machine, set the Remote computer name in the variable strComputer instead of "." (local machine).
4. Save the file with a .vbs extension, for example: ChangeServiceAccount.vbs
5. Double-click the vbscript file (or Run this file from command window) to change the given windows service logon user account .
Option Explicit
Dim objWMIService, objService,errOut
Dim strService,strComputer,strUser,strPwd
strService="RemoteRegistry"
strUser= ".\Morgan"
strPwd = "Password"
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
For Each objService In objWMIService.ExecQuery("Select * from Win32_Service Where Name = '"&strService&"'")
    errOut = objService.Change( , , , , , , strUser, strPwd)
  If(errOut=0) Then
     WScript.Echo strService & ": Service account changed successfully"
  Else
     WScript.Echo strService & ": Account change failed. Error code: "& errOut
  End If
Next
WScript.Quit
Use the following vbscript code if you want to change only password of the service account.
Option Explicit
Dim objWMIService, objService,errOut
Dim strService,strComputer,strPwd
strService="RemoteRegistry"
strPwd = "NewPassword"
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
For Each objService In objWMIService.ExecQuery("Select * from Win32_Service Where Name = '"&strService&"'")
    errOut = objService.Change( , , , , , , , strPwd)
  If(errOut=0) Then
     WScript.Echo strService & ": Service account password changed successfully"
  Else
     WScript.Echo strService & ": Password change failed. Error code: "& errOut
  End If
Next
WScript.Quit

Advertisements
Advertisements

No comments:

Post a Comment