简体   繁体   中英

Unable to change password of a VM through Azure Automation

I have a created a Powershell script with Function "RandomPassword" to randomly generate a password. But, I am unable to set the user password to randomly generated password. Below is the PS script and error I am encountering -

PSScript -

Function RandomPassword {

$ABC="A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
$abc="a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"
$char="!","@","#","$","%","*"
$num="1","2","3","4","5","6","7","8","9","0"

$Upper=Get-Random -count 1 -InputObject $ABC
$Lower=Get-Random -count 5 -InputObject $abc
$Char=Get-Random -count 1 -InputObject $char
$Num=Get-Random -count 1 -InputObject $num

$All=Get-Random -Count 7 -InputObject ($Lower+$Char+$Num)
$Raw=$All -join("")

$Temppassword = $Upper + $Raw
$Temppassword
}

$RandomPassword=RandomPassword
$RandomPassword

Set-ADAccountPassword -Reset -NewPassword $RandomPassword -Identity <username>

Error -

Set-ADAccountPassword : Cannot bind parameter 'NewPassword'. Cannot convert the "qkqjh#8w" value of type "System.String" to 
type "System.Security.SecureString".

I have also used this line, but it did not not help and I am receiving same error

$SecPaswd=ConvertTo-SecureString -String $RandomPassword -AsPlainText -Force

Is there a way I could achieve it? Any help would be appreciated.

Based on the error, I would say that the suggested solution would help and it would not be possible to get the same error as $SecPaswd is indeed of the type System.Security.SecureString.

You should of course also change the Parameter in the line:

Set-ADAccountPassword -Reset -NewPassword $RandomPassword -Identity <username>

where $RandomPassword should be changed to $SecPaswd .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM