Asked By BuckWoodyMicrosoftSQLServerTea
30-Jul-07 09: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