Advertisement
Guest User

Untitled

a guest
Feb 18th, 2025
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.45 KB | None | 0 0
  1. <#
  2.  
  3. .SYNOPSIS
  4. PSAppDeployToolkit - This script performs the installation or uninstallation of an application(s).
  5.  
  6. .DESCRIPTION
  7. - The script is provided as a template to perform an install, uninstall, or repair of an application(s).
  8. - The script either performs an "Install", "Uninstall", or "Repair" deployment type.
  9. - The install deployment type is broken down into 3 main sections/phases: Pre-Install, Install, and Post-Install.
  10.  
  11. The script imports the PSAppDeployToolkit module which contains the logic and functions required to install or uninstall an application.
  12.  
  13. PSAppDeployToolkit is licensed under the GNU LGPLv3 License - (C) 2024 PSAppDeployToolkit Team (Sean Lillis, Dan Cunningham, Muhammad Mashwani, Mitch Richters, Dan Gough).
  14.  
  15. 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
  16. 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
  17. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  18. for more details. 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/>.
  19.  
  20. .PARAMETER DeploymentType
  21. The type of deployment to perform. Default is: Install.
  22.  
  23. .PARAMETER DeployMode
  24. 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.
  25.  
  26. .PARAMETER AllowRebootPassThru
  27. 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.
  28.  
  29. .PARAMETER TerminalServerMode
  30. Changes to "user install mode" and back to "user execute mode" for installing/uninstalling applications for Remote Desktop Session Hosts/Citrix servers.
  31.  
  32. .PARAMETER DisableLogging
  33. Disables logging to file for the script. Default is: $false.
  34.  
  35. .EXAMPLE
  36. powershell.exe -File Invoke-AppDeployToolkit.ps1 -DeployMode Silent
  37.  
  38. .EXAMPLE
  39. powershell.exe -File Invoke-AppDeployToolkit.ps1 -AllowRebootPassThru
  40.  
  41. .EXAMPLE
  42. powershell.exe -File Invoke-AppDeployToolkit.ps1 -DeploymentType Uninstall
  43.  
  44. .EXAMPLE
  45. Invoke-AppDeployToolkit.exe -DeploymentType "Install" -DeployMode "Silent"
  46.  
  47. .INPUTS
  48. None. You cannot pipe objects to this script.
  49.  
  50. .OUTPUTS
  51. None. This script does not generate any output.
  52.  
  53. .NOTES
  54. Toolkit Exit Code Ranges:
  55. - 60000 - 68999: Reserved for built-in exit codes in Invoke-AppDeployToolkit.ps1, and Invoke-AppDeployToolkit.exe
  56. - 69000 - 69999: Recommended for user customized exit codes in Invoke-AppDeployToolkit.ps1
  57. - 70000 - 79999: Recommended for user customized exit codes in PSAppDeployToolkit.Extensions module.
  58.  
  59. .LINK
  60. https://psappdeploytoolkit.com
  61.  
  62. #>
  63.  
  64. [CmdletBinding()]
  65. param
  66. (
  67. [Parameter(Mandatory = $false)]
  68. [ValidateSet('Install', 'Uninstall', 'Repair')]
  69. [System.String]$DeploymentType = 'Install',
  70.  
  71. [Parameter(Mandatory = $false)]
  72. [ValidateSet('Interactive', 'Silent', 'NonInteractive')]
  73. [System.String]$DeployMode = 'Interactive',
  74.  
  75. [Parameter(Mandatory = $false)]
  76. [System.Management.Automation.SwitchParameter]$AllowRebootPassThru,
  77.  
  78. [Parameter(Mandatory = $false)]
  79. [System.Management.Automation.SwitchParameter]$TerminalServerMode,
  80.  
  81. [Parameter(Mandatory = $false)]
  82. [System.Management.Automation.SwitchParameter]$DisableLogging
  83. )
  84.  
  85.  
  86. ##================================================
  87. ## MARK: Variables
  88. ##================================================
  89.  
  90. $adtSession = @{
  91. # App variables.
  92. AppVendor = 'Microsoft'
  93. AppName = 'visual Studio Code'
  94. AppVersion = '1.97.2'
  95. AppArch = 'x64'
  96. AppLang = 'EN'
  97. AppRevision = '01'
  98. AppSuccessExitCodes = @(0)
  99. AppRebootExitCodes = @(1641, 3010)
  100. AppScriptVersion = '1.0.0'
  101. AppScriptDate = '2025-18-02'
  102. AppScriptAuthor = ''
  103.  
  104. # Install Titles (Only set here to override defaults set by the toolkit).
  105. InstallName = ''
  106. InstallTitle = ''
  107.  
  108. # Script variables.
  109. DeployAppScriptFriendlyName = $MyInvocation.MyCommand.Name
  110. DeployAppScriptVersion = '4.0.5'
  111. DeployAppScriptParameters = $PSBoundParameters
  112. }
  113.  
  114. function Install-ADTDeployment
  115. {
  116. ##================================================
  117. ## MARK: Pre-Install
  118. ##================================================
  119. $adtSession.InstallPhase = "Pre-$($adtSession.DeploymentType)"
  120.  
  121. ## 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.
  122. ##Used for close running apps before installation
  123. ##Show-ADTInstallationWelcome -CloseProcesses iexplore -AllowDefer -DeferTimes 3 -CheckDiskSpace -PersistPrompt
  124.  
  125. ## Show Progress Message (with the default message).
  126. Show-InstallationProgress -StatusMessage "Installation in Progress...`r`nThe installation may take 5 minutes to complete."
  127.  
  128. ## <Perform Pre-Installation tasks here>
  129.  
  130.  
  131.  
  132.  
  133. ##================================================
  134. ## MARK: Install
  135. ##================================================
  136. $adtSession.InstallPhase = $adtSession.DeploymentType
  137.  
  138. ## Handle Zero-Config MSI installations.
  139. if ($adtSession.UseDefaultMsi)
  140. {
  141. $ExecuteDefaultMSISplat = @{ Action = $adtSession.DeploymentType; FilePath = $adtSession.DefaultMsiFile }
  142. if ($adtSession.DefaultMstFile)
  143. {
  144. $ExecuteDefaultMSISplat.Add('Transform', $adtSession.DefaultMstFile)
  145. }
  146. Start-ADTMsiProcess @ExecuteDefaultMSISplat
  147. if ($adtSession.DefaultMspFiles)
  148. {
  149. $adtSession.DefaultMspFiles | Start-ADTMsiProcess -Action Patch
  150. }
  151. }
  152.  
  153. ## <Perform Installation tasks here>
  154.  
  155. ##EXE install
  156. Start-ADTProcess -FilePath 'VSCodeSetup-x64-1.97.2.exe' -ArgumentList '/VERYSILENT' -WindowStyle 'Hidden'
  157.  
  158. ##MSI uninstall
  159. ##Start-ADTMsiProcess -Action 'Install' -FilePath 'Adobe_FlashPlayer_11.2.202.233_x64_EN.msi' -ArgumentList '/QN'
  160.  
  161. ##================================================
  162. ## MARK: Post-Install
  163. ##================================================
  164. $adtSession.InstallPhase = "Post-$($adtSession.DeploymentType)"
  165.  
  166. ## <Perform Post-Installation tasks here>
  167.  
  168.  
  169. ## Display a message at the end of the install.
  170. if (!$adtSession.UseDefaultMsi)
  171. {
  172. Show-ADTInstallationPrompt -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
  173. }
  174. }
  175.  
  176. function Uninstall-ADTDeployment
  177. {
  178. ##================================================
  179. ## MARK: Pre-Uninstall
  180. ##================================================
  181. $adtSession.InstallPhase = "Pre-$($adtSession.DeploymentType)"
  182.  
  183. ## Show Welcome Message, close Internet Explorer with a 60 second countdown before automatically closing.
  184. ##Used for close running apps before uninstall begin
  185. ##Show-ADTInstallationWelcome -CloseProcesses iexplore -CloseProcessesCountdown 60
  186.  
  187. ## Show Progress Message (with the default message).
  188. Show-ADTInstallationProgress -StatusMessage "Uninstallation in Progress...`r`nThe uninstallation may take 5 minutes to complete."
  189.  
  190. ## <Perform Pre-Uninstallation tasks here>
  191.  
  192.  
  193. ##================================================
  194. ## MARK: Uninstall
  195. ##================================================
  196. $adtSession.InstallPhase = $adtSession.DeploymentType
  197.  
  198. ## Handle Zero-Config MSI uninstallations.
  199. if ($adtSession.UseDefaultMsi)
  200. {
  201. $ExecuteDefaultMSISplat = @{ Action = $adtSession.DeploymentType; FilePath = $adtSession.DefaultMsiFile }
  202. if ($adtSession.DefaultMstFile)
  203. {
  204. $ExecuteDefaultMSISplat.Add('Transform', $adtSession.DefaultMstFile)
  205. }
  206. Start-ADTMsiProcess @ExecuteDefaultMSISplat
  207. }
  208.  
  209. ## <Perform Uninstallation tasks here>
  210.  
  211. ##EXE Uninstall
  212. Start-ADTProcess -FilePath '$envProgramFiles\Microsoft VS Code\unins000.exe' -ArgumentList '/SILENT' -WindowStyle 'Hidden'
  213.  
  214.  
  215. ##MSI uninstall
  216. ##Start-ADTMsiProcess -Action 'Uninstall' -ProductCode '{26923b43-4d38-484f-9b9e-de460746276c}' -Parameters '/qb /norestart'
  217.  
  218. ##================================================
  219. ## MARK: Post-Uninstallation
  220. ##================================================
  221. $adtSession.InstallPhase = "Post-$($adtSession.DeploymentType)"
  222.  
  223. ## <Perform Post-Uninstallation tasks here>
  224. }
  225.  
  226. function Repair-ADTDeployment
  227. {
  228. ##================================================
  229. ## MARK: Pre-Repair
  230. ##================================================
  231. $adtSession.InstallPhase = "Pre-$($adtSession.DeploymentType)"
  232.  
  233. ## Show Welcome Message, close Internet Explorer with a 60 second countdown before automatically closing.
  234. Show-ADTInstallationWelcome -CloseProcesses iexplore -CloseProcessesCountdown 60
  235.  
  236. ## Show Progress Message (with the default message).
  237. Show-ADTInstallationProgress
  238.  
  239. ## <Perform Pre-Repair tasks here>
  240.  
  241.  
  242. ##================================================
  243. ## MARK: Repair
  244. ##================================================
  245. $adtSession.InstallPhase = $adtSession.DeploymentType
  246.  
  247. ## Handle Zero-Config MSI repairs.
  248. if ($adtSession.UseDefaultMsi)
  249. {
  250. $ExecuteDefaultMSISplat = @{ Action = $adtSession.DeploymentType; FilePath = $adtSession.DefaultMsiFile }
  251. if ($adtSession.DefaultMstFile)
  252. {
  253. $ExecuteDefaultMSISplat.Add('Transform', $adtSession.DefaultMstFile)
  254. }
  255. Start-ADTMsiProcess @ExecuteDefaultMSISplat
  256. }
  257.  
  258. ## <Perform Repair tasks here>
  259.  
  260.  
  261. ##================================================
  262. ## MARK: Post-Repair
  263. ##================================================
  264. $adtSession.InstallPhase = "Post-$($adtSession.DeploymentType)"
  265.  
  266. ## <Perform Post-Repair tasks here>
  267. }
  268.  
  269.  
  270. ##================================================
  271. ## MARK: Initialization
  272. ##================================================
  273.  
  274. # Set strict error handling across entire operation.
  275. $ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
  276. $ProgressPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
  277. Set-StrictMode -Version 1
  278.  
  279. # Import the module and instantiate a new session.
  280. try
  281. {
  282. $moduleName = if ([System.IO.File]::Exists("$PSScriptRoot\PSAppDeployToolkit\PSAppDeployToolkit.psd1"))
  283. {
  284. Get-ChildItem -LiteralPath $PSScriptRoot\PSAppDeployToolkit -Recurse -File | Unblock-File -ErrorAction Ignore
  285. "$PSScriptRoot\PSAppDeployToolkit\PSAppDeployToolkit.psd1"
  286. }
  287. else
  288. {
  289. 'PSAppDeployToolkit'
  290. }
  291. Import-Module -FullyQualifiedName @{ ModuleName = $moduleName; Guid = '8c3c366b-8606-4576-9f2d-4051144f7ca2'; ModuleVersion = '4.0.5' } -Force
  292. try
  293. {
  294. $iadtParams = Get-ADTBoundParametersAndDefaultValues -Invocation $MyInvocation
  295. $adtSession = Open-ADTSession -SessionState $ExecutionContext.SessionState @adtSession @iadtParams -PassThru
  296. }
  297. catch
  298. {
  299. Remove-Module -Name PSAppDeployToolkit* -Force
  300. throw
  301. }
  302. }
  303. catch
  304. {
  305. $Host.UI.WriteErrorLine((Out-String -InputObject $_ -Width ([System.Int32]::MaxValue)))
  306. exit 60008
  307. }
  308.  
  309.  
  310. ##================================================
  311. ## MARK: Invocation
  312. ##================================================
  313.  
  314. try
  315. {
  316. Get-Item -Path $PSScriptRoot\PSAppDeployToolkit.* | & {
  317. process
  318. {
  319. Get-ChildItem -LiteralPath $_.FullName -Recurse -File | Unblock-File -ErrorAction Ignore
  320. Import-Module -Name $_.FullName -Force
  321. }
  322. }
  323. & "$($adtSession.DeploymentType)-ADTDeployment"
  324. Close-ADTSession
  325. }
  326. catch
  327. {
  328. Write-ADTLogEntry -Message ($mainErrorMessage = Resolve-ADTErrorRecord -ErrorRecord $_) -Severity 3
  329. Show-ADTDialogBox -Text $mainErrorMessage -Icon Stop | Out-Null
  330. Close-ADTSession -ExitCode 60001
  331. }
  332. finally
  333. {
  334. Remove-Module -Name PSAppDeployToolkit* -Force
  335. }
  336.  
  337.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement