2012-06-22 21 views
8

Come posso ottenere tutto questo non solo per l'output sullo schermo, ma per salvare in un file di testo nel formato CSV?Mostra output su schermo e file in 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 
} 

ho cercato

$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 

e molti altri formati, ma ho sempre ottenere l'errore:

An empty pipe element is not allowed.

risposta

24

Utilizzare la 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 

Si dovrebbe utilizzarlo come,

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

Nota: ha un alias, tee, che è la stessa di Unix' tee.

+0

che mi dà lo stesso errore – Ron

+0

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

+0

Solo ** pipe ** su un comando 'Tee-Object'. Vedi il mio aggiornamento. –

0

An empty pipe element is not allowed.

Alcuni anni troppo tardi, ma avete una barra duplicato nell'ultima riga:

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

questo non risolve completamente l'errore perché non è possibile reindirizzare direttamente dal ciclo foreach. Puoi esportarlo in un altro modo in un file CSV anziché in un file di testo come fa Tee-Object (potresti convertire tee-object in csv) in questo modo, restituisce i risultati in un file CSV e stampa i risultati sullo schermo:

$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 

Questo può essere fatto anche con uno script molto più semplice:

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 
Problemi correlati