Skip to content

$cs.UsrMgr_ChangePassword

Changes the password of a local user account. If the specified user does not exist, the function returns silently without error. Throws an exception on other failures.

$cs.UsrMgr_ChangePassword(string userName, string password)

Name of the local user whose password will be changed. If the user does not exist, no error is raised.

The new password, in clear text.

None.

Rotate a local service account’s password as part of an application upgrade:

Terminal window
if ($cs.UsrMgr_ExistLocalUserAccount('administrator')) {
$cs.Job_WriteLog("Changing password for administrator")
$cs.UsrMgr_ChangePassword('administrator', 'VerySuperSecretPassw0rd!')
}

Generate a random password, store it in a variable, and apply it to a service account before restarting the service that runs under it:

Terminal window
Add-Type -AssemblyName System.Web
$newPassword = [System.Web.Security.Membership]::GeneratePassword(16, 4)
if ($cs.UsrMgr_ExistLocalUserAccount('svc_capaagent')) {
$cs.Job_WriteLog("Rotating password for svc_capaagent")
$cs.UsrMgr_ChangePassword('svc_capaagent', $newPassword)
Set-Service -Name 'CapaAgentService' -Credential (New-Object System.Management.Automation.PSCredential('svc_capaagent', (ConvertTo-SecureString $newPassword -AsPlainText -Force)))
Restart-Service -Name 'CapaAgentService' -Force
}

$cs.UsrMgr_CreateLocalUser $cs.UsrMgr_ExistLocalUserAccount