无法通过Azure自动化更改VM的密码

时间:2017-07-21 10:29:05

标签: powershell

我创建了一个带有函数“RandomPassword”的Powershell脚本来随机生成密码。但是,我无法将用户密码设置为随机生成的密码。以下是我遇到的PS脚本和错误 -

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>

错误 -

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

我也使用了这一行,但它没有帮助,我收到同样的错误

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

有没有办法实现它?任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:0)

根据错误,我会说建议的解决方案会有所帮助,并且不可能得到与$SecPaswd确实属于System.Security.SecureString类型相同的错误。

您当然也应该更改行中的参数:

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

其中$RandomPassword应更改为$SecPaswd