logo

Change Service Account Passwords

BuckWoodyMicrosoftSQLServerTea posted on Monday, July 30, 2007 5:42 PM

Is there a way to change a service account password using PowerShell? I have
a VB Script that will do it, but it is a bit clunky. Thanks!
reply


well, show the vb code and we will translate to powershell for you ...

IT Staff posted on Monday, July 30, 2007 9:07 PM

well, show the vb code and we will translate to powershell for you ...in
shorter forms
reply

I'm not sure translating line by line is the best method - I wasn't sure if

BuckWoodyMicrosoftSQLServerTea posted on Monday, July 30, 2007 9:26 PM

I'm not sure translating line by line is the best method - I wasn't sure if
there was a more direct route. At any rate, here's the VB code:
Set oArgs = WScript.Arguments


' Verify the inputs.

If oArgs.Count <> 5 Then
WScript.Echo "Usage: ChangeSQLServiceAccounts.vbs MachineName
InstaNcename ServiceType SQLAccount SQLPassword"
WScript.Echo "ServiceType = 1 (SQLServer), 2 (Agent), 3
(FTE), 4 (DTS), 5 (AS), 6 (RS), 7 (Browser)"
WScript.Echo "Example: ChangeSQLServiceAccounts.vbs .
MSSQLServer 1 BuiltIn\System NULL"
WScript.Quit(1)
Else

' Load the inputs into variables.

strComputer = oArgs(0)
strInstanceName = oArgs(1)
strServiceType = oArgs(2)
strAccountName = oArgs(3)
strPassword = oArgs(4)
End If

' Get a WMI object for the SQL namespace.

Set objWMIService = GetObject("winmgmts:" &

' Get an instance for this specific service.

Set objSQLService = objWMIService.Get("SqlService.ServiceName=""" &
strInstanceName & """,SQLServiceType=" & strServiceType)

' Obtain an InParameters object specific to the SQLService.SetServiceAccount
method.

Set objInParam =
objSQLService.Methods_("SetServiceAccount").inParameters.SpawnInstance_()

' Add the input parameters to the input object.

objInParam.Properties_.item("ServiceStartName") = strAccountName
objInParam.Properties_.item("ServiceStartPassword") = strPassword

' Call the SetServiceAccount method, and pass in the input object.

Set objOutParams = objSQLService.ExecMethod_("SetServiceAccount", objInParam)

'Check the return to see whether there were any errors.

If objOutParams.ReturnValue = 0 Then
Wscript.Echo "The service account was changed to " & strAccountName
Else
Wscript.Echo "Could not change the service account to " & strAccountName
& " due to error " & objOutParams.ReturnValue
End If
reply

Change Service Account Passwords

Marco Shaw posted on Tuesday, July 31, 2007 10:31 AM

Whithin this class:
$class=[WMICLASS]'\\.\root\Microsoft\SqlServer\ComputerManagement:SqlService'

There's a method named setserviceaccount().

I've not used WMI enough, especially with methods, to figure out how to
invoke the method in this case.

Doing $class.psbase gives me a invokemethod() method, but I've not been
able to figure out the syntax or whether I'm on the right track.

Marco
reply

Change Service Account Passwords

BuckWoodyMicrosoftSQLServerTea posted on Tuesday, July 31, 2007 2:02 PM

Answered my own question, thought I'd share the script:

$class = Get-WmiObject -computername "SQLVM03-QF59YPW" -namespace
root\Microsoft\SqlServer\ComputerManagement -class SqlService
Server Agent in my case):
stop-service -displayName $class[6].DisplayName
passwords. At your own risk here!
$class[6].SetServiceAccount("account", "password")
start-service -displayName $class[6].DisplayName
reply

 

Didn't Find The Answer You Were Looking For?

View Powershell Posts   Ask A New Question

EggHeadCafe has experts online right now that may know the answer to your question.  We pay them a bonus for answering as many questions as they can.  So, why not help them and yourself by becoming a member (free) and ask them your question right now?
Ask Question In Live Forum

If you have an OpenID and do not want to become a member of the EggHeadCafe forum, you can also sign on to Chat Chaos and post your question to our real time Silverlight chat application.
Ask Question In Chat Chaos

Previous Powershell conversation.

Deployment Server    .NET Distributed Applications    .NET Framework    ADO.NET    ASP.NET    ASP.NET Security    ASP.NET Web Controls    ASP.NET Web Services    .NET CLR    .NET Compact Framework    .NET Drawing GDI+    .NET COM Interop    .NET Microframework    .NET Microframework Porting    .NET Performance    .NET Web Services    .NET Windows Forms    .NET WinForms Controls    .NET    C#    VB.NET    VC++.NET    .NET Security    Xml    JScript    VBScript    WSH    Smart Phone Developer    VB COM    VB Controls    VB Crystal    VB DataBase ADO    VB Syntax    VB Vista Compatibility    VB WinAPI    VC ATL    VC Debugger    VC Language    VC MFC    VC STL    Visio Developer VBA    Visual Studio .NET Debugging    Powershell    WindowsCE Embedded VC    Xml    Xsl   






  $1000 Contest    [)ia6l0 iii - $228  |  Jonathan VH - $161  |  Huggy Bear - $135  |  F Cali - $95  |  egg egg - $94  |  more Advertise  |  Privacy  |   (c) 2010