Advertisement
Guest User

PSADT

a guest
Oct 9th, 2021
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3.     This script performs the installation or uninstallation of an application(s).
  4.     # LICENSE #
  5.     PowerShell App Deployment Toolkit - Provides a set of functions to perform common application deployment tasks on Windows.
  6.     Copyright (C) 2017 - Sean Lillis, Dan Cunningham, Muhammad Mashwani, Aman Motazedian.
  7.     This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  8.     You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
  9. .DESCRIPTION
  10.     The script is provided as a template to perform an install or uninstall of an application(s).
  11.     The script either performs an "Install" deployment type or an "Uninstall" deployment type.
  12.     The install deployment type is broken down into 3 main sections/phases: Pre-Install, Install, and Post-Install.
  13.     The script dot-sources the AppDeployToolkitMain.ps1 script which contains the logic and functions required to install or uninstall an application.
  14. .PARAMETER DeploymentType
  15.     The type of deployment to perform. Default is: Install.
  16. .PARAMETER DeployMode
  17.     Specifies whether the installation should be run in Interactive, Silent, or NonInteractive mode. Default is: Interactive. Options: Interactive = Shows dialogs, Silent = No dialogs, NonInteractive = Very silent, i.e. no blocking apps. NonInteractive mode is automatically set if it is detected that the process is not user interactive.
  18. .PARAMETER AllowRebootPassThru
  19.     Allows the 3010 return code (requires restart) to be passed back to the parent process (e.g. SCCM) if detected from an installation. If 3010 is passed back to SCCM, a reboot prompt will be triggered.
  20. .PARAMETER TerminalServerMode
  21.     Changes to "user install mode" and back to "user execute mode" for installing/uninstalling applications for Remote Destkop Session Hosts/Citrix servers.
  22. .PARAMETER DisableLogging
  23.     Disables logging to file for the script. Default is: $false.
  24. .EXAMPLE
  25.     powershell.exe -Command "& { & '.\Deploy-Application.ps1' -DeployMode 'Silent'; Exit $LastExitCode }"
  26. .EXAMPLE
  27.     powershell.exe -Command "& { & '.\Deploy-Application.ps1' -AllowRebootPassThru; Exit $LastExitCode }"
  28. .EXAMPLE
  29.     powershell.exe -Command "& { & '.\Deploy-Application.ps1' -DeploymentType 'Uninstall'; Exit $LastExitCode }"
  30. .EXAMPLE
  31.     Deploy-Application.exe -DeploymentType "Install" -DeployMode "Silent"
  32. .NOTES
  33.     Toolkit Exit Code Ranges:
  34.     60000 - 68999: Reserved for built-in exit codes in Deploy-Application.ps1, Deploy-Application.exe, and AppDeployToolkitMain.ps1
  35.     69000 - 69999: Recommended for user customized exit codes in Deploy-Application.ps1
  36.     70000 - 79999: Recommended for user customized exit codes in AppDeployToolkitExtensions.ps1
  37. .LINK
  38.     http://psappdeploytoolkit.com
  39. #>
  40. [CmdletBinding()]
  41. Param (
  42.     [Parameter(Mandatory=$false)]
  43.     [ValidateSet('Install','Uninstall','Repair')]
  44.     [string]$DeploymentType = 'Install',
  45.     [Parameter(Mandatory=$false)]
  46.     [ValidateSet('Interactive','Silent','NonInteractive')]
  47.     [string]$DeployMode = 'Interactive',
  48.     [Parameter(Mandatory=$false)]
  49.     [switch]$AllowRebootPassThru = $false,
  50.     [Parameter(Mandatory=$false)]
  51.     [switch]$TerminalServerMode = $false,
  52.     [Parameter(Mandatory=$false)]
  53.     [switch]$DisableLogging = $false
  54. )
  55.  
  56. Try {
  57.     ## Set the script execution policy for this process
  58.     Try { Set-ExecutionPolicy -ExecutionPolicy 'ByPass' -Scope 'Process' -Force -ErrorAction 'Stop' } Catch {}
  59.  
  60.     ##*===============================================
  61.     ##* VARIABLE DECLARATION
  62.     ##*===============================================
  63.     ## Variables: Application
  64.     [string]$appVendor = 'AppVendor'
  65.     [string]$appName = 'AppName'
  66.     [string]$appVersion = 'x.y.z.w'
  67.     [string]$appArch = ''
  68.     [string]$appLang = 'EN'
  69.     [string]$appRevision = '01'
  70.     [string]$appScriptVersion = '1.0.0'
  71.     [string]$appScriptDate = 'XX/XX/20XX'
  72.     [string]$appScriptAuthor = 'Script Author'
  73.     ##*===============================================
  74.     ## Variables: Install Titles (Only set here to override defaults set by the toolkit)
  75.     [string]$installName = ''
  76.     [string]$installTitle = ''
  77.  
  78.     ##* Do not modify section below
  79.     #region DoNotModify
  80.  
  81.     ## Variables: Exit Code
  82.     [int32]$mainExitCode = 0
  83.  
  84.     ## Variables: Script
  85.     [string]$deployAppScriptFriendlyName = 'Deploy Application'
  86.     [version]$deployAppScriptVersion = [version]'3.8.4'
  87.     [string]$deployAppScriptDate = '26/01/2021'
  88.     [hashtable]$deployAppScriptParameters = $psBoundParameters
  89.  
  90.     ## Variables: Environment
  91.     If (Test-Path -LiteralPath 'variable:HostInvocation') { $InvocationInfo = $HostInvocation } Else { $InvocationInfo = $MyInvocation }
  92.     [string]$scriptDirectory = Split-Path -Path $InvocationInfo.MyCommand.Definition -Parent
  93.  
  94.     ## Dot source the required App Deploy Toolkit Functions
  95.     Try {
  96.         [string]$moduleAppDeployToolkitMain = "$scriptDirectory\AppDeployToolkit\AppDeployToolkitMain.ps1"
  97.         If (-not (Test-Path -LiteralPath $moduleAppDeployToolkitMain -PathType 'Leaf')) { Throw "Module does not exist at the specified location [$moduleAppDeployToolkitMain]." }
  98.         If ($DisableLogging) { . $moduleAppDeployToolkitMain -DisableLogging } Else { . $moduleAppDeployToolkitMain }
  99.     }
  100.     Catch {
  101.         If ($mainExitCode -eq 0){ [int32]$mainExitCode = 60008 }
  102.         Write-Error -Message "Module [$moduleAppDeployToolkitMain] failed to load: `n$($_.Exception.Message)`n `n$($_.InvocationInfo.PositionMessage)" -ErrorAction 'Continue'
  103.         ## Exit the script, returning the exit code to SCCM
  104.         If (Test-Path -LiteralPath 'variable:HostInvocation') { $script:ExitCode = $mainExitCode; Exit } Else { Exit $mainExitCode }
  105.     }
  106.  
  107.     #endregion
  108.     ##* Do not modify section above
  109.     ##*===============================================
  110.     ##* END VARIABLE DECLARATION
  111.     ##*===============================================
  112.  
  113.     If ($deploymentType -ine 'Uninstall' -and $deploymentType -ine 'Repair') {
  114.         ##*===============================================
  115.         ##* PRE-INSTALLATION
  116.         ##*===============================================
  117.         [string]$installPhase = 'Pre-Installation'
  118.  
  119.         ## Show Welcome Message, close Internet Explorer if required, allow up to 3 deferrals, verify there is enough disk space to complete the install, and persist the prompt
  120.         Show-InstallationWelcome -CloseApps 'iexplore' -AllowDefer -DeferTimes 3 -CheckDiskSpace -PersistPrompt
  121.  
  122.         ## Show Progress Message (with the default message)
  123.         Show-InstallationProgress
  124.  
  125.         ## <Perform Pre-Installation tasks here>
  126.  
  127.  
  128.         ##*===============================================
  129.         ##* INSTALLATION
  130.         ##*===============================================
  131.         [string]$installPhase = 'Installation'
  132.  
  133.         ## Handle Zero-Config MSI Installations
  134.         If ($useDefaultMsi) {
  135.             [hashtable]$ExecuteDefaultMSISplat =  @{ Action = 'Install'; Path = $defaultMsiFile }; If ($defaultMstFile) { $ExecuteDefaultMSISplat.Add('Transform', $defaultMstFile) }
  136.             Execute-MSI @ExecuteDefaultMSISplat; If ($defaultMspFiles) { $defaultMspFiles | ForEach-Object { Execute-MSI -Action 'Patch' -Path $_ } }
  137.         }
  138.  
  139.         ## <Perform Installation tasks here>
  140.  
  141.  
  142.         ##*===============================================
  143.         ##* POST-INSTALLATION
  144.         ##*===============================================
  145.         [string]$installPhase = 'Post-Installation'
  146.  
  147.         ## <Perform Post-Installation tasks here>
  148.  
  149.         ## Display a message at the end of the install
  150.         If (-not $useDefaultMsi) { Show-InstallationPrompt -Message 'You can customize text to appear at the end of an install or remove it completely for unattended installations.' -ButtonRightText 'OK' -Icon Information -NoWait }
  151.     }
  152.     ElseIf ($deploymentType -ieq 'Uninstall')
  153.     {
  154.         ##*===============================================
  155.         ##* PRE-UNINSTALLATION
  156.         ##*===============================================
  157.         [string]$installPhase = 'Pre-Uninstallation'
  158.  
  159.         ## Show Welcome Message, close Internet Explorer with a 60 second countdown before automatically closing
  160.         Show-InstallationWelcome -CloseApps 'iexplore' -CloseAppsCountdown 60
  161.  
  162.         ## Show Progress Message (with the default message)
  163.         Show-InstallationProgress
  164.  
  165.         ## <Perform Pre-Uninstallation tasks here>
  166.  
  167.  
  168.         ##*===============================================
  169.         ##* UNINSTALLATION
  170.         ##*===============================================
  171.         [string]$installPhase = 'Uninstallation'
  172.  
  173.         ## Handle Zero-Config MSI Uninstallations
  174.         If ($useDefaultMsi) {
  175.             [hashtable]$ExecuteDefaultMSISplat =  @{ Action = 'Uninstall'; Path = $defaultMsiFile }; If ($defaultMstFile) { $ExecuteDefaultMSISplat.Add('Transform', $defaultMstFile) }
  176.             Execute-MSI @ExecuteDefaultMSISplat
  177.         }
  178.  
  179.         # <Perform Uninstallation tasks here>
  180.  
  181.  
  182.         ##*===============================================
  183.         ##* POST-UNINSTALLATION
  184.         ##*===============================================
  185.         [string]$installPhase = 'Post-Uninstallation'
  186.  
  187.         ## <Perform Post-Uninstallation tasks here>
  188.  
  189.  
  190.     }
  191.     ElseIf ($deploymentType -ieq 'Repair')
  192.     {
  193.         ##*===============================================
  194.         ##* PRE-REPAIR
  195.         ##*===============================================
  196.         [string]$installPhase = 'Pre-Repair'
  197.  
  198.         ## Show Progress Message (with the default message)
  199.         Show-InstallationProgress
  200.  
  201.         ## <Perform Pre-Repair tasks here>
  202.  
  203.         ##*===============================================
  204.         ##* REPAIR
  205.         ##*===============================================
  206.         [string]$installPhase = 'Repair'
  207.  
  208.         ## Handle Zero-Config MSI Repairs
  209.         If ($useDefaultMsi) {
  210.             [hashtable]$ExecuteDefaultMSISplat =  @{ Action = 'Repair'; Path = $defaultMsiFile; }; If ($defaultMstFile) { $ExecuteDefaultMSISplat.Add('Transform', $defaultMstFile) }
  211.             Execute-MSI @ExecuteDefaultMSISplat
  212.         }
  213.         # <Perform Repair tasks here>
  214.  
  215.         ##*===============================================
  216.         ##* POST-REPAIR
  217.         ##*===============================================
  218.         [string]$installPhase = 'Post-Repair'
  219.  
  220.         ## <Perform Post-Repair tasks here>
  221.  
  222.  
  223.     }
  224.     ##*===============================================
  225.     ##* END SCRIPT BODY
  226.     ##*===============================================
  227.  
  228.     ## Call the Exit-Script function to perform final cleanup operations
  229.     Exit-Script -ExitCode $mainExitCode
  230. }
  231. Catch {
  232.     [int32]$mainExitCode = 60001
  233.     [string]$mainErrorMessage = "$(Resolve-Error)"
  234.     Write-Log -Message $mainErrorMessage -Severity 3 -Source $deployAppScriptFriendlyName
  235.     Show-DialogBox -Text $mainErrorMessage -Icon 'Stop'
  236.     Exit-Script -ExitCode $mainExitCode
  237. }
  238.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement