Modifying Powershell to display Security Event log statistics for multiple computer

People,

Can anyone here please assist me in how to configure the below script to run on multiple computers ?

Get-Content C:\Users\user\Documents\server_list.txt | ....

Open in new window


When I execute the below script with the log name System it works with no issue, but when I modify it to Security, it does not work ?

$logName = "Security"

#get date
$dateCurrent = Get-Date

#For the Week
$NDays = -7
$dateNDaysAgo = $dateCurrent.AddDays($NDays)

$logLevel = @("Critical", "Warning", "Error")
$logLevelCritical = "Critical"
$logLevelWarning = "Warning"
$logLevelError = "Error"

Write-Host "Log Entries since $dateNDaysAgo"

Get-WinEvent -Logname $logName  | `
where-object {($_.timecreated -gt $date) -and ( ($_.levelDisplayName -eq $logLevelCritical) -or ($_.levelDisplayName -eq $logLevelWarning) -or ($_.levelDisplayName -eq $logLevelError) )  } | `
Group-Object ProviderName, levelDisplayName, ID | `
Sort-Object Count -descending | `
Format-Table Count, Name -auto

Open in new window


Thanks in advance.
LVL 11
Senior IT System EngineerIT ProfessionalAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subash SundharanIT Infrastructure Architect Commented:
In security you mostly find only Information logs, which will be either Audit Success or Audit Failed. So you may not get any result with current code.
Senior IT System EngineerIT ProfessionalAuthor Commented:
Ah I see,

So which section should be modified to reflect success or failure ?

And how to make it working for multiple server object ?
Subash SundharanIT Infrastructure Architect Commented:
Filter using KeywordsDisplayNames
?{$_.KeywordsDisplayNames -match "Audit Failed"}

or

?{$_.KeywordsDisplayNames -match "Audit Success"}

Open in new window


But It's depends what you trying to looks for.. There might be Error/warnings in security log but it's rare. If you want to capture the count for Audit success and failure its possible..
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

arnoldCommented:
Any consideration to consolidate a central repository for the logs, eventlog-forwarder this way you need not have so much local storage allocated ........
and to others point, by the time you run the script, the event could be long gone.

Using SNMP, you can have the events of interest to you proactively appear and generate notification.
While at the same time logging, recording the events in a database, etc......


splunk is one such tool that can pull data from multipe....
Senior IT System EngineerIT ProfessionalAuthor Commented:
Thanks SUbsun, I will give it a try now.

Arnold, Splunk is not free for anything larger than 1 GB. So is there any builtin Microsoft features that can do this event forwarding and notification ?
Senior IT System EngineerIT ProfessionalAuthor Commented:
@Subsun:

it does not work when I tried in my local PC:

$logName = "Security"

#get date
$dateCurrent = Get-Date

#For the Week
$NDays = -7
$dateNDaysAgo = $dateCurrent.AddDays($NDays)

$logLevel = @("Critical", "Warning", "Error")
$logLevelCritical = "Critical"
$logLevelWarning = "Warning"
$logLevelError = "Error"

Write-Host "Log Entries since $dateNDaysAgo"

Get-WinEvent -Logname $logName  | `
	Where-Object {($_.timecreated -gt $date) -and ( ?{$_.KeywordsDisplayNames -match "Audit Failed"} )  } | `
	Group-Object ProviderName, KeywordsDisplayNames, ID | `
	Sort-Object Count -descending | `
	Format-Table Count, Name -auto

Open in new window


There is no result displayed ?
arnoldCommented:
Yes, splunk does what your powershell script....
The data aggregation is done using event forwarding, subscriptions.......

as long as you have a Windows 2008 or newer,
There are more detail through a search coming from the splunk site
Here is MS's basics steps.
https://msdn.microsoft.com/en-us/library/cc748890(v=ws.11).aspx

Dism /online /enable-feature:SNMP
Dism /online /enable-feature:WMIProvider

This will install/activate SNMP and wmi over snmp
You would need to configure one system as an SNMPTRP destination(the one where you plan running this powershell script

Evntwin is the graphical interface to specify which event shoul be sent via snmptrap.
You can select which options you want, then export
If you have the set, you can use evntcmd to load the exported data versus going through the GUI one system at a time.

Using GPO one can push all related SNMP destination, read community, to all AD computers..
The use of evntcmd in a computer configuration as a startup script ........ You can distribute these settings...
Subash SundharanIT Infrastructure Architect Commented:
Made some changes.. Try this..
$logName = "Security"

#get date
$dateCurrent = Get-Date

#For the Week
$NDays = -7
$dateNDaysAgo = $dateCurrent.AddDays($NDays)

Write-Host "Log Entries since $dateNDaysAgo"

Get-WinEvent -Logname $logName |
	Where-Object {$_.timecreated -gt $dateNDaysAgo -and $_.KeywordsDisplayNames -match "Audit Failure"} | 
	Group-Object ProviderName,KeywordsDisplayNames,ID |
	Sort-Object Count -descending |
	Format-Table Count,Name -Auto

Open in new window

Senior IT System EngineerIT ProfessionalAuthor Commented:
Subsun,

Thanks for the reply, however, the script does not work when I execute in my Powershell ISE on my laptop as well as the server logging in as DOMAIN\Administrator.

Get-WinEvent : Could not retrieve information about the Security log. Error: Attempted to perform an unauthorized operation..
At line:12 char:1
+ Get-WinEvent -Logname $logName |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-WinEvent], Exception
    + FullyQualifiedErrorId : LogInfoUnavailable,Microsoft.PowerShell.Commands.GetWinEventCommand
 
Get-WinEvent : There is not an event log on the localhost computer that matches "Security".
At line:12 char:1
+ Get-WinEvent -Logname $logName |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Security:String) [Get-WinEvent], Exception
    + FullyQualifiedErrorId : NoMatchingLogsFound,Microsoft.PowerShell.Commands.GetWinEventCommand

Open in new window

Subash SundharanIT Infrastructure Architect Commented:
Use Run as Administrator option to open ISE or PowerShell console.

and if you want to make it bit faster, use -FilterHashtable parameter, instead of where-object
$logName = "Security"

#get date
$dateCurrent = Get-Date

#For the Week
$NDays = -1
$dateNDaysAgo = $dateCurrent.AddDays($NDays)

Write-Host "Log Entries since $dateNDaysAgo"
$Key = [System.Diagnostics.Eventing.Reader.StandardEventKeywords]::AuditFailure
Get-WinEvent -FilterHashtable @{StartTime=$dateNDaysAgo;Logname=$logName;Keywords=$Key.value__}  |
	Group-Object ProviderName,KeywordsDisplayNames,ID |
	Sort-Object Count -descending |
	Format-Table Count,Name -Auto

Open in new window

Read following article for more details..
https://blogs.technet.microsoft.com/heyscriptingguy/2014/06/03/use-filterhashtable-to-filter-event-log-with-powershell/

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Subash SundharanIT Infrastructure Architect Commented:
Script should suffice the requirement.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.