# Script install App MDT # ----------- Modifier variable apres cette ligne ----------- # ------------- Modify variable after this line ------------- $NameApp = "App" $Installer32 = "App.exe" $Installer64 = "App.exe" $arguments = "/S" $uninstaller32or64 = "App\Uninstall.exe" $argumentsUninstall = "/S" $Global:IsMsi = "0" #IsMsi = "0" or "1" # --------------- Ne rien modifier apres cette ligne --------------- # ------------- Do not modify anything after this line ------------- $scriptPath = split-path -parent $MyInvocation.MyCommand.Definition $Global:IsCopyNeed = "0" # $CopyNeed = "0" or "1" $SourceConfig = "$scriptPath\ConfigApp" $DestinationConfig = "%AppData%\App" $Global:DestinationConfig = "%AppData%_or_ProgramFiles\App" $StartJob1 = "1" # $StartJob = "0" or "1" $NameJob = "StopApp" $ProcName = "AppProcName" Write-Host "Debut installation $NameApp" -ForegroundColor Green function CopyFilesToFolder ($fromFolder, $toFolder) { # Make sure the destination folder exists If(!(Test-Path $toFolder)){New-Item $toFolder -ItemType Directory -Force|Out-null} # Copy source folder contents to destination folder recursively Copy-Item "$fromFolder\*" -Destination "$toFolder" -recurse -force } function StartJob1PowerShell($NameJob1, $ProcName1) { Start-Job -Name "$NameJob1" -ScriptBlock { while($true) { Get-Process | where {$_.ProcessName -like "$using:ProcName1"} | Stop-Process; Start-Sleep -Seconds 10 } } -ArgumentList $ProcName1 } # Uninstall Write-Host "Uninstall $NameApp" -ForegroundColor Cyan If ((Test-Path "${env:ProgramFiles(x86)}\$uninstaller32or64" -PathType Leaf) -or (Test-Path "${Env:ProgramFiles}\$uninstaller32or64" -PathType Leaf)) { If (Test-Path "${env:ProgramFiles(x86)}\$uninstaller32or64" -PathType Leaf) { Write-Host "TEST Desinstallation $NameApp ProgramFilesX86" -ForegroundColor Magenta If ($StartJob1 -eq "1"){ StartJob1PowerShell "$NameJob" "$ProcName"} $executableSupprFinal = "${env:ProgramFiles(x86)}\$uninstaller32or64" start-process $executableSupprFinal $argumentsUninstall -PassThru -Verb RunAs -Wait #-NoNewWindow Write-Host "Desinstallation $NameApp reussi" -ForegroundColor Yellow } elseif (Test-Path "${Env:ProgramFiles}\$uninstaller32or64" -PathType Leaf) { Write-Host "TEST Desinstallation $NameApp ProgramFiles" -ForegroundColor Magenta If ($StartJob1 -eq "1"){ StartJob1PowerShell "$NameJob" "$ProcName"} $executableSupprFinal = "${env:ProgramFiles}\$uninstaller32or64" start-process $executableSupprFinal $argumentsUninstall -PassThru -Verb RunAs -Wait #-NoNewWindow Write-Host "Desinstallation $NameApp reussi" -ForegroundColor Yellow } else { Write-Host "Desinstaller $NameApp introuvable" -ForegroundColor Red } } else { Write-Host "$NameApp NON presente" -ForegroundColor Green } # Suppression du Job créer If ($StartJob1 -eq "1") { If ( [bool](get-job -Name $NameJob -ea silentlycontinue) ) { Remove-Job -Name $NameJob -Force } Start-Sleep -Seconds 5 } # Install Write-Host "Installation $NameApp" -ForegroundColor Cyan If (Test-Path "${env:ProgramFiles(x86)}") { If ($StartJob1 -eq "1"){ StartJob1PowerShell "$NameJob" "$ProcName"} $Installer = $Installer64 $DestinationConfig = "${env:ProgramFiles}\$DestinationConfig" $InstallerFinal = "$scriptPath\$Installer" If ($IsMsi -eq "1") { start-process msiexec.exe -ArgumentList '/i',$InstallerFinal,$arguments -PassThru -Verb RunAs -Wait #-NoNewWindow } Else { start-process $InstallerFinal $arguments -PassThru -Verb RunAs -Wait #-NoNewWindow } } Else { If ($StartJob1 -eq "1"){ StartJob1PowerShell "$NameJob" "$ProcName"} $Installer = $Installer32 $DestinationConfig = "${env:ProgramFiles(x86)}\$DestinationConfig" $InstallerFinal = "$scriptPath\$Installer" If ($IsMsi -eq "1") { start-process msiexec.exe -ArgumentList '/i',$InstallerFinal,$arguments -PassThru -Verb RunAs -Wait #-NoNewWindow } Else { start-process $InstallerFinal $arguments -PassThru -Verb RunAs -Wait #-NoNewWindow } } # Suppression du Job créer If ($StartJob1 -eq "1") { If ( [bool](get-job -Name $NameJob -ea silentlycontinue) ) { Remove-Job -Name $NameJob -Force } Start-Sleep -Seconds 5 } # Copy Config Item from Deployroot If ($IsCopyNeed -eq "1") { Start-Sleep -Seconds 2 Write-Host "Copie auxiliere $NameApp" -ForegroundColor Cyan CopyFilesToFolder "$SourceConfig" "$DestinationConfig" } Write-Host "Fin install $NameApp" -ForegroundColor Green