如何在 powershell 中正确管道对象以避免“表达式仅允许作为管道的第一个元素”错误

Dro*_*ick 4 windows powershell

有没有办法在不创建对象 $obj 的情况下执行以下操作?

$obj = New-Object System.Security.Principal.NTAccount("Students") ; $obj.Translate([System.Security.Principal.SecurityIdentifier]).Value
Run Code Online (Sandbox Code Playgroud)

我尝试过管道,如下所示:

New-Object System.Security.Principal.NTAccount("Students") | $_.Translate([System.Security.Principal.SecurityIdentifier]).Value
Run Code Online (Sandbox Code Playgroud)

但出现以下错误:

Expressions are only allowed as the first element of a pipeline.
At line:1 char:128
+ New-Object System.Security.Principal.NTAccount("Students") | $_.Translate([System.Security.Principal.SecurityIdentifi
er]).Value <<<<
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline
Run Code Online (Sandbox Code Playgroud)

提前致谢!

Rya*_*ies 5

$(New-Object System.Security.Principal.NTAccount("Students")).Translate([System.Security.Principal.SecurityIdentifier]).Value
Run Code Online (Sandbox Code Playgroud)