Backup-AdHoc/install-AdHoc-Backup.ps1

62 lines
2.3 KiB
PowerShell

# install-AdHoc-Backup.ps1
# Uso tipico:
# iwr -useb https://<il-tuo-raw-url>/install-AdHoc-Backup.ps1 | iex
# Opzioni:
# ... } -ForceConfig # sovrascrive backup.conf se già esiste
# ... } -NoRun # installa senza eseguire il backup
# ... } -AdHocArgs '-WhatIf' # argomenti aggiuntivi da passare allo script di backup
param(
[string]$InstallDir = 'C:\polo\scripts',
[string]$ScriptUrl = 'https://gitea.poloinformatico.it/Mattia/Backup-AdHoc/raw/branch/main/AdHoc_Backup.ps1',
[string]$ConfigUrl = 'https://gitea.poloinformatico.it/Mattia/Backup-AdHoc/raw/branch/main/backup.conf',
[switch]$ForceConfig,
[switch]$NoRun,
[string[]]$AdHocArgs
)
$ErrorActionPreference = 'Stop'
try { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls13 } catch {}
# 1) Crea cartella di destinazione
if (-not (Test-Path -LiteralPath $InstallDir)) {
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}
# 2) Scarica i due file
$ScriptPath = Join-Path $InstallDir 'AdHoc-Backup.ps1' # <- nome finale con trattino
$ConfigPath = Join-Path $InstallDir 'backup.conf'
Write-Host "Scarico script: $ScriptUrl -> $ScriptPath"
Invoke-WebRequest -UseBasicParsing -Uri $ScriptUrl -OutFile $ScriptPath
if ((-not (Test-Path -LiteralPath $ConfigPath)) -or $ForceConfig.IsPresent) {
Write-Host "Scarico config: $ConfigUrl -> $ConfigPath"
Invoke-WebRequest -UseBasicParsing -Uri $ConfigUrl -OutFile $ConfigPath
} else {
Write-Host "Config già presente, non sovrascrivo (usa -ForceConfig per forzare)."
}
# 3) Sblocca eventuali zone mark
foreach ($p in @($ScriptPath, $ConfigPath)) {
if (Test-Path -LiteralPath $p) {
Unblock-File -LiteralPath $p -ErrorAction SilentlyContinue
}
}
# 4) Esegui lo script (se non esplicitamente evitato)
if (-not $NoRun.IsPresent) {
Write-Host "Avvio: $ScriptPath"
# Se il tuo AdHoc-Backup richiede la config, la passiamo in modo esplicito:
$argsToPass = @()
if (Test-Path -LiteralPath $ConfigPath) {
$argsToPass += @('-Config', $ConfigPath)
}
if ($AdHocArgs) {
$argsToPass += $AdHocArgs
}
# Esegui in un processo figlio con ExecutionPolicy limitata al processo
& powershell -NoProfile -ExecutionPolicy Bypass -File $ScriptPath @argsToPass
}