Avatar of Albert Widjaja
Albert Widjaja
Flag for Australia asked on

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.
PowershellActive DirectoryShell ScriptingOS SecuritySecurity

Avatar of undefined
Last Comment
SubSun

8/22/2022 - Mon
SubSun

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.
Albert Widjaja

ASKER
Ah I see,

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

And how to make it working for multiple server object ?
SubSun

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..
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
arnold

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....
Albert Widjaja

ASKER
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 ?
Albert Widjaja

ASKER
@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 ?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
arnold

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

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

Albert Widjaja

ASKER
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

Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
SubSun

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SubSun

Script should suffice the requirement.