Hello all,
I am currently working on a powershell script with a Windows forms front end to allow users to change their own SQL password. Essentially, they would enter their SQL username and current password, as well as the new password. I've got everything working up until the point of actually changing the password. From various other posts I've created the following function to handle this:
Function SetSQLPwd
{
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null
$Server = New-Object ("Microsoft.SqlServer.Management.Smo.Server") $global:Instance
$server.ConnectionContext.LoginSecure = $false
$securePassword = ConvertTo-SecureString $global:oldPassword -AsPlainText –Force
$server.ConnectionContext.set_Login($global:userName)
$server.ConnectionContext.set_securePassword($securePassword)
$SQLuser=$server.logins | ? {$_.Name -eq $global:username}
$SQLuser.ChangePassword($global:newPwd)
$SQLuser.Alter()
$SQLuser.refresh()
}
What i'm wanting it to do is to connect to the SQL instance using the current username and password, and then change it to the new username and password.The error I'm getting is as follows:
Exception calling "ChangePassword" with "1" argument(s): "Change password faile
d for Login 'USER'. "
At line:28 char:30
+ $SQLuser.ChangePassword <<<< ($global:newPwd)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
In the above error, USER is replaced by the correct username when the script is ran. I'd like to avoid having to use an SA or elevated rights account to make the script work if at all possible. Anyone see what I'm doing wrong or have any suggestions?