新对象PSCredential的工作不

问题描述:

我试图接受用户名和密码PARAMS到PowerShell脚本,但新物体新对象PSCredential的工作不

$UserID="Name" 
$SecurePassword=convertto-securestring -AsPlainText -Force -String $Password 
New-object –TypeName System.Management.Automation.PSCredential –ArgumentList ($UserID,$SecurePassword) 

给出了一个错误

新对象:无法找到键入[TypeName System.Management.Automation.PSCredential-ArgumentList]:确认 包含此类型的组件已加载。在C:\ ps \ login.ps1:14 char:17 + ... rCredential = New-object - TypeName System.Management.Automation.PSCre ... + ~~~~~~~~~ 〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜 + CategoryInfo:InvalidType:(:) [新对象],PSArgumentException + FullyQualifiedErrorId:TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

任何人有一些线索,如何解决这个问题?

+2

的'-'中的'前面ArgumentList'是不连字符,但EN破折号(U + 2013),用连字符('-')代替它 –

TypeNameArgumentList之前的参数不是连字符,而是EN破折号(U + 2013),并且这将跳过解析器。以连字符(-)替换它们:

'New-object –TypeName System.Management.Automation.PSCredential –ArgumentList ($UserID,$SecurePassword)' -replace '\p{Pd}','-' 

\p{Pd}为Unicode的类 “P unctuation,d灰烬”

+0

是的,Notepad ++保存为UTF-8谢谢 –