added default run mode when variables are missing or not defined
This commit is contained in:
+75
-27
@@ -8,15 +8,17 @@ Gets user logon, logoff, lock, and unlock activity from the Windows Security log
|
|||||||
$Start = '2026-07-03T00:00:00Z'
|
$Start = '2026-07-03T00:00:00Z'
|
||||||
$End = '2026-07-06T00:00:00Z'
|
$End = '2026-07-06T00:00:00Z'
|
||||||
.\Get-UserLogonActivity.ps1
|
.\Get-UserLogonActivity.ps1
|
||||||
|
|
||||||
|
.EXAMPLE
|
||||||
|
.\Get-UserLogonActivity.ps1
|
||||||
#>
|
#>
|
||||||
|
|
||||||
If (-not (Get-Variable -Name Start -ErrorAction SilentlyContinue)) {
|
$StartVariable = Get-Variable -Name Start -ErrorAction SilentlyContinue
|
||||||
Write-Error '$Start must be defined by the caller. Use ISO 8601 UTC format: yyyy-MM-ddTHH:mm:ssZ. Example: 2026-07-03T00:00:00Z'
|
$EndVariable = Get-Variable -Name End -ErrorAction SilentlyContinue
|
||||||
Exit 1
|
$UseDateRange = ($null -ne $StartVariable -and $null -ne $EndVariable)
|
||||||
}
|
|
||||||
|
|
||||||
If (-not (Get-Variable -Name End -ErrorAction SilentlyContinue)) {
|
If (($null -ne $StartVariable) -xor ($null -ne $EndVariable)) {
|
||||||
Write-Error '$End must be defined by the caller. Use ISO 8601 UTC format: yyyy-MM-ddTHH:mm:ssZ. Example: 2026-07-06T00:00:00Z'
|
Write-Error '$Start and $End must either both be defined or both be omitted. If omitted, the script returns the latest 15 activity events.'
|
||||||
Exit 1
|
Exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -176,31 +178,33 @@ Function ConvertTo-UserLogonActivity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Try {
|
|
||||||
$StartDateTime = ConvertTo-ActivityDateTime -Value $Start -ParameterName 'Start'
|
|
||||||
$EndDateTime = ConvertTo-ActivityDateTime -Value $End -ParameterName 'End'
|
|
||||||
}
|
|
||||||
Catch {
|
|
||||||
Write-Error $_.Exception.Message
|
|
||||||
Exit 1
|
|
||||||
}
|
|
||||||
|
|
||||||
If ($EndDateTime -le $StartDateTime) {
|
|
||||||
Write-Error "-End must be later than -Start."
|
|
||||||
Exit 1
|
|
||||||
}
|
|
||||||
|
|
||||||
$Filter = @{
|
$Filter = @{
|
||||||
LogName = 'Security'
|
LogName = 'Security'
|
||||||
Id = @(4624, 4634, 4647, 4800, 4801)
|
Id = @(4624, 4634, 4647, 4800, 4801)
|
||||||
StartTime = $StartDateTime.LocalDateTime
|
|
||||||
EndTime = $EndDateTime.LocalDateTime
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Try {
|
If ($UseDateRange) {
|
||||||
|
Try {
|
||||||
|
$StartDateTime = ConvertTo-ActivityDateTime -Value $Start -ParameterName 'Start'
|
||||||
|
$EndDateTime = ConvertTo-ActivityDateTime -Value $End -ParameterName 'End'
|
||||||
|
}
|
||||||
|
Catch {
|
||||||
|
Write-Error $_.Exception.Message
|
||||||
|
Exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
If ($EndDateTime -le $StartDateTime) {
|
||||||
|
Write-Error "-End must be later than -Start."
|
||||||
|
Exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
$Filter.StartTime = $StartDateTime.LocalDateTime
|
||||||
|
$Filter.EndTime = $EndDateTime.LocalDateTime
|
||||||
|
|
||||||
|
Try {
|
||||||
$Events = Get-WinEvent -FilterHashtable $Filter -ErrorAction Stop
|
$Events = Get-WinEvent -FilterHashtable $Filter -ErrorAction Stop
|
||||||
}
|
}
|
||||||
Catch {
|
Catch {
|
||||||
If ($_.FullyQualifiedErrorId -like '*NoMatchingEventsFound*' -or $_.Exception.Message -like '*No events were found*') {
|
If ($_.FullyQualifiedErrorId -like '*NoMatchingEventsFound*' -or $_.Exception.Message -like '*No events were found*') {
|
||||||
$Events = @()
|
$Events = @()
|
||||||
}
|
}
|
||||||
@@ -208,8 +212,52 @@ Catch {
|
|||||||
Write-Error "Unable to read the Windows Security event log. Run PowerShell as administrator if access is denied. $($_.Exception.Message)"
|
Write-Error "Unable to read the Windows Security event log. Run PowerShell as administrator if access is denied. $($_.Exception.Message)"
|
||||||
Exit 1
|
Exit 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ForEach ($Event in ($Events | Sort-Object -Property TimeCreated)) {
|
ForEach ($Event in ($Events | Sort-Object -Property TimeCreated)) {
|
||||||
ConvertTo-UserLogonActivity -Event $Event
|
ConvertTo-UserLogonActivity -Event $Event
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Else {
|
||||||
|
$ActivityLimit = 15
|
||||||
|
$SearchLimit = 200
|
||||||
|
$MaxSearchLimit = 10000
|
||||||
|
$ActivityEvents = New-Object System.Collections.Generic.List[object]
|
||||||
|
|
||||||
|
Do {
|
||||||
|
$ActivityEvents.Clear()
|
||||||
|
|
||||||
|
Try {
|
||||||
|
$Events = Get-WinEvent -FilterHashtable $Filter -MaxEvents $SearchLimit -ErrorAction Stop
|
||||||
|
}
|
||||||
|
Catch {
|
||||||
|
If ($_.FullyQualifiedErrorId -like '*NoMatchingEventsFound*' -or $_.Exception.Message -like '*No events were found*') {
|
||||||
|
$Events = @()
|
||||||
|
}
|
||||||
|
Else {
|
||||||
|
Write-Error "Unable to read the Windows Security event log. Run PowerShell as administrator if access is denied. $($_.Exception.Message)"
|
||||||
|
Exit 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ForEach ($Event in $Events) {
|
||||||
|
$ActivityEvent = ConvertTo-UserLogonActivity -Event $Event
|
||||||
|
|
||||||
|
If ($null -ne $ActivityEvent) {
|
||||||
|
$ActivityEvents.Add($ActivityEvent)
|
||||||
|
}
|
||||||
|
|
||||||
|
If ($ActivityEvents.Count -ge $ActivityLimit) {
|
||||||
|
Break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
If ($ActivityEvents.Count -ge $ActivityLimit -or $Events.Count -lt $SearchLimit -or $SearchLimit -ge $MaxSearchLimit) {
|
||||||
|
Break
|
||||||
|
}
|
||||||
|
|
||||||
|
$SearchLimit = [Math]::Min(($SearchLimit * 2), $MaxSearchLimit)
|
||||||
|
} While ($true)
|
||||||
|
|
||||||
|
$ActivityEvents | Select-Object -First $ActivityLimit
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user