Загрузка данных


[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
[Console]::InputEncoding = [System.Text.Encoding]::UTF8

# ------------------- НАСТРОЙКИ -------------------
$alertFile      = "\\server\AlertShare\alert.txt"
$alertDoneFile  = "\\server\AlertShare\Done\alert_done_$env:COMPUTERNAME.txt"
$checkInterval  = 5

$AlertTitle = "ЭКСТРЕННОЕ ОПОВЕЩЕНИЕ"
$AlertMessage = @"
Срочно покиньте помещение
"@
$SoundRepeat = $true
# -------------------------------------------------

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

while ($true) {
    
    # Ждём появления файла тревоги
    while (-not (Test-Path $alertFile)) {
        Start-Sleep -Seconds $checkInterval
    }
    
    # Файл появился — тревога
    if (Test-Path $alertDoneFile) {
        # На этом ПК уже подтверждали — ждём отбоя, не показываем окно повторно
        while (Test-Path $alertFile) {
            Start-Sleep -Seconds $checkInterval
        }
        # Отбой — удаляем метку
        try { Remove-Item $alertDoneFile -Force -ErrorAction SilentlyContinue } catch {}
        continue
    }
    
    # ---------- СОЗДАЁМ ФОРМУ ----------
    $Form = New-Object System.Windows.Forms.Form
    $Form.Text = $AlertTitle
    $Form.StartPosition = "CenterScreen"
    $Form.FormBorderStyle = "FixedDialog"
    $Form.ControlBox = $false
    $Form.TopMost = $true
    $Form.WindowState = "Maximized"
    $Form.BackColor = [System.Drawing.Color]::FromArgb(192, 0, 0)
    $Form.KeyPreview = $true
    
    # Таймер возврата фокуса
    $FocusTimer = New-Object System.Windows.Forms.Timer
    $FocusTimer.Interval = 200
    $FocusTimer.Add_Tick({
        $Form.Activate()
        $Form.BringToFront()
    })
    
    # Таймер проверки отбоя (каждую секунду проверяет, жив ли alert.txt)
    $CheckTimer = New-Object System.Windows.Forms.Timer
    $CheckTimer.Interval = 1000
    $CheckTimer.Add_Tick({
        if (-not (Test-Path $alertFile)) {
            # Файл исчез — закрываем форму
            $Form.Close()
        }
    })
    
    # Запрещаем закрытие пользователем
    $Form.Add_FormClosing({
        if ($_.CloseReason -eq "UserClosing") {
            $_.Cancel = $true
        }
    })
    
    # ---------- ЗАГОЛОВОК ----------
    $LabelTitle = New-Object System.Windows.Forms.Label
    $LabelTitle.Text = "БЕСПИЛОТНАЯ ОПАСНОСТЬ"
    $LabelTitle.Font = New-Object System.Drawing.Font("Arial", 32, [System.Drawing.FontStyle]::Bold)
    $LabelTitle.ForeColor = [System.Drawing.Color]::White
    $LabelTitle.AutoSize = $true
    $LabelTitle.TextAlign = "MiddleCenter"
    $Form.Add_Shown({
        $LabelTitle.Left = ($Form.ClientSize.Width - $LabelTitle.Width) / 2
        $LabelTitle.Top = ($Form.ClientSize.Height * 0.15) - ($LabelTitle.Height / 2)
    })
    $Form.Controls.Add($LabelTitle)
    
    # ---------- ОСНОВНОЙ ТЕКСТ ----------
    $LabelMessage = New-Object System.Windows.Forms.Label
    $LabelMessage.Text = $AlertMessage
    $LabelMessage.Font = New-Object System.Drawing.Font("Arial", 20)
    $LabelMessage.ForeColor = [System.Drawing.Color]::White
    $LabelMessage.AutoSize = $true
    $LabelMessage.TextAlign = "MiddleCenter"
    $Form.Add_Shown({
        $LabelMessage.Left = ($Form.ClientSize.Width - $LabelMessage.Width) / 2
        $LabelMessage.Top = ($Form.ClientSize.Height / 2) - ($LabelMessage.Height / 2)
    })
    $Form.Controls.Add($LabelMessage)
    
    # ---------- КНОПКА ----------
    $ButtonOk = New-Object System.Windows.Forms.Button
    $ButtonOk.Text = "ПОДТВЕРЖДАЮ"
    $ButtonOk.Font = New-Object System.Drawing.Font("Arial", 18, [System.Drawing.FontStyle]::Bold)
    $ButtonOk.Size = New-Object System.Drawing.Size(300, 80)
    $ButtonOk.BackColor = [System.Drawing.Color]::White
    $ButtonOk.ForeColor = [System.Drawing.Color]::DarkRed
    $ButtonOk.FlatStyle = "Popup"
    $Form.Add_Shown({
        $ButtonOk.Left = ($Form.ClientSize.Width - $ButtonOk.Width) / 2
        $ButtonOk.Top = ($Form.ClientSize.Height * 0.78) - ($ButtonOk.Height / 2)
    })
    $ButtonOk.Add_Click({
        try {
            "ПК: $env:COMPUTERNAME`nПользователь: $env:USERNAME`nВремя: $(Get-Date -Format 'dd.MM.yyyy HH:mm:ss')" | Out-File $alertDoneFile -Force -Encoding UTF8
        } catch {}
        $ButtonOk.Text = "ПРИНЯТО. ЖДИТЕ."
        $ButtonOk.Enabled = $false
    })
    $Form.Controls.Add($ButtonOk)
    
    # ---------- ФУТЕР ----------
    $LabelFooter = New-Object System.Windows.Forms.Label
    $LabelFooter.Text = "Ждите дальнейших указаний"
    $LabelFooter.Font = New-Object System.Drawing.Font("Arial", 12, [System.Drawing.FontStyle]::Italic)
    $LabelFooter.ForeColor = [System.Drawing.Color]::LightGray
    $LabelFooter.AutoSize = $true
    $LabelFooter.TextAlign = "MiddleCenter"
    $Form.Add_Shown({
        $LabelFooter.Left = ($Form.ClientSize.Width - $LabelFooter.Width) / 2
        $LabelFooter.Top = ($Form.ClientSize.Height * 0.92) - ($LabelFooter.Height / 2)
    })
    $Form.Controls.Add($LabelFooter)
    
    # ---------- ЗВУК ----------
    if ($SoundRepeat) {
        $SoundTimer = New-Object System.Windows.Forms.Timer
        $SoundTimer.Interval = 10000
        $SoundTimer.Add_Tick({ [System.Console]::Beep(800, 500) })
        $SoundTimer.Start()
    }
    
    # ---------- ПОКАЗЫВАЕМ ФОРМУ ----------
    $Form.Add_Shown({
        $FocusTimer.Start()
        $CheckTimer.Start()
    })
    $Form.ShowDialog()
    
    # Сюда попадаем только когда окно закрылось (по отбою)
    $FocusTimer.Stop()
    $CheckTimer.Stop()
    if ($SoundRepeat) { $SoundTimer.Stop() }
    $Form.Dispose()
    
    # Удаляем метку после отбоя
    if (Test-Path $alertDoneFile) {
        try { Remove-Item $alertDoneFile -Force -ErrorAction SilentlyContinue } catch {}
    }
}