有没有办法在不创建对象 $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)
}