系统返回类的名称而不是变量



我正在编写一个小的PowerShell脚本,它允许我从Active Directory中获取用户并随机为他们生成密码。

$Alphabet=$NULL
for ($a=48; $a –le 70; $a++) {
    $Alphabet += ,[char][byte]$a
}
function Get-TempPassword() {
    Param (
        [int]$Length=10,
        [string[]]$Source
    )
    for ($loop=1; $loop –le $length; $loop++) {
        $TempPassword += ($Source | Get-Random) 
    }
    return $TempPassword
}
$Password = Get-TempPassword -Length 10 -Source $Alphabet
$NewPassword = ConvertTo-SecureString -String $Password -AsPlainText -Force
Set-ADAccountPassword -Identity $User -NewPassword $NewPassword
Set-ADUser –Identity $User –ChangePasswordAtLogon $true
Unlock-ADAccount $User | Out-Null
$Name = (Get-ADUser $User -Properties Name).Name
Write-Host "Okay, $Name's new password has been set to '$NewPassword'."

而不是最后一行返回

好的,用户的新密码已设置为"[密码]"。

它回来了

好的,用户的新密码已设置为"System.Security.SecureString"。

我相信它返回该类而不是将其设置为密码,因为我无法将其作为用户的密码登录。我想我忽略了一些东西,但我已经盯着它看了很长时间了,看不到我错过了什么。我也尝试注释掉该行

$NewPassword = ConvertTo-SecureString -String $Password -AsPlainText -Force

它似乎没有帮助,我预计会出错,因为变量不再匹配。

在此行中,您正在设置密码:

$Password = Get-TempPassword -Length 10 -Source $Alphabet

然后把它变成一个安全字符串

$NewPassword = ConvertTo-SecureString -String $Password -AsPlainText -Force

因此,如果您想查看密码,请输出$Password而不是安全字符串$NewPassword

Write-Host "Okay, $Name's new password has been set to '$Password'."

最新更新