2012-06-22 28 views
8

¿Cómo puedo obtener todo esto para que no solo salga en la pantalla, sino para guardar en un archivo de texto en formato CSV?Mostrar salida en pantalla y en archivo en 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 
} 

He tratado

$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 

y muchos otros formatos, pero siempre me sale el error:

An empty pipe element is not allowed.

Respuesta

24

usar el cmdlet Tee-Object.

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 

Debe usarlo como,

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

Nota: Tiene un alias, tee, lo que es lo mismo que Unix' tee.

+0

que me da el mismo error – Ron

+0

$ OU = Get-ADObject -LDAPFilter "(= objectCategory organizationalUnit)" ' -SearchBase "unidad organizativa = GA, OU = ESTE, DC = corp, DC = chm, DC = com "| Seleccionar distinguishedName ParaCada (OU $ $ En OU) { $ OU.distinguishedName Get-ADComputer -SearchBase $ OU.distinguishedName -SearchScope OneLevel ' -Filter * | Seleccione Nombre } Tee-Object -file c: \ temp \ outfile.txt – Ron

+0

Simplemente ** pipe ** a un comando 'Tee-Object'. Ver mi actualización –

0

An empty pipe element is not allowed.

unos años demasiado tarde, pero que tienen una barra duplicado en la última línea:

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

esto no soluciona por completo el error porque no se puede canalizar directamente desde el bucle foreach. Puede exportarlo de otra manera a un archivo CSV en lugar de a un archivo de texto como lo hace Tee-Object (puede convertir tee-object a csv) de esta manera, muestra los resultados en un archivo CSV e imprime los resultados en la pantalla:

$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 

Esto también se puede hacer con un guión mucho más simple:

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 
Cuestiones relacionadas