powershellexport-to-csvget-eventlog

New to PowerShell


I am extremely new to PowerShell I am trying to create a script that will look thought the system event log and pull out the items that match Error, Verbose , and Warnings; Then I want to export them to a CSV file.

I was able to get each of the variables created for the $errorlog, $verboselog, and $warninglog (shown below).

$errorlog = Get-EventLog system -Newest 200 | Where-Object {$_.entryType -Match 'Error'}
$verboselog = Get-EventLog system -Newest 200 | Where-Object {$_.entryType -Match 'Verbose'}
$warninglog = Get-EventLog system -Newest 200 | Where-Object {$_.entryType -Match 'Warning'}

When I go ahead and try export them to one CSV file it just displays the $errorlog. From what I have gather from various websites the command I am using should be working.

$errorlog,$verboselog,$waninglog | Export-CSV -inputobject  -path 'C:\service\test.CSV'

It is tell me that is it missing '-inputobject' so I moved the variables around to look like the following.

Export-CSV -inputobject  $errorlog,$verboselog,$warninglog  -path 'C:\service\test.CSV'

It exported with out error but it didn't display the data I wanted in the file.

I thank you in advance for your help.


Solution

  • $errorlog = Get-EventLog system -Newest 200| Where-Object {$_.entryType -eq 'Error'}
    $verboselog = Get-EventLog system -Newest 200| Where-Object {$_.entryType -eq 'Verbose'}
    $warninglog = Get-EventLog system -Newest 200| Where-Object {$_.entryType -eq 'Warning'}
    $errorlog+$verboselog+$warninglog | Export-Csv C:\service\test.CSV
    

    I replaced the -match with -eq and dropped the inputobject switch. I also changed the commas to plus symbols to concantinate the results. This worked for me.