From 99be51467e01bcf3c8c213af853f2bfb43982aa7 Mon Sep 17 00:00:00 2001 From: PTah Date: Thu, 30 Apr 2026 16:55:30 +1000 Subject: [PATCH] Refine RD Gateway 303 semantics and show deploy update timestamp --- Deploy-LoginMonitor.ps1 | 10 +++++++ Login_Monitor.ps1 | 59 ++++++++++++++++++++++++++++++++++++++--- version.txt | 2 +- 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/Deploy-LoginMonitor.ps1 b/Deploy-LoginMonitor.ps1 index 63c0718..ae6b282 100644 --- a/Deploy-LoginMonitor.ps1 +++ b/Deploy-LoginMonitor.ps1 @@ -36,6 +36,7 @@ $ErrorActionPreference = "Stop" $InstallRoot = [System.IO.Path]::GetFullPath("$env:ProgramData\RDP-login-monitor") $LocalScript = Join-Path $InstallRoot "Login_Monitor.ps1" $VersionStampPath = Join-Path $InstallRoot "deployed_version.txt" +$DeployUpdateMarkerPath = Join-Path $InstallRoot "deploy_last_update.txt" $DeployLogPath = Join-Path $InstallRoot "Logs\deploy.log" $ScriptName = "Login_Monitor.ps1" $VersionFileName = "version.txt" @@ -223,6 +224,15 @@ try { [System.IO.File]::WriteAllText($VersionStampPath, "$shareVerRaw`r`n", $Utf8Bom) Write-DeployLog "Записана метка версии: $VersionStampPath" + $updStamp = Get-Date -Format "dd.MM.yyyy HH:mm:ss" + $updMarker = @( + "Version=$shareVerRaw" + "UpdatedAt=$updStamp" + "PendingStartupNotice=1" + ) -join "`r`n" + [System.IO.File]::WriteAllText($DeployUpdateMarkerPath, "$updMarker`r`n", $Utf8Bom) + Write-DeployLog "Записана метка обновления: $DeployUpdateMarkerPath (Version=$shareVerRaw; UpdatedAt=$updStamp)." + if (-not $SkipStartMonitorAfterUpdate) { Start-Process -FilePath $PsExe -ArgumentList @( '-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', $LocalScript diff --git a/Login_Monitor.ps1 b/Login_Monitor.ps1 index 320dd62..035e05f 100644 --- a/Login_Monitor.ps1 +++ b/Login_Monitor.ps1 @@ -69,7 +69,7 @@ $script:MonitorSingletonLockStream = $null # строки ниже, если правки «мелкие» и вы не хотите менять отображаемую версию в логах). # Рекомендация: при значимых релизах меняйте и $ScriptVersion, и version.txt одинаково; при только # исправлениях на шаре — достаточно поднять patch в version.txt (например 1.3.0.1). -$ScriptVersion = "1.3.12" +$ScriptVersion = "1.3.13" # Логи (все под InstallRoot) $LogFile = Join-Path $script:InstallRoot "Logs\login_monitor.log" @@ -83,6 +83,7 @@ $LogRotationMinute = 0 # Heartbeat (только файл) $HeartbeatInterval = 3600 $HeartbeatFile = Join-Path $script:InstallRoot "Logs\last_heartbeat.txt" +$DeployUpdateMarkerFile = Join-Path $script:InstallRoot "deploy_last_update.txt" # Ежедневный отчет $DailyReportHour = 9 @@ -883,9 +884,53 @@ function Send-Heartbeat { $timestamp = Get-Date -Format "dd.MM.yyyy HH:mm:ss" $hHost = (ConvertTo-TelegramHtml $env:COMPUTERNAME) + function Get-DeployUpdateMarker { + $info = [pscustomobject]@{ + Version = $null + UpdatedAt = $null + PendingStartupNotice = $false + } + if (-not (Test-Path -LiteralPath $DeployUpdateMarkerFile)) { return $info } + try { + $lines = @((Get-Content -LiteralPath $DeployUpdateMarkerFile -ErrorAction Stop) | Where-Object { $_ -match '\S' }) + foreach ($ln in $lines) { + $parts = $ln -split '=', 2 + if ($parts.Count -ne 2) { continue } + $k = ($parts[0]).Trim() + $v = ($parts[1]).Trim() + switch ($k) { + 'Version' { $info.Version = $v } + 'UpdatedAt' { $info.UpdatedAt = $v } + 'PendingStartupNotice' { $info.PendingStartupNotice = ($v -eq '1' -or $v -ieq 'true') } + } + } + } catch { } + return $info + } + + function Set-DeployUpdateMarkerPendingOff { + param([pscustomobject]$Marker) + try { + $ver = if ([string]::IsNullOrWhiteSpace($Marker.Version)) { '' } else { $Marker.Version } + $upd = if ([string]::IsNullOrWhiteSpace($Marker.UpdatedAt)) { '' } else { $Marker.UpdatedAt } + $content = @( + "Version=$ver" + "UpdatedAt=$upd" + "PendingStartupNotice=0" + ) -join "`r`n" + Write-TextFileUtf8Bom -Path $DeployUpdateMarkerFile -Text $content + } catch { } + } + if ($IsStartup) { $message = "✅ Мониторинг логинов ЗАПУЩЕН`r`n" - $message += "🏷️ Версия скрипта: $(ConvertTo-TelegramHtml $ScriptVersion)`r`n" + $message += "🏷️ Версия скрипта: $(ConvertTo-TelegramHtml $ScriptVersion)" + $upd = Get-DeployUpdateMarker + if ($upd.PendingStartupNotice -and $upd.Version -eq $ScriptVersion -and -not [string]::IsNullOrWhiteSpace($upd.UpdatedAt)) { + $message += " (обновлён $(ConvertTo-TelegramHtml $upd.UpdatedAt))" + Set-DeployUpdateMarkerPendingOff -Marker $upd + } + $message += "`r`n" $message += "🖥️ Сервер: $hHost`r`n" $message += "🕐 Время запуска: $timestamp" if ($script:OsInstallKindLabel) { @@ -1342,8 +1387,14 @@ function Format-RDGatewayEvent { $hTime = (ConvertTo-TelegramHtml ($TimeCreated.ToString('dd.MM.yyyy HH:mm:ss'))) $message = "" - if ($EventID -eq 302) { $message += "🖥️ УСПЕШНОЕ ПОДКЛЮЧЕНИЕ ЧЕРЕЗ RD GATEWAY" } - elseif ($EventID -eq 303) { $message += "❌ НЕУДАЧНОЕ ПОДКЛЮЧЕНИЕ ЧЕРЕЗ RD GATEWAY" } + if ($EventID -eq 302) { $message += "🖥️ ПОДКЛЮЧЕНИЕ ЧЕРЕЗ RD GATEWAY" } + elseif ($EventID -eq 303) { + if ($ErrorCode -ne "0" -and $ErrorCode -ne "N/A" -and -not [string]::IsNullOrWhiteSpace($ErrorCode)) { + $message += "⚠️ СЕАНС ЧЕРЕЗ RD GATEWAY ЗАВЕРШЁН С ОШИБКОЙ" + } else { + $message += "ℹ️ СЕАНС ЧЕРЕЗ RD GATEWAY ЗАВЕРШЁН" + } + } else { $message += "⚠️ СОБЫТИЕ RD GATEWAY" } $message += "`r`n" diff --git a/version.txt b/version.txt index 90a7f60..7962dcf 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.3.12 +1.3.13