新对象PSCredential不起作用-语法上使用Unicode标点符号 [英] New Object PSCredential not working - using Unicode punctuation syntactically
问题描述
我试图接受用户名和密码作为Powershell脚本的参数,但是新对象
I'm trying to accept username and password as params to a Powershell script but the new-Object
$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 字符:17 + ... rCredential =新对象– TypeName System.Management.Automation.PSCre ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~ + CategoryInfo:InvalidType:(:) [New-Object],PSArgumentException + FullyQualifiedErrorId:TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
New-object : Cannot find type [â€TypeName System.Management.Automation.PSCredential â€ArgumentList]: verify that the as sembly containing this type is loaded. At C:\ps\login.ps1:14 char:17 + ... rCredential=New-object â€"TypeName System.Management.Automation.PSCre ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
有人知道如何解决这个问题吗?
Anybody got some clues how to solve this?
推荐答案
TypeName
和ArgumentList
参数前面的–
不是连字符,而是EN破折号(U + 2013),并且跳闸解析器.用连字符(-
)替换它们:
The –
in front of the TypeName
and ArgumentList
parameters are not hyphens, but EN dashes (U+2013), and this trips up the parser. Replace them with a hyphen (-
):
'New-object –TypeName System.Management.Automation.PSCredential –ArgumentList ($UserID,$SecurePassword)' -replace '\p{Pd}','-'
\p{Pd}
是" P 失踪, d 灰烬"的Unicode类
\p{Pd}
is the unicode class for "Punctuation, dashes"
编者注:PowerShell可能令人惊讶地是, not 在使用–
(破折号)代替常规ASCII范围时是否存在问题? -
(破折号",技术上为:连字符减)-例如,尝试使用Get-ChildItem –File
.
OP的唯一问题是字符编码,如果您将其转换为常规的破折号",则会碰巧绕过 .
将Unicode破折号转换为ASCII破折号"只是一个权宜之计 :它掩盖了OP的真正问题,即使用Windows PowerShell错误解释的编码来保存文件.如果该文件包含非ASCII字符作为 data ,它仍然会损坏.
Editor's note: PowerShell, perhaps surprisingly, does not have a problem with using –
(en dash) in lieu of the regular ASCII-range -
("dash", technically: hyphen-minus) - try Get-ChildItem –File
, for instance.
The OP's only problem is a character-encoding one, which you happen to bypass if you convert to regular "dashes".
Turning the Unicode en dashes into ASCII "dashes" is only a stopgap: It masks the OP's true problem of having saved a file with an encoding that Windows PowerShell misinterprets. If that file contained non-ASCII characters as data, it would still be broken.
这篇关于新对象PSCredential不起作用-语法上使用Unicode标点符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!