New to PowerShell - 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.

$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.

Related

-Property time / Not showing time. PowerShell

I have a quick question about powershell. If I write $event1 = Get-EventLog system | Where-Object {$_.EventID -eq 6006} | Select-Object -first 1 -Property Index it only displays the index. Which is right... But if I write $event1 = Get-EventLog system | Where-Object {$_.EventID -eq 6006} | Select-Object -first 1 -Property Time The time is not showing up... Just a blank row. What am I doing wrong. I fond nothing on the internet about that probem. So maybe you could help me. I am a beginner btw :)
As commented, there is no property called Time. Use TimeGenerated or TimeWritten instead.
$event1 = Get-EventLog System | Where-Object {$_.EventID -eq 6006} | Select-Object -First 1 -Property TimeGenerated
However, the docs say that
"Get-EventLog uses a Win32 API that is deprecated. The results may not be accurate. Use the Get-WinEvent cmdlet instead."
To change to Get-WinEvent the following gets you what you want:
$event1 = Get-WinEvent -LogName System | Where-Object {$_.Id -eq 6006} | Select-Object -First 1 -Property TimeCreated

Powershell script doesn't return event log results based on where clause

I am new to powershell and found examples to display messages from the event log. The -match and -eq property don't seem to be working. Below is my script which does not return any results. When I use -notmatch or -ne it displays correctly. I’ve tried many different variations of the code, but nothing has worked.
$sysEvent = Get-EventLog -LogName Application -Newest 300
$sysError = $sysEvent | where {$_entryType -match "Information"}
$sysError | Sort-Object EventID | Format-Table EventID, EntryType, Source, TimeWritten, Message -AutoSize

Add Filter to Get-EventLog (server side), returning only newest N records

Is there any way to filter event log entries using PowerShell before retrieving them?
i.e.
Instead of:
[string[]]$IgnoredSources = 'SomeValue','SomeOtherValue'
Get-Eventlog -LogName $MyLog -ComputerName $MyComputer `
| ?{$IgnoredSources -notcontains $_.Source} `
| Sort-Object TimeGenerated -Descending `
| Select-Object -First 10
Something like:
Get-Eventlog -LogName $MyLog -ComputerName $MyComputer `
-Filter {(Source -ne 'SomeValue') -and (Source -ne 'SomeOtherValue')} `
-Newest 10
More info
I'm aware that I can add a where-object statement to filter the results pulled back; but that's less efficient than filtering on the server side, and means that commands such as -Newest 100 won't necessarily return 100 results once filtered (i.e. I'd have to pull back the entire event log to ensure that I'd get the latest
I'm also aware that for dates this is possible via the -After and -Before attributes, and that it's possible to provide a list of -Username's and -Source's to limit to those. However if I want to exclude 1 source, or filter on a range of event ids, there seems to be no way at present.
I've looked into using Get-WmiObject instead of Get-EventLog, but whilst this allows the filtering to take place server side, I couldn't determine a way to limit the number of results returned (i.e. returned to my machine before sorting then using select-object's -first to then filter down the results).
Get-WmiObject Win32_NTLogEvent -ComputerName $MyComputer `
-filter "(logfile='$MyLog') and (sourcename != 'SomeValue') and (sourcename != 'SomeOtherValue') " `
| Sort-Object TimeGenerated -Descending `
| Select-Object -First 10
How about Get-WinEvent? Something like this:
Get-WinEvent -ComputerName $MyComputer -MaxEvents 100 -FilterHashtable #{
LogName=$MyLog;
ID=$MyID;
<# etc. #>
}

Powershell List of Computers

How can I make this use a list of servers
Tried doing $ComputerList = gc <list location> but it doesnt seem to be working
correctly with one computer
$Start = (Get-Date).AddMinutes(-120)
$ComputerList = $env:ComputerName
$Events = gc C:\Temp\ErrorCodes.txt
# Getting all event logs
Get-EventLog -AsString -ComputerName $Computername |
ForEach-Object {
# write status info
Write-Progress -Activity "Checking Eventlogs on \\$ComputerName" -Status $_
# get event entries and add the name of the log this came from
Get-EventLog -LogName $_ -EntryType Error, Warning -After $Start -ComputerName $ComputerName -ErrorAction SilentlyContinue |
Add-Member NoteProperty EventLog $_ -PassThru | Where-Object {$Events -contains $_.eventid}
} |
# sort descending
Sort-Object -Property EventLog |
# select the properties for the report
Select-Object EventLog, EventID, TimeGenerated, EntryType, Source, Message
# output into grid view window
Out-GridView -Title "All Errors & Warnings from \\$Computername"
Force it to be an array.. Otherwise it will come in as a string if there is only one item
$ComputerList = #(gc c:\folder\computerlist.txt)
PowerShell: How can I to force to get a result as an Array instead of Object

Exclude get-eventlog (powershell)

I have a report that gets generated by a script in powershell, it reports to me the event-logs of multiple servers. I didn't make the script, my former colleague did this. I'm new to powershell and just started to learn a bit about it.
I want to exclude a Citrix error event (EventID 110) that doesn't do any harm. (has to do with Session Reliability)
get-eventlog-log application-computername
$server -EntryType error-after $d |select eventid,machinename,entrytype, message, source, timegenerated | ConvertTo-Html
-Head $h -Title "Rapport Server eventlogs" | out-file -append $Report
get-eventlog -log application -computername $server -EntryType error -after $d |select eventid,machinename,entrytype, message, source, timegenerated| where {$_.eventid -ne 110} | ConvertTo-Html -Head $h -Title "Rapport Server eventlogs" | out-file -append $Report