Out-File出错:表达式只允许作为管道的第一个元素

时间:2015-01-02 15:26:13

标签: powershell output pipeline

以下代码行给出了管道错误:

$user.name + "|" + $user.DisplayName | Out-File $output -Append
"Managing Group: " | + $_.name + "|" +`
" Group Description: " +  ($_.description -replace "`r`n", "  ") | Out-File $output -Append

我尝试用以下代码替换部分代码:

(& { process{$_.description -replace "`r`n", "  "}} )
(foeach-object{$_.description -replace "`r`n", "  "} )
%{$_.description -replace "`r`n", "  "}

但似乎没有什么能解决这个错误。

错误:

  

表达式仅允许作为管道的第一个元素。在   C:\ AD-User.MonitorAll.ps1:92 char:82   +“组描述:”+($ _。description -replace“r n”,“”)<<<< | Out-File $ output -Append       + CategoryInfo:ParserError:(:) [],ParentContainsErrorRecordException       + FullyQualifiedErrorId:ExpressionsMustBeFirstInPipeline

更新:

我的问题是我错过了“”在“管理组:”之后

$user.name + "|" + $user.DisplayName | Out-File $output -Append
          "Managing Group: " + "|" + $_.name + "|" +`
            " Group Description: " +  ($_.description -replace "`r`n", "  ") | Out-File $output -Append

2 个答案:

答案 0 :(得分:1)

您没有在第二行引用|。另外,尽量避免反引号来逃避换行。我已经重写它以使用子表达式$()

"$($user.name)|$($user.DisplayName)" | Out-File $output -Append
"Managing Group: | $($_.name)| Group Description: $($_.description -replace "`r`n", "  ")" | Out-File $output -Append

答案 1 :(得分:0)

我错过了#34; "超过我的一个分隔符。谢谢@Shawn Melton。你说的那一刻" |分离器"我看了一眼,发现自己有一个错字。