fix: allow null SacOccurredAt on lifecycle notifications (2.0.15-SAC)
PS 5.1 rejects binding null to [datetime]; use optional splatting for SAC occurred_at. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+15
-4
@@ -89,7 +89,7 @@ $script:SkipLogDetailLimit = 15
|
||||
# строки ниже, если правки «мелкие» и вы не хотите менять отображаемую версию в логах).
|
||||
# Рекомендация: при значимых релизах меняйте и $ScriptVersion, и version.txt одинаково; при только
|
||||
# исправлениях на шаре — достаточно поднять patch в version.txt (например 1.3.0.1).
|
||||
$ScriptVersion = "2.0.14-SAC"
|
||||
$ScriptVersion = "2.0.15-SAC"
|
||||
|
||||
# Логи (все под InstallRoot)
|
||||
$LogFile = Join-Path $script:InstallRoot "Logs\login_monitor.log"
|
||||
@@ -978,7 +978,7 @@ function Send-MonitorNotification {
|
||||
[string]$SacTitle = '',
|
||||
[string]$SacSummary = '',
|
||||
[hashtable]$SacDetails = $null,
|
||||
[datetime]$SacOccurredAt = $null
|
||||
$SacOccurredAt = $null
|
||||
)
|
||||
|
||||
$title = if (-not [string]::IsNullOrWhiteSpace($SacTitle)) { $SacTitle } else { $EmailSubject }
|
||||
@@ -992,8 +992,19 @@ function Send-MonitorNotification {
|
||||
if ($script:SacClientLoaded -and (Get-Command Send-NotifyOrSac -ErrorAction SilentlyContinue)) {
|
||||
$sacMode = Get-SacNormalizedMode
|
||||
if ($sacMode -ne 'off') {
|
||||
return Send-NotifyOrSac -EventType $etype -Severity $SacSeverity -Title $title -Summary $summary `
|
||||
-TelegramMessage $Message -EmailSubject $EmailSubject -Details $SacDetails -OccurredAt $SacOccurredAt
|
||||
$sacNotifyArgs = @{
|
||||
EventType = $etype
|
||||
Severity = $SacSeverity
|
||||
Title = $title
|
||||
Summary = $summary
|
||||
TelegramMessage = $Message
|
||||
EmailSubject = $EmailSubject
|
||||
Details = $SacDetails
|
||||
}
|
||||
if ($null -ne $SacOccurredAt) {
|
||||
$sacNotifyArgs['OccurredAt'] = $SacOccurredAt
|
||||
}
|
||||
return Send-NotifyOrSac @sacNotifyArgs
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+41
-8
@@ -342,7 +342,7 @@ function New-SacEventPayload {
|
||||
[Parameter(Mandatory = $true)][string]$Title,
|
||||
[Parameter(Mandatory = $true)][string]$Summary,
|
||||
[hashtable]$Details = $null,
|
||||
[datetime]$OccurredAt = $null
|
||||
$OccurredAt = $null
|
||||
)
|
||||
|
||||
$Title = Limit-SacString -Text $Title -MaxLen 256 -Label 'title'
|
||||
@@ -656,7 +656,7 @@ function Send-SacEvent {
|
||||
[Parameter(Mandatory = $true)][string]$Title,
|
||||
[Parameter(Mandatory = $true)][string]$Summary,
|
||||
[hashtable]$Details = $null,
|
||||
[datetime]$OccurredAt = $null
|
||||
$OccurredAt = $null
|
||||
)
|
||||
|
||||
if (-not (Test-SacConfigured)) {
|
||||
@@ -669,7 +669,17 @@ function Send-SacEvent {
|
||||
$mergedDetails = $null
|
||||
}
|
||||
|
||||
$payload = $(New-SacEventPayload -EventType $EventType -Severity $Severity -Title $Title -Summary $Summary -Details $mergedDetails -OccurredAt $OccurredAt)
|
||||
$payloadArgs = @{
|
||||
EventType = $EventType
|
||||
Severity = $Severity
|
||||
Title = $Title
|
||||
Summary = $Summary
|
||||
Details = $mergedDetails
|
||||
}
|
||||
if ($null -ne $OccurredAt) {
|
||||
$payloadArgs['OccurredAt'] = $OccurredAt
|
||||
}
|
||||
$payload = $(New-SacEventPayload @payloadArgs)
|
||||
if ($payload -is [System.Array]) {
|
||||
$payload = $payload[-1]
|
||||
}
|
||||
@@ -731,6 +741,29 @@ function Merge-SacNotifyDetails {
|
||||
return $merged
|
||||
}
|
||||
|
||||
function Get-SacEventInvokeArgs {
|
||||
param(
|
||||
[Parameter(Mandatory = $true)][string]$EventType,
|
||||
[Parameter(Mandatory = $true)][string]$Severity,
|
||||
[Parameter(Mandatory = $true)][string]$Title,
|
||||
[Parameter(Mandatory = $true)][string]$Summary,
|
||||
[hashtable]$Details = $null,
|
||||
$OccurredAt = $null
|
||||
)
|
||||
|
||||
$args = @{
|
||||
EventType = $EventType
|
||||
Severity = $Severity
|
||||
Title = $Title
|
||||
Summary = $Summary
|
||||
Details = $Details
|
||||
}
|
||||
if ($null -ne $OccurredAt) {
|
||||
$args['OccurredAt'] = $OccurredAt
|
||||
}
|
||||
return $args
|
||||
}
|
||||
|
||||
function Send-NotifyOrSac {
|
||||
param(
|
||||
[Parameter(Mandatory = $true)][string]$EventType,
|
||||
@@ -740,7 +773,7 @@ function Send-NotifyOrSac {
|
||||
[string]$TelegramMessage = '',
|
||||
[string]$EmailSubject = 'RDP Login Monitor',
|
||||
[hashtable]$Details = $null,
|
||||
[datetime]$OccurredAt = $null
|
||||
$OccurredAt = $null
|
||||
)
|
||||
|
||||
if ([string]::IsNullOrWhiteSpace($TelegramMessage)) {
|
||||
@@ -755,7 +788,7 @@ function Send-NotifyOrSac {
|
||||
return $false
|
||||
}
|
||||
$hbDetails = Merge-SacNotifyDetails -Details $Details -TelegramVia 'sac'
|
||||
return (Send-SacEvent -EventType $EventType -Severity $Severity -Title $Title -Summary $Summary -Details $hbDetails -OccurredAt $OccurredAt)
|
||||
return (Send-SacEvent @ (Get-SacEventInvokeArgs -EventType $EventType -Severity $Severity -Title $Title -Summary $Summary -Details $hbDetails -OccurredAt $OccurredAt))
|
||||
}
|
||||
|
||||
switch ($mode) {
|
||||
@@ -764,16 +797,16 @@ function Send-NotifyOrSac {
|
||||
}
|
||||
'exclusive' {
|
||||
$merged = Merge-SacNotifyDetails -Details $Details -TelegramVia 'sac'
|
||||
return (Send-SacEvent -EventType $EventType -Severity $Severity -Title $Title -Summary $Summary -Details $merged -OccurredAt $OccurredAt)
|
||||
return (Send-SacEvent @ (Get-SacEventInvokeArgs -EventType $EventType -Severity $Severity -Title $Title -Summary $Summary -Details $merged -OccurredAt $OccurredAt))
|
||||
}
|
||||
'dual' {
|
||||
$merged = Merge-SacNotifyDetails -Details $Details -TelegramVia 'agent'
|
||||
Send-SacEvent -EventType $EventType -Severity $Severity -Title $Title -Summary $Summary -Details $merged -OccurredAt $OccurredAt | Out-Null
|
||||
Send-SacEvent @ (Get-SacEventInvokeArgs -EventType $EventType -Severity $Severity -Title $Title -Summary $Summary -Details $merged -OccurredAt $OccurredAt) | Out-Null
|
||||
return (Send-SacLocalChannels -TelegramMessage $TelegramMessage -EmailSubject $EmailSubject)
|
||||
}
|
||||
'fallback' {
|
||||
$merged = Merge-SacNotifyDetails -Details $Details -TelegramVia 'sac'
|
||||
if (Send-SacEvent -EventType $EventType -Severity $Severity -Title $Title -Summary $Summary -Details $merged -OccurredAt $OccurredAt) {
|
||||
if (Send-SacEvent @ (Get-SacEventInvokeArgs -EventType $EventType -Severity $Severity -Title $Title -Summary $Summary -Details $merged -OccurredAt $OccurredAt)) {
|
||||
return $true
|
||||
}
|
||||
return (Send-SacLocalChannels -TelegramMessage $TelegramMessage -EmailSubject $EmailSubject)
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
2.0.14-SAC
|
||||
2.0.15-SAC
|
||||
|
||||
Reference in New Issue
Block a user