119 lines
3.3 KiB
PowerShell
119 lines
3.3 KiB
PowerShell
# ==========================================
|
||
# 🛠️ COMMON UTILS
|
||
# ==========================================
|
||
|
||
# --- ГЛОБАЛЬНЫЕ НАСТРОЙКИ ---
|
||
|
||
# Режим отладки (передаётся через -Debug)
|
||
if (-not (Test-Path variable:script:DebugMode)) {
|
||
$script:DebugMode = $false
|
||
}
|
||
|
||
function Set-DebugMode {
|
||
param([bool]$Enabled)
|
||
$script:DebugMode = $Enabled
|
||
if ($Enabled) {
|
||
Write-Host " 🔧 Debug режим включён" -ForegroundColor Magenta
|
||
}
|
||
}
|
||
|
||
function Get-DebugMode {
|
||
return $script:DebugMode
|
||
}
|
||
|
||
# --- ЦВЕТА И ВЫВОД ---
|
||
|
||
function Write-Step { param($msg) Write-Host "`n📦 $msg" -ForegroundColor Cyan }
|
||
function Write-Success { param($msg) Write-Host " ✅ $msg" -ForegroundColor Green }
|
||
function Write-Warning { param($msg) Write-Host " ⚠️ $msg" -ForegroundColor Yellow }
|
||
function Write-Error { param($msg) Write-Host " ❌ $msg" -ForegroundColor Red }
|
||
function Write-Info { param($msg) Write-Host " ℹ️ $msg" -ForegroundColor Gray }
|
||
|
||
function Write-DebugLog {
|
||
param($msg)
|
||
if ($script:DebugMode) {
|
||
Write-Host " [DEBUG] $msg" -ForegroundColor DarkGray
|
||
}
|
||
}
|
||
|
||
function Write-Header {
|
||
param($Title, [switch]$ClearScreen)
|
||
|
||
if ($ClearScreen -and -not $script:DebugMode) {
|
||
Clear-Host
|
||
}
|
||
|
||
Write-Host ""
|
||
Write-Host "==========================================" -ForegroundColor Cyan
|
||
Write-Host " $Title" -ForegroundColor Cyan
|
||
Write-Host "==========================================" -ForegroundColor Cyan
|
||
Write-Host ""
|
||
}
|
||
|
||
# --- ЗАПУСК КОМАНД ---
|
||
|
||
function Invoke-Silent {
|
||
param(
|
||
[string]$FilePath,
|
||
[string]$Arguments,
|
||
[switch]$Wait
|
||
)
|
||
|
||
$psi = New-Object System.Diagnostics.ProcessStartInfo
|
||
$psi.FileName = $FilePath
|
||
$psi.Arguments = $Arguments
|
||
$psi.UseShellExecute = $false
|
||
$psi.CreateNoWindow = $true
|
||
|
||
if (-not $script:DebugMode) {
|
||
$psi.RedirectStandardOutput = $true
|
||
$psi.RedirectStandardError = $true
|
||
}
|
||
|
||
$process = [System.Diagnostics.Process]::Start($psi)
|
||
|
||
if ($Wait) {
|
||
$process.WaitForExit()
|
||
return $process.ExitCode
|
||
}
|
||
|
||
return $process
|
||
}
|
||
|
||
# --- ПОЛЕЗНЫЕ ФУНКЦИИ ---
|
||
|
||
function Get-ScriptDirectory {
|
||
if ($PSScriptRoot) { return $PSScriptRoot }
|
||
return Split-Path -Parent $MyInvocation.MyCommand.Path
|
||
}
|
||
|
||
function Ensure-Admin {
|
||
$isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
|
||
if (-not $isAdmin) {
|
||
Write-Host "⛔ Требуются права АДМИНИСТРАТОРА!" -ForegroundColor Red
|
||
Write-Host " Пожалуйста, запустите скрипт от имени администратора." -ForegroundColor Gray
|
||
Start-Sleep -Seconds 3
|
||
exit 1
|
||
}
|
||
}
|
||
|
||
function Show-Menu {
|
||
param(
|
||
[string]$Title,
|
||
[System.Collections.Specialized.OrderedDictionary]$Options,
|
||
[string]$Prompt = "👉 Ваш выбор"
|
||
)
|
||
|
||
if ($Title) {
|
||
Write-Host "`n$Title" -ForegroundColor Yellow
|
||
}
|
||
|
||
$keys = $Options.Keys
|
||
foreach ($key in $keys) {
|
||
Write-Host " [$key] $($Options[$key])" -ForegroundColor White
|
||
}
|
||
Write-Host ""
|
||
|
||
return Read-Host "$Prompt"
|
||
}
|