From 0a1d4275ee7fd83641274e6994a6970ca5e52c7c Mon Sep 17 00:00:00 2001 From: Mattia Tadini Date: Mon, 10 Nov 2025 09:20:50 +0000 Subject: [PATCH] Add install-AdHoc-Backup.ps1 --- install-AdHoc-Backup.ps1 | 61 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 install-AdHoc-Backup.ps1 diff --git a/install-AdHoc-Backup.ps1 b/install-AdHoc-Backup.ps1 new file mode 100644 index 0000000..f5c670d --- /dev/null +++ b/install-AdHoc-Backup.ps1 @@ -0,0 +1,61 @@ +# install-AdHoc-Backup.ps1 +# Uso tipico: +# iwr -useb https:///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 +}