0

May I know how to properly export this script to CSV?

Try {

    Invoke-Command -scriptblock {Get-EventLog System -After "7/8/2017" -Before "07/28/2017" | 
    Where-Object {$_.EventID -eq "50" -or $_.EventID -eq "51" -or $_.EventID -eq "55" -or $_.EventID -eq "57" -or $_.EventID -eq "6008"} | 
    FT -Property Machinename, TimeWritten, EntryType, Source, EventID, Message -AutoSize -wrap }  -computername $computer -ErrorAction Stop  
}
Catch {
    Write-Host $Computer "Error/RDC Problem" -ForegroundColor Red
}

Result: enter image description here

Export-CSV command is not working properly when added at the end. It outputs a different set of data. enter image description here

1
  • What do you mean by "not working properly"? I would expect Format-Csv to output all the properties of the objects, whereas Format-Table will often show a subset that will fit on the screen. Format-Csv is going to a format that won't be constrained by the screen width, so it can (and should) include everything. Commented Jul 29, 2017 at 16:13

2 Answers 2

2

Formatting cmdlets like Format-Table don't just change the way the object is displayed, it changes the object itself into something that will display how you want it to. This is part of why it's commonly recommended not to use the formatting cmdlets in scripts or functions.

Instead, you should use the Select-Object cmdlet to limit the number of properties passed to Export-Csv.

Invoke-Command -ComputerName $computer -ErrorAction Stop -ScriptBlock {
    Get-EventLog System -After "7/8/2017" -Before "07/28/2017" | 
    Where-Object { 50, 51, 55, 57, 6008 -contains $_.EventID } | 
    Select-Object -Property MachineName, TimeWritten, EntryType, Source, EventID, Message
}
Sign up to request clarification or add additional context in comments.

1 Comment

It worked! Thank you. Plus points for making the script shorter too, I didn't know you can work with Where-Object like that :)
1

try this

Try {

    Invoke-Command -scriptblock {Get-EventLog System -After "7/8/2017" -Before "07/28/2017" | 
    Where EventID -in ("50", "51", "55", "57", "6008") | 
    select Machinename, TimeWritten, EntryType, Source, EventID, Message }  -computername $computer -ErrorAction Stop |export-csv "c:\temp\result.csv"  
}
Catch {
    Write-Host $Computer "Error/RDC Problem" -ForegroundColor Red
}

or may be simply like this :

Try 
{
    Get-EventLog System -After "7/8/2017" -Before "07/28/2017" -ComputerName $computer | 
    Where EventID -in ("50", "51", "55", "57", "6008") | 
    select Machinename, TimeWritten, EntryType, Source, EventID, Message |export-csv "c:\temp\result.csv" -NoType
}
Catch 
{
    Write-Host $Computer "Error/RDC Problem" -ForegroundColor Red
}

1 Comment

I didn't think about bringing the Export-CSV command inside the try block. Thank you for this! +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.