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
'switch'는 같은 주제의 다른 값/범위를 구별해야 할 때만 유용합니다. 이 경우 비교에는 전적으로 주체가 있습니다. 보다 실질적인 향상은 루프에서'Out-File' 문을 제거하고, 바깥 쪽 루프를'ForEach-Object' 루프로 바꾸고 그것을 하나의'Out-File' 문으로 파이프하는 것입니다 :'$ results | ForEach-Object {...} | 출력 파일 $ 출력 '. 이렇게하면 출력 파일에 반복적으로 추가되지 않으므로 전체 성능이 향상됩니다. 그러나,이 경우'$ user'는'$ _'로 대체되어야합니다. –
@AnsgarWiechers 네, 전환에 대해 말하는 것은 사실입니다. 이 경우 나는 그것을 설명하기 위해'Switch ($ true)'를 사용했을 것이다. 또한이 질문에 답하려고 시도했습니다. 지난 몇 차례 사람들을 위해 코드를 최적화하려고 시도했습니다. – Matt
설명해 주셔서 감사합니다. 나는 Powershell을 여전히 배우고 있으며 실제로 사용되지 않을 때 Append가 비교에서 무엇을했는지를 완전히 알지 못했습니다. 이제 네가 설명했던 것을 정말 고맙게 생각한다. – narue1992