powershell - Powershell:使用 New-Object cmdlet 创建 PSCredential 对象时出错
问题描述
我正在使用 PS 版本 5.0
尝试使用 New-Object cmdlet 创建 PSCredential 对象时,出现此错误:
New-Object : Cannot find an overload for "PSCredential" and the argument count: "2".
我的PS代码如下:
[string][ValidateNotNullOrEmpty()] $secureStringPwd = "pass"
$secureStringPwd = $secureStringPwd|ConvertTo-SecureString -AsPlainText -Force
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "svcacct", $secureStringPwd
如您所见,我正在将密码转换为安全字符串,但仍然出现错误
解决方案
试试这个方法
[ValidateNotNullOrEmpty()] $secureStringPwd = "pass"
$secureStringPwd = $secureStringPwd|ConvertTo-SecureString -AsPlainText -Force
($creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "svcacct", $secureStringPwd)
# Results
UserName Password
-------- --------
svcacct System.Security.SecureString
或者这样
[string][ValidateNotNullOrEmpty()]$secureStringPwd = "pass"
$secpasswd = ConvertTo-SecureString $secureStringPwd -AsPlainText -Force
($creds = New-Object System.Management.Automation.PSCredential ("username", $secpasswd))
# Results
UserName Password
-------- --------
username System.Security.SecureString
推荐阅读
- vbscript - 使用 VBScript 以字节形式发送 UDP
- ios - UIPageControllerDelegate 操作顺序倒退?
- string - 创建一个字符串并存储在一个变量中,然后访问该字符串?
- winapi - 如何通过 OCM_ 消息处理未处理的 Win32 消息反射到子类化控件
- flask - 部署 Flask Web 应用程序:从远程服务器访问
- javascript - Javascript:按值获取索引
- c# - 为什么修改一个会话变量也会修改另一个会话变量?
- postgresql - 我想在 PostgreSQL 中创建数据类型为 money 的列
- r - Ops.factor 中的错误(实际,预测):因子的水平集与包 iml 中的 Featureimp 不同
- c# - 在 xaml 中选中一个复选框以取消选中另一个复选框