【问题标题】:System returning name of class instead of variable [duplicate]系统返回类的名称而不是变量[重复]
【发布时间】:2017-01-11 17:21:28
【问题描述】:

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

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

【问题讨论】:

  • 您不能直接将安全字符串打印到屏幕上
  • 感谢@Hackerman,我不知道如何表达我的问题,也找不到其他线程。

标签: powershell passwords securestring write-host


【解决方案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'."

【讨论】:

  • 正如 BenH 所说,您看不到安全字符串。这是设计使然。换句话说,在将$password 转换为安全字符串之前,将其打印出来。
  • 解决了这个问题,非常感谢。我会将其标记为最佳答案,但我认为发布后为时过早,没有选择:)
  • 如果一个答案解决了你的问题,那么你应该接受这个答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-23
相关资源
最近更新 更多