Out-File不会显示所有Out-File命令

时间:2015-01-02 13:22:39

标签: powershell output

我只是一个简单的问题。我想要我在文本文档中打印的信息。问题是我唯一看到的印刷是“没有直接报告列出”这一行。为什么会这样?

$results = Get-ADUser -Properties enabled,displayname,sn,name,surname `
             -Filter {name -like "*"} |
           Select name,sn,displayname,enabled,surname
$name    = $user.Name
$owned   = Get-Adgroup -Properties description,managedby `
             -Filter {managedby -eq $name}

foreach ($user in $results) {
  if ($user.enabled -eq $false) {
    if ($owned -eq $Null) {
      $user.name + "|" + $user.DisplayName + "|"  +
        "Managing Group: None Found " + "|" +
        " Group Description: None Provided " | Out-File $output
    } elseif (($description -eq " ") -or ($description -eq $Null)) {
      $user.name + "|" + $user.DisplayName + "|"  + "Managing Group: " +
        $_.name + "|" + " Group Description: None Provided " | Out-File $output
    } else {
      $user.name + "|" + $user.DisplayName + "|"  + "Managing Group: " +
        $_.name + "|" + " Group Description: " +
        ($_.description -replace "`r`n", "  ") | Out-File $output
    }

    $directReports = Get-ADUser -Identity $name -Properties directreports |
                     Select -ExpandProperty directreports

    foreach ($ID in $directReports) {
      if ($ID -ne $Null) {
        $directreports = get-aduser $ID
        "Direct Reports Listed Under User: " + "|" + $directreports.name |
          Out-File $output
      } else {
        "No Direct Reports Listed" | Out-File $output
      }
    }#foreach
  }#if
}#foreach

1 个答案:

答案 0 :(得分:2)

代码中的最后一个输出(在本例中为"No Direct Reports Listed" | Out-File $output)发送到Out-File将是您在文件中看到的内容。在当前设置中,您需要使用-Append开关。来自TechNet

  

-append

     

将输出添加到现有文件的末尾,而不是替换文件内容。

此外,您可以使用默认情况下执行此操作的Add-Content cmdlet。

由于默认情况下Out-FileAdd-Content的编码不同,请注意您可能还需要调整-Encoding

旁注

并非它非常复杂,但当If语句开始失控时,使用switch语句可以更直观地处理。