Pergunta

I'm playing around with a PS script to pull values of a PC using Get-ADComputer (this is to be part of a larger script, but I'm tackling it in chunks.) The code is:

Get-ADComputer -Filter * -SearchBase "OU=Computers,DC=domain,DC=local" -Properties Name,lastLogonTimestamp | ForEach-Object {$_.Name,[DateTime]::FromFileTime($_.lastLogonTimestamp)} | Out-File time.csv

When I export to CSV to above line, I get each entry on a new line:

Thursday, October 31, 2013 8:49:26 AM
COMPUTER1
Sunday, March 02, 2014 1:34:37 PM
COMPUTER2

I'd like to get the output to read (pretending "|" is a new column in the same row):

COMPUTER1 | Thursday, October 31, 2013 8:49:26 AM
COMPUTER2 | Sunday, March 02, 2014 1:34:37 PM

Also, I got to the point of using Out-File because Export-Csv was giving me the length of the computer name when I ran the above script. Thanks for the help!

Foi útil?

Solução

Instead of doing a ForEach, use Select instead, and toss an impromptu hash table in to get your LastLogonTime converted like this:

Get-ADComputer -Filter * -SearchBase "OU=Computers,DC=domain,DC=local" -Properties Name,lastLogonTimestamp | select Name,@{l="Last Logon";e={[DateTime]::FromFileTime($_.lastLogonTimestamp)}}|Export-Csv time.csv -NoTypeInformation
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top