2012-06-22 12 views
8

Jak mogę to wszystko uzyskać, nie tylko wyświetlając obraz na ekranie, ale również zapisując plik tekstowy w formacie CSV?Pokaż dane wyjściowe na ekranie iw pliku w programie 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 
} 

Próbowałem

$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 

I wiele innych formatów, ale zawsze pojawia się błąd:

An empty pipe element is not allowed.

Odpowiedz

24

Użyj 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 

Należy go używać jak,

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

Uwaga: Posiada alias tee, która jest taka sama jak Unix”tee.

+0

że daje mi ten sam błąd – Ron

+0

$ OU = Get-ADObject -LDAPFilter "(objectCategory = organizationalUnit)" ' -SearchBase„OU = GA, OU = EAST, DC = corp, DC = chm, DC = com "| Wybierz distinguishedName foreach ($ w $ OU OU) { $ OU.distinguishedName Get-ADComputer -SearchBase $ OU.distinguishedName -SearchScope OneLevel ' -Filter * | Wybierz nazwę } Tee-Object -File c: \ temp \ outfile.txt – Ron

+0

Po prostu ** pipe ** to do polecenia 'Tee-Object'. Zobacz moją aktualizację. –

0

An empty pipe element is not allowed.

Kilka lat za późno, ale trzeba duplikat poprzeczkę w ostatnim wierszu:

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

nie rozwiąże całkowicie błąd, ponieważ nie można rura bezpośrednio z pętli foreach. Możesz wyeksportować go w inny sposób do pliku CSV zamiast pliku tekstowego, takiego jak Tee-Object (w ten sposób możesz przekonwertować tee-obiekt na csv), wyświetla wyniki w pliku CSV i wyświetla wyniki na ekranie:

$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 

ten może być również wykonane ze znacznie prostszą skryptu:

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 
Powiązane problemy