Home > HowTo, VbScripts > Change the Service Account

Change the Service Account

November 25th, 2008 Leave a comment Go to comments
Well as I posted bellow about the bug in NRPE_NT, it result I will have to change all my Nagios Plugins service set to a different account,

the code bellow is how I will be doing this.

I used the RemoteExec script to copy this over to the server and execute it.

changeserviceacct.vbs

‘Should reset the password, stop the service, change the service password then
‘Author: Felipe Ferreira Date: 27/11/2008 Version: 1.0

‘GLOBAL VARIABLES
dim userdomain
dim verbose : verbose = 1
dim strPassword : strPassword = “Passw0rD”
dim strUser : strUser = “OPExchange”
Dim strServiceName : strServiceName = “NRPE_NT”
Dim strStartName

‘CALLS THE SUBS
getdomain()
pt “Domain is: ” & userdomain
strStartName = userdomain & “” & strUser
pt “Setting password for ” & strUser
setpass()
pt “Setting password for service ” & strServiceName & ” and ” & strStartName
setsvc()
pt “Restarting service”
restartsvc(strServiceName) ‘pass short service name
pt “Deleting itself now”
deleteitself()

sub setsvc()
Dim WServices
Set WServices = GetObject(“WinMgmts:{impersonationLevel=impersonate}!root/cimv2”)
strServiceName = chr(34) & strServiceName & chr(34)
s = “Win32_Service.Name=” & strServiceName
pt “Changing Service: ” & s
Set TheService = WServices.Get(s)
Set method = TheService.Methods_(“Change”)
Set inParam = method.InParameters.SpawnInstance_()
inParam.StartName = strStartName
inParam.StartPassword = strPassword
Set outParam = TheService.ExecMethod_(“Change”, inParam)
If outParam.returnValue = 0 Then
pt ” -> Service users/password has been changed”
Else
pt “—–Failed to change service user/pass—-”
wscript.quit
End If

Set method = Nothing
Set inParam = Nothing
Set outParam = Nothing
Set TheService = Nothing
Set WServices = Nothing
end sub

sub getdomain()
set objShell = CreateObject(“Wscript.shell”)
Set objExecObject = objShell.Exec(“cmd /c echo %USERDOMAIN%”)
Do While Not objExecObject.StdOut.AtEndOfStream
userdomain = objExecObject.StdOut.ReadLine() ‘gets %userdomain%
userdomain = trim(userdomain)
loop
end sub

sub setpass()
Dim objOU, objUser, objRootDSE
Dim strContainer, strDNSDomain, strPassword

‘ Bind to Active Directory Domain
Set objRootDSE = GetObject(“LDAP://RootDSE”)
strDNSDomain = objRootDSE.Get(“DefaultNamingContext”)
strContainer = “CN=Users,”
strContainer = strContainer & strDNSDomain
‘pt “Searching user in ” & strContainer
set objOU =GetObject(“LDAP://” & strContainer )
For each objUser in objOU
If objUser.class=”user” then
‘pt lcase(objUser.name)
if instr(lcase(objUser.name),lcase(strUser)) then
objUser.SetPassword strPassword
objUser.SetInfo
pt ” -> Password changed for ” & strUser
end if
end if
next

end sub

sub restartsvc(strService)
Dim objWMIService, objItem, objService
Dim colListOfServices, strComputer, intSleep
strComputer = “.”
intSleep = 7000 ‘7 seconds
On Error Resume Next
Set objWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!” _
& strComputer & “rootcimv2”)
Set colListOfServices = objWMIService.ExecQuery _
(“Select * from Win32_Service Where Name =”_
& strService & ” “)
For Each objService in colListOfServices
objService.StopService()
wscript.sleep intSleep
if objService.State = “Stopped” then
pt ” -> The “& strService & ” service has Stoped”
objService.StartService()
end if
wscript.sleep intSleep
if objService.State = “Stopped” then
pt “——Could not start the service: ” & strService & “——”
wscript.quit
else
pt ” -> The “& strService & ” service has Started”
end if
Next
end sub

sub pt(txtmsg)
if verbose = 1 then
wscript.echo txtmsg
end if
end sub

sub deleteitself()
‘since this script contains admin password, we should delete it!
set fso = createObject(“Scripting.FIleSystemObject”)
fso.deleteFile wscript.ScriptFullName
end sub

  1. No comments yet.
  1. No trackbacks yet.

VAMOVE *

*