【发布时间】: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
【问题讨论】:
-
如果您想安全地存储凭据而不自己做脏活,请查看我最近对相关问题的回答:What is the best way to store account credentials (especially password) for an automated email script?
-
查看这个 MS GitHub 文档:github.com/dotnet/platform-compat/blob/master/docs/DE0001.md
标签: powershell powershell-3.0 powershell-4.0