# ------------------------------------------------------------------------------------------------------------------------------ # AUTEUR : François ALBERT le 28/06/2022 # ------------------------------------------------------------------------------------------------------------------------------ # # Paramètres possibles à l'exécution : # # -UnInstall = Désinstallation # -Install = Installation # -PostInstall = PostInstallation # -Trace = Débogage # Param( [Parameter(Mandatory=$False)] [Switch]$UnInstall, [Parameter(Mandatory=$False)] [Switch]$Install, [Parameter(Mandatory=$False)] [Switch]$PostInstall, [Parameter(Mandatory=$False)] [Switch]$RollBack, [Parameter(Mandatory=$False)] [Switch]$Trace ) # ------------------------------------------------------------------------------------------------------------------------------ # PACKAGE / VERSION # ------------------------------------------------------------------------------------------------------------------------------ [String]$AppName = 'Duplicati - IPR Foton' [String]$AppVersion = '2.0.6.3' # ------------------------------------------------------------------------------------------------------------------------------ # SCRIPT # ------------------------------------------------------------------------------------------------------------------------------ Try { # Positionnement du Débogage (Trace) si demandé If ($Trace) { $Global:EnableDebugMessage = $True } # ----------------- # INITIALISATIONS # ----------------- $ScriptTime = [Diagnostics.Stopwatch]::StartNew() [String]$CurrentScript = $MyInvocation.MyCommand.Definition # Chemin complet de ce script [String]$CurrentPath = Split-Path -Path $CurrentScript -Parent # Dossier parent de ce script [String]$ScriptParameters = ($MyInvocation.BoundParameters.Keys | Out-String) -Replace("`r`n"," ") # Liste des paramètres [String]$LogPath = $($ENV:SystemRoot + '\Logs\UR1_Software') # Dossier parent du fichier de log [String]$Global:LogFile = $($LogPath + '\' + $AppName + '-' + $AppVersion + '-' + $(Get-Date -UFormat '%Y%m%d-%Hh%M') + '.log') # Chemin complet du fichier de log If ($RollBack) { [String]$Global:LogFile = $($LogPath + '\RollBack - ' + $AppName + '-' + $AppVersion + '-' + $(Get-Date -UFormat '%Y%m%d-%Hh%M') + '.log') # Chemin complet du fichier de log du RollBack } # --------------------- # IMPORT DE MODULE(S) # --------------------- Import-Module "$CurrentPath\Bin\Module_UR1.psd1" -ErrorAction 'Stop' # ----------------------- # INITIALISATION DU LOG # ----------------------- Write-Log -Message "****************************************************************************************************" -Component $CurrentScript -LogFile $LogFile Write-Log -Message "Début d'exécution de '$AppName - $AppVersion'" -Component $CurrentScript -LogFile $LogFile Write-Log -Message "Script : $CurrentScript" -Component $CurrentScript -LogFile $LogFile Write-Log -Message "Paramètres : $ScriptParameters" -Component $CurrentScript -LogFile $LogFile Write-Log -Message "****************************************************************************************************" -Component $CurrentScript -LogFile $LogFile # ------------------------------------------- # DÉFINITION DES VARIABLES SYSTÈME & SCRIPT # ------------------------------------------- Set-ScriptVariables # Variables disponibles : variables système standard + # $Arch, $CommonProgFiles, $CommonProgFiles86, $Default, $Desktop, $HomeDrive, $Msiexec, $OS, $OSName, $OSServicePack, $OSVersion, # $ProgFiles, $ProgFiles86, $ProcessArch, $Public, $Reg, $SchTasks, $StartMenu, $UserDesktop, $UsersProfile, $UserStartMenu # # ------------------------------------------------------------------------------------------------------------------------------ # UNINSTALL # ------------------------------------------------------------------------------------------------------------------------------ If ($UnInstall) { [String]$Step = 'Désinstallation' Write-Verbose "$(Get-Date -UFormat '%H:%M:%S') - $Step en cours...`r`n" Write-Log -Message " " -LogFile $LogFile Write-Log -Message "***** $($Step.ToUpper()) **********************************************************************************************" -Component $CurrentScript -LogFile $LogFile # # Début des commandes de Désinstallation # -------------------------------------- # # Interaction avec un utilisateur éventuellement connecté si process en cours # $Process = Invoke-KillProcessWithPopUp -AppName $AppName -PackageVersion $AppVersion -Process $('Duplicati.GUI.TrayIcon') If ($Process -gt '1') { # Le code retour de l'interaction est supérieur à 1, on sort du script $ExitUR1 = 1000 Throw "Script annulé par l'utilisateur ou Process ouvert" } # # Le code retour de l'interaction est 0 ou 1, on poursuit # Désinstallation toutes versions # [HashTable[]]$Versions = @{Version = "2.0.5.1" ; Guid = "{FC1F2B24-1783-4352-98E9-669896601613}"}, @{Version = "2.0.6.3" ; Guid = "{D2A5D819-4FA0-493B-8D37-9531C659D95A}"} ForEach ($Version in $Versions) { # Si exécutable Foreach ($Uninst in $Version.Uninst) { If ($Uninst -ne $null) { If (Test-Path -Path $Uninst -PathType 'Leaf') { If (!($Version.ExitCode)) { $Version.ExitCode = '0' } Write-Log -Message " " -LogFile $LogFile Write-Log -Message $('Désinstallation EXECUTABLE de la version : ' + $Version.Version) -Component $CurrentScript -LogFile $LogFile Write-Log -Message $(' --> Désinstalleur : "' + $Uninst + '"') -Component $CurrentScript -LogFile $LogFile Write-Log -Message $(' --> Arguments : "' + $Version.Arguments + '"') -Component $CurrentScript -LogFile $LogFile Write-Log -Message $(' --> ExitCode attendu : "' + $Version.ExitCode + '"') -Component $CurrentScript -LogFile $LogFile Invoke-LaunchProcess -Path $Uninst -ArgumentList $Version.Arguments -WorkingDirectory $(Split-Path -Path $Uninst -Parent) -ExitCode $Version.ExitCode -NoNewWindow -LogFile $LogFile | Out-Null } } } # Si MSI $RegUninst64 = 'Registry::HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\' $RegUninst = 'Registry::HKLM\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\' ForEach ($Guid in $Version.Guid) { If ($Guid -ne $null) { If ((Test-Path -Path $("$RegUninst64" + "$Guid")) -or (Test-Path -Path $("$RegUninst" + "$Guid"))) { Write-Log -Message " " -LogFile $LogFile Write-Log -Message $('Désinstallation MSI de la version : ' + $Version.Version) -Component $CurrentScript -LogFile $LogFile Write-Log -Message $(' --> Guid : "' + $Guid + '"') -Component $CurrentScript -LogFile $LogFile Write-Log -Message $(' --> ExitCode attendu : "' + $Version.ExitCode + '"') -Component $CurrentScript -LogFile $LogFile Invoke-MsiInstall -Action 'Uninstall' -ProductCode $Guid -ExitCode $Version.ExitCode -NoLog | Out-Null If (Test-Path -Path $("$RegUninst64" + "$Guid")) { Write-Log -Message $('Suppression dans le registre : ' + $("$RegUninst64" + "$Guid")) -Component $CurrentScript -LogFile $LogFile Remove-Item -Path $("$RegUninst64" + "$Guid") -Recurse -Force -ErrorAction SilentlyContinue | Out-Null } If (Test-Path -Path $("$RegUninst" + "$Guid")) { Write-Log -Message $('Suppression dans le registre : ' + $("$RegUninst" + "$Guid")) -Component $CurrentScript -LogFile $LogFile Remove-Item -Path $("$RegUninst" + "$Guid") -Recurse -Force -ErrorAction SilentlyContinue | Out-Null } } } } } Write-Log -Message " " -LogFile $LogFile # Suppression de la clé de registre indiquant la présence de l'application # Remove-RegistryKey -Key "HKEY_LOCAL_MACHINE\SOFTWARE\UR1_Software\$AppName" -Recurse -LogFile $LogFile # # Fin des commandes de Désinstallation # ------------------------------------ # } # ------------------------------------------------------------------------------------------------------------------------------ # INSTALL # ------------------------------------------------------------------------------------------------------------------------------ If ($Install) { [String]$Step = 'Installation' Write-Verbose "$(Get-Date -UFormat '%H:%M:%S') - $Step en cours...`r`n" Write-Log -Message " " -LogFile $LogFile Write-Log -Message "***** $($Step.ToUpper()) **************************************************************************************************" -Component $CurrentScript -LogFile $LogFile # # Début des commandes d'Installation # ---------------------------------- # Invoke-MsiInstall -Path "$Currentpath\Sources\duplicati-2.0.6.3_beta_2021-06-17-x64.msi" -LogFile $LogFile | Out-Null # # Fin des commandes d'Installation # -------------------------------- # } # ------------------------------------------------------------------------------------------------------------------------------ # POSTINSTALL # ------------------------------------------------------------------------------------------------------------------------------ If ($PostInstall) { [String]$Step = 'PostInstallation' Write-Verbose "$(Get-Date -UFormat '%H:%M:%S') - $Step en cours...`r`n" Write-Log -Message " " -LogFile $LogFile Write-Log -Message "***** $($Step.ToUpper()) ********************************************************************************************" -Component $CurrentScript -LogFile $LogFile # # Début des commandes de PostInstallation # --------------------------------------- # [String]$NOMMACHINE = "$ENV:COMPUTERNAME" [String]$ENTITE = 'IPR' [String]$CurrentUser = (Get-WmiObject -Class win32_computersystem | Select-Object -ExpandProperty username).split('\')[1] # Ménage # [String[]]$AllItems = "$Desktop\Duplicati 2.lnk" ForEach ($Item in $AllItems) { If (Test-Path -Path $Item -PathType 'Container') { Write-Log -Message $('Suppression de dossier : ' + $Item) -Component $CurrentScript -LogFile $LogFile Remove-Item -Path $Item -Recurse -Force -ErrorAction Stop | Out-Null } Else { If (Test-Path -Path $Item -PathType 'Leaf') { Write-Log -Message $('Suppression de fichier : ' + $Item) -Component $CurrentScript -LogFile $LogFile Remove-Item -Path $Item -Force -ErrorAction Stop | Out-Null } } } # ---------------------------------------- # Creation et copie du fichier de config # ---------------------------------------- if (!(test-path "D:\Home\$CurrentUser\Duplicati\")) { New-Item "D:\Home\$CurrentUser\Duplicati\" -itemType Directory } if (!(test-path "D:\Home\$CurrentUser\Duplicati\.ssh\")) { New-Item "D:\Home\$CurrentUser\Duplicati\.ssh\" -itemType Directory } $Fichier = get-content "$Currentpath\Files\sauvegarde.duplicati.config.json" #Detection si la machine fait partie de l'entitée FOTON sinon on fait la config pour l'IPR if (("$ENV:COMPUTERNAME").Substring(2,3) -eq "034" ) { $ENTITE = "FOTON" } switch ($ENTITE) { IPR { if (!(test-path "D:\Home\$CurrentUser\Duplicati\.ssh\duplicati_id_ed25519")) { ssh-keygen -t ed25519 -f "D:\Home\$CurrentUser\Duplicati\.ssh\duplicati_id_ed25519" -q -N '""' Write-Log -Message "Création des clefs SSH ed25519 IPR" -Component $CurrentScript -LogFile $LogFile Send-MailMessage -SmtpServer smtpint.univ-rennes1.fr -From "$CurrentUser@univ-rennes1.fr" -To IPR.ADMIN.EMAIL@listes.univ-rennes.fr -Subject "$Hostname $CurrentUser - Nouvelle clef Duplicati" -Attachments D:\Home\$CurrentUser\Duplicati\.ssh\duplicati_id_ed25519.pub } $fichier = $fichier -replace "TARGETURLUR", "ssh://duplicati.ipr.univ-rennes1.fr//home//$CurrentUser//$NOMMACHINE//?auth-username=$CurrentUser&ssh-fingerprint=ssh-ed25519 32 4A:2D:A0:5F:66:7A:D5:3A:F4:B2:63:EC:EE:1E:D0:21&ssh-keyfile=D:\\Home\\$CurrentUser\\Duplicati\\.ssh\\duplicati_id_ed25519" } FOTON { if (!(test-path "D:\Home\$CurrentUser\Duplicati\.ssh\duplicati_id_ed25519")) { ssh-keygen -t ed25519 -f "D:\Home\$CurrentUser\Duplicati\.ssh\duplicati_id_ed25519" -q -N '""' Write-Log -Message "Création des clefs SSH ed25519 FOTON" -Component $CurrentScript -LogFile $LogFile Send-MailMessage -SmtpServer smtpint.univ-rennes1.fr -From "$CurrentUser@univ-rennes1.fr" -To FOTON.ADMIN.EMAIL@univ-rennes.fr -Subject "$Hostname $CurrentUser - Nouvelle clef Duplicati" -Attachments D:\Home\$CurrentUser\Duplicati\.ssh\duplicati_id_ed25519.pub } $fichier = $fichier -replace "TARGETURLUR", "ssh://bkpdop.univ-rennes1.fr//mnt//datastore_backuppc//duplicati//$CurrentUser.$NOMMACHINE//?auth-username=$CurrentUser&ssh-fingerprint=ssh-ed25519 32 1E:EC:EB:7D:52:A3:54:5A:48:EA:59:6A:CF:8C:74:48&ssh-keyfile=D:\\Home\\$CurrentUser\\Duplicati\\.ssh\\duplicati_id_ed25519" } <# FOTON { if (!(test-path "D:\Home\$CurrentUser\Duplicati\.ssh\id_rsa_duplicati")) { ssh-keygen -t rsa -f "D:\Home\$CurrentUser\Duplicati\.ssh\id_rsa_duplicati" -q -N '""' Write-Log -Message "Création des clefs SSH RSA FOTON" -Component $CurrentScript -LogFile $LogFile Send-MailMessage -SmtpServer smtpint.univ-rennes1.fr -From "$CurrentUser@univ-rennes1.fr" -To FOTON.ADMIN.EMAIL@univ-rennes.fr -Subject "$Hostname $CurrentUser - Nouvelle clef Duplicati" -Attachments D:\Home\$CurrentUser\Duplicati\.ssh\duplicati_id_ed25519.pub } $fichier = $fichier -replace "TARGETURLUR", "ssh://bkpdop.univ-rennes1.fr//mnt//datastore_backuppc//duplicati//$CurrentUser.$NOMMACHINE//?auth-username=$CurrentUser&ssh-fingerprint=ssh-ed25519 32 1E:EC:EB:7D:52:A3:54:5A:48:EA:59:6A:CF:8C:74:48&ssh-keyfile=D:\\Home\\$CurrentUser\\Duplicati\\.ssh\\id_rsa_duplicati" #$fichier = $fichier -replace "TARGETURLUR", "ssh://bkpdop.univ-rennes1.fr//mnt//datastore_backuppc//duplicati//$CurrentUser.$NOMMACHINE//?auth-username=$CurrentUser&ssh-fingerprint=ssh-rsa 2048 02:b9:38:a6:17:66:ba:90:e0:be:bb:7e:fb:a7:e6:14&ssh-keyfile=D:\\Home\\$CurrentUser\\Duplicati\\.ssh\\id_rsa_duplicati" } #> } #Donne les droits à l'utilisateur courant aux clés ssh générées. get-acl "D:\Home\$CurrentUser" | set-acl D:\Home\$CurrentUser\Duplicati\.ssh\* $fichier = $fichier -replace "SESAME",$CurrentUser $Fichier | set-content "D:\Home\$CurrentUser\Duplicati\sauvegarde.duplicati.config.json" Write-Log -Message "Creation du fichier de configuration pour l'entitée $entite , Utilisateur : $CurrentUser" -Component $CurrentScript -LogFile $LogFile Write-Log -Message " " -Component $CurrentScript -LogFile $LogFile Invoke-LaunchProcess -Path "$ProgFiles\Duplicati 2\Duplicati.GUI.TrayIcon.exe" -Argumentlist "--webservice-interface=loopback" -nowait -NoNewWindow -LogFile $LogFile | Out-Null # Création de la clé de registre indiquant la présence de l'application ("Version" à utiliser en méthode de détection avec SCCM) # Set-RegistryKey -Key "HKEY_LOCAL_MACHINE\SOFTWARE\UR1_Software\$AppName" -Name "Version" -Type String -Value $AppVersion -LogFile $LogFile Set-RegistryKey -Key "HKEY_LOCAL_MACHINE\SOFTWARE\UR1_Software\$AppName" -Name "Date" -Type String -Value $(Get-Date -UFormat '%Y%m%d') -LogFile $LogFile # # Fin des commandes de PostInstallation # ------------------------------------- # } } # ------------------------------------------------------------------------------------------------------------------------------ # SCRIPT EN ERREUR # ------------------------------------------------------------------------------------------------------------------------------ Catch { Write-Log -Message $(' -- Erreur : ' + $_ + ' --') -Severity '2' -Component $CurrentScript -LogFile $LogFile Write-Log -Message " " -Component $CurrentScript -LogFile $LogFile # If (!($RollBack)) { If ($Step -ne 'Désinstallation') { Write-Log -Message 'RollBack lancé' -Severity '2' -Component $CurrentScript -LogFile $LogFile Invoke-AppRollback -FileName $CurrentScript Write-Log -Message 'RollBack terminé' -Severity '2' -Component $CurrentScript -LogFile $LogFile } Else { Write-Log -Message 'RollBack non lancé, erreur dans la partie Désinstallation' -Severity '3' -Component $CurrentScript -LogFile $LogFile } } Else { Write-Log -Message 'Erreur dans la partie Désinstallation du script de RollBack' -Severity '3' -Component $CurrentScript -LogFile $LogFile } Write-Log -Message " " -LogFile $LogFile Write-Log -Message "****************************************************************************************************" -Component $CurrentScript -LogFile $LogFile Write-Log -Message "$Step EN ERREUR pour '$AppName - $AppVersion'" -Component $CurrentScript -LogFile $LogFile Write-Log -Message "A la ligne : $($_.InvocationInfo.ScriptLineNumber)" -Component $CurrentScript -LogFile $LogFile Write-Log -Message "****************************************************************************************************" -Component $CurrentScript -LogFile $LogFile Throw $_.Exception.ErrorRecord } # ------------------------------------------------------------------------------------------------------------------------------ # FIN # ------------------------------------------------------------------------------------------------------------------------------ Finally { $ScriptTime.Stop() $ScriptDuration = $([String]::Format("{0:d2}:{1:d2}:{2:d2}", $ScriptTime.Elapsed.Hours, $ScriptTime.Elapsed.Minutes, $ScriptTime.Elapsed.Seconds)) Write-Log -Message " " -LogFile $LogFile Write-Log -Message '****************************************************************************************************' -Component $CurrentScript -LogFile $LogFile Write-Log -Message "Fin d'exécution de '$AppName - $AppVersion'" -Component $CurrentScript -LogFile $LogFile Write-Log -Message "Script : $CurrentScript" -Component $CurrentScript -LogFile $LogFile Write-Log -Message "Paramètres : $ScriptParameters" -Component $CurrentScript -LogFile $LogFile Write-Log -Message "Durée d'exécution : $ScriptDuration" -Component $CurrentScript -LogFile $LogFile Write-Log -Message '****************************************************************************************************' -Component $CurrentScript -LogFile $LogFile # If ($ExitUR1) { Exit $ExitUR1 } }