2012-06-22 2 views
8

이 모든 것을 화면에 출력 할뿐만 아니라 CSV 형식의 텍스트 파일로 저장할 수 있습니까?출력을 PowerShell의 화면 및 파일에 표시

$OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" ` 
    -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chm,DC=com" | Select distinguishedName 
ForEach ($OU In $OUs) 
{ 
    $OU.distinguishedName 
    Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel ` 
     -Filter * | Select Name 
} 

나는

$OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" ` 
    -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chartercom,DC=com" | Select distinguishedName 
ForEach ($OU In $OUs) 
{ 
    $OU.distinguishedName 
    Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel ` 
     -Filter * | Select Name 
} | | export-CSV c:\temp\outfile.csv –noType 

그리고 많은 다른 형식

을 시도,하지만 난 항상 오류가 발생합니다 :

An empty pipe element is not allowed.

답변

24

Tee-Object cmdlet를 사용합니다.

The Tee-Object cmdlet enables you to display data in the Windows PowerShell window and to save that same data to a text file, all with a single command.

dir | Tee-Object -file dir.txt 

당신이 좋아하는 그것을 사용한다

ForEach ($OU In $OUs) 
{ 
    $OU.distinguishedName 
    Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel ` 
     -Filter * | Select Name 
} | Tee-Object -file c:\temp\outfile.txt 

참고 : 그것은 유닉스 'tee과 동일 별명, tee을 가지고 있습니다.

} | | export-CSV c:\temp\outfile.csv –noType 

완전히 오류를 수정하지 않습니다 당신이 foreach 루프에서 직접 파이프 할 수 없기 때문에 :

+0

을 – Ron

+0

$의 OU = GET-ADObject -LDAPFilter "(수 objectcategory = organizationalUnit가)"' -SearchBase "OU = GA, OU = EAST, DC = 회사, DC = Chm, DC = com "| 구분 이름 선택 ForEach ($ OU $ OUs) { $ OU.distinguishedName Get-ADComputer -SearchBase $ OU.distinguishedName -SearchScope OneLevel ' -Filter * | 이름 선택 } 티 - 개체 - 파일 c : \ temp \ outfile.txt – Ron

+0

단지 ** 파이프 ** '티 - 개체'명령. 내 업데이트를 참조하십시오. –

0

An empty pipe element is not allowed.

몇 년은 너무 늦게하지만 당신은 마지막 줄에 중복 바있다. 이런 방식으로 Tee-Object와 같은 텍스트 파일 대신 CSV로 다른 방법으로 내보낼 수 있습니다 (CSV 파일로 변환 할 수 있음). 결과는 CSV 파일로 출력되고 결과는 화면에 인쇄됩니다.

$OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" ` 
    -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chartercom,DC=com" | Select distinguishedName 

$results = @() 
ForEach ($OU In $OUs) 
{ 
    $OU.distinguishedName 
    Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel ` 
     -Filter * | Select Name 
} $results | Export-Csv c:\temp\outfile.csv -NoTypeInformation 
write-host $results 

이 는 훨씬 간단한 스크립트를 사용하여 수행 할 수 있습니다 날 같은 오류를 제공

Get-ADComputer -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chartercom,DC=com" -SearchScope OneLevel -Filter * | Select Name | Export-Csv c:\temp\outfile.csv -NoTypeInformation 
Import-Csv c:\temp\outfile.csv 
관련 문제