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

时间:2017-01-11 17:21:28

标签: powershell passwords securestring write-host

我正在编写一个小型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

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

1 个答案:

答案 0 :(得分:1)

在这一行中,您正在输入密码:

$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'."
相关问题