Назад | Перейти на главную страницу

Как правильно передать объект в оболочку PowerShell, чтобы избежать ошибки «Выражения разрешены только как первый элемент конвейера»

Есть ли способ сделать следующее без создания объекта $ 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

Заранее спасибо!

$(New-Object System.Security.Principal.NTAccount("Students")).Translate([System.Security.Principal.SecurityIdentifier]).Value

Если вы хотите использовать конвейер, используйте командлет ForEach-Object.

 New-Object System.Security.Principal.NTAccount("Students") | ForEach-Object {

    $_.Translate([System.Security.Principal.SecurityIdentifier]).Value)
}