2012-06-22 23 views
8

Làm thế nào tôi có thể nhận tất cả điều này không chỉ xuất trên màn hình mà còn lưu vào tệp văn bản ở định dạng CSV?Hiển thị đầu ra trên màn hình và trong tệp trong 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 
} 

Tôi đã thử

$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 

Và nhiều định dạng khác, nhưng tôi luôn luôn nhận được lỗi:

An empty pipe element is not allowed.

Trả lời

24

Sử dụng 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 

Bạn nên sử dụng nó như thế nào,

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

Lưu ý: Nó có một bí danh, tee, mà là giống như Unix' tee.

+0

đó mang lại cho tôi những lỗi tương tự – Ron

+0

$ OU = Get-ADObject -LDAPFilter "(objectCategory = organizationalUnit)" ' -SearchBase "OU = GA, OU = EAST, DC = corp, DC = chm, DC = com "| Chọn phân biệtName ForEach ($ OU trong $ OUs) { $ OU.distinguishedName Nhận-ADComputer -SearchBase $ OU.distinguishedName -SearchScope OneLevel ' -Filter * | Chọn Tên } Tee-Object -file c: \ temp \ outfile.txt – Ron

+0

Chỉ ** pipe ** nó vào lệnh 'Tee-Object'. Xem cập nhật của tôi. –

0

An empty pipe element is not allowed.

Một vài năm quá muộn nhưng bạn có một thanh trùng lặp trong dòng cuối cùng:

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

Điều đó không sửa lỗi hoàn toàn bởi vì bạn không thể đường ống trực tiếp từ vòng lặp foreach. Bạn có thể xuất một cách khác sang CSV thay vì tệp văn bản như Tee-Object (bạn có thể chuyển đổi đối tượng tee thành csv) theo cách này, kết quả sẽ xuất ra tệp CSV và in kết quả vào màn hình:

$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 

này cũng có thể được thực hiện với một kịch bản đơn giản hơn nhiều:

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 
Các vấn đề liên quan