2015-01-02 1 views
0

빠른 질문이 있습니다. 텍스트 문서로 인쇄해야하는 정보를 원합니다. 이 문제는 인쇄본이 "No Direct Reports Listed"라는 단 한 줄인 유일한 문제입니다. 이게 왜 그렇게?Out-File 모든 Out-File 명령을 표시하지 않습니다.

$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 

답변

2

Out-File로 전송이 경우 "No Direct Reports Listed" | Out-File $output 당신의 코드에서 마지막 출력은 파일에 표시되는 내용 일 것이다. 현재 설정에서 -Append 스위치를 사용해야합니다.

-append

TechNet에서 대신 파일의 내용을 대체하는 기존 파일 끝에 출력을 추가한다.

또한 기본값 인 Add-Content cmdlet을 사용할 수도 있습니다.

Out-FileAdd-Content 모두 기본적으로 인코딩이 다르므로 -Encoding도 조정해야 할 수 있습니다.

사이드 참고

되지는 믿을 수 없을만큼 복잡하지만 If 문을 손에서 얻을 시작할 때 switch 문을 사용하는 것이 훨씬 더 직관적으로 처리하는 수.

+0

'switch'는 같은 주제의 다른 값/범위를 구별해야 할 때만 유용합니다. 이 경우 비교에는 전적으로 주체가 있습니다. 보다 실질적인 향상은 루프에서'Out-File' 문을 제거하고, 바깥 쪽 루프를'ForEach-Object' 루프로 바꾸고 그것을 하나의'Out-File' 문으로 파이프하는 것입니다 :'$ results | ForEach-Object {...} | 출력 파일 $ 출력 '. 이렇게하면 출력 파일에 반복적으로 추가되지 않으므로 전체 성능이 향상됩니다. 그러나,이 경우'$ user'는'$ _'로 대체되어야합니다. –

+0

@AnsgarWiechers 네, 전환에 대해 말하는 것은 사실입니다. 이 경우 나는 그것을 설명하기 위해'Switch ($ true)'를 사용했을 것이다. 또한이 질문에 답하려고 시도했습니다. 지난 몇 차례 사람들을 위해 코드를 최적화하려고 시도했습니다. – Matt

+0

설명해 주셔서 감사합니다. 나는 Powershell을 여전히 배우고 있으며 실제로 사용되지 않을 때 Append가 비교에서 무엇을했는지를 완전히 알지 못했습니다. 이제 네가 설명했던 것을 정말 고맙게 생각한다. – narue1992

관련 문제