![如何正确地在 powershell 中管道传输对象以避免“表达式仅允许作为管道的第一个元素”错误](https://linux22.com/image/801164/%E5%A6%82%E4%BD%95%E6%AD%A3%E7%A1%AE%E5%9C%B0%E5%9C%A8%20powershell%20%E4%B8%AD%E7%AE%A1%E9%81%93%E4%BC%A0%E8%BE%93%E5%AF%B9%E8%B1%A1%E4%BB%A5%E9%81%BF%E5%85%8D%E2%80%9C%E8%A1%A8%E8%BE%BE%E5%BC%8F%E4%BB%85%E5%85%81%E8%AE%B8%E4%BD%9C%E4%B8%BA%E7%AE%A1%E9%81%93%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%85%83%E7%B4%A0%E2%80%9D%E9%94%99%E8%AF%AF.png)
有没有办法在不创建对象 $obj 的情况下执行以下操作?
$obj = New-Object System.Security.Principal.NTAccount("Students") ; $obj.Translate([System.Security.Principal.SecurityIdentifier]).Value
我曾尝试过管道,像这样:
New-Object System.Security.Principal.NTAccount("Students") | $_.Translate([System.Security.Principal.SecurityIdentifier]).Value
但出现以下错误:
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
提前致谢!
答案1
$(New-Object System.Security.Principal.NTAccount("Students")).Translate([System.Security.Principal.SecurityIdentifier]).Value
答案2
如果您想坚持使用管道,请使用 ForEach-Object cmdlet。
New-Object System.Security.Principal.NTAccount("Students") | ForEach-Object {
$_.Translate([System.Security.Principal.SecurityIdentifier]).Value)
}