【问题标题】:Retrieving password with secureString使用secureString检索密码
【发布时间】:2018-12-02 05:29:00
【问题描述】:

我正在创建一个文件,该文件的密码如下所示

[string][ValidateNotNullOrEmpty()]$secureStringPwd = "password123"
$secpasswd = ConvertTo-SecureString $secureStringPwd -AsPlainText -Force
$secureStringText = $secpasswd | ConvertFrom-SecureString 
Set-Content "C:\folder\user.txt" $secureStringText

现在我正在尝试通过以下方式检索密码

$Password = Get-Content "C:\folder\user.txt" | ConvertFrom-SecureString 

$creds = New-Object System.Management.Automation.PSCredential ("user", $Password)
$creds.Password
$creds.UserName

但我收到如下错误:

New-Object : Cannot find an overload for "PSCredential" and the argument count: "2".
At line:1 char:10
+ $creds = New-Object System.Management.Automation.PSCredential ("user ...
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

【问题讨论】:

标签: powershell powershell-3.0 powershell-4.0


【解决方案1】:

您需要将文本文件中的密码转换回安全字符串

$Password = Get-Content "C:\folder\user.txt" | ConvertTo-SecureString

您还需要使用网络凭据将密码转换为纯文本

$creds.GetNetworkCredential().Password

这是一个工作示例

[string][ValidateNotNullOrEmpty()]$secureStringPwd = "password123"
$secpasswd = ConvertTo-SecureString $secureStringPwd -AsPlainText -Force
$secureStringText = $secpasswd | ConvertFrom-SecureString 
Set-Content "C:\folder\user.txt" $secureStringText

$Password = Get-Content "C:\folder\user.txt" | ConvertTo-SecureString 

$creds = New-Object System.Management.Automation.PSCredential ("user", $Password)
$creds.GetNetworkCredential().Password
$creds.UserName

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多