1
+ # Standalone application install script for VDI environment - (C)2023 Jonathan Pitre
2
+
3
+ # Requires -Version 5.1
4
+ # Requires -RunAsAdministrator
5
+
6
+ # ---------------------------------------------------------[Initialisations]--------------------------------------------------------
7
+
8
+ # region Initialisations
9
+
10
+ $ProgressPreference = " SilentlyContinue"
11
+ $ErrorActionPreference = " SilentlyContinue"
12
+ # Set the script execution policy for this process
13
+ Try { Set-ExecutionPolicy - ExecutionPolicy ' ByPass' - Scope ' Process' - Force } Catch {}
14
+ # Unblock ps1 script
15
+ Get-ChildItem - Recurse * .ps* 1 | Unblock-File
16
+ $env: SEE_MASK_NOZONECHECKS = 1
17
+ [Net.ServicePointManager ]::SecurityProtocol = [Net.SecurityProtocolType ]::Tls12
18
+ [System.Net.WebRequest ]::DefaultWebProxy.Credentials = [System.Net.CredentialCache ]::DefaultCredentials
19
+ $Modules = @ (" PSADT" , " Evergreen" ) # Modules list
20
+
21
+ Function Get-ScriptPath
22
+ {
23
+ <#
24
+ . SYNOPSIS
25
+ Get-ScriptPath returns the path of the current script.
26
+ . OUTPUTS
27
+ System.String
28
+ #>
29
+ [CmdletBinding ()]
30
+ [OutputType ([string ])]
31
+ Param ()
32
+
33
+ Begin
34
+ {
35
+ Remove-Variable appScriptPath
36
+ }
37
+ Process
38
+ {
39
+ If ($psEditor ) { Split-Path - Path $psEditor.GetEditorContext ().CurrentFile.Path } # Visual Studio Code
40
+ ElseIf ($MyInvocation.MyCommand.CommandType -eq " ExternalScript" ) { Split-Path - Path $My $MyInvocation.MyCommand.Source } # PS1 converted to EXE
41
+ ElseIf ($null -ne $HostInvocation ) { $HostInvocation.MyCommand.Path } # SAPIEN PowerShell Studio
42
+ ElseIf ($psISE ) { Split-Path - Path $psISE.CurrentFile.FullPath } # Windows PowerShell ISE
43
+ ElseIf ($MyInvocation.PSScriptRoot ) { $MyInvocation.PSScriptRoot } # Windows PowerShell 3.0+
44
+ ElseIf ($MyInvocation.MyCommand.Path ) { Split-Path - Path $MyInvocation.MyCommand.Path - Parent } # Windows PowerShell
45
+ Else
46
+ {
47
+ Write-Host - Object " Unable to resolve script's file path!" - ForegroundColor Red
48
+ Exit 1
49
+ }
50
+ }
51
+ }
52
+
53
+ Function Get-ScriptName
54
+ {
55
+ <#
56
+ . SYNOPSIS
57
+ Get-ScriptName returns the name of the current script.
58
+ . OUTPUTS
59
+ System.String
60
+ #>
61
+ [CmdletBinding ()]
62
+ [OutputType ([string ])]
63
+ Param ()
64
+ Begin
65
+ {
66
+ Remove-Variable appScriptName
67
+ }
68
+ Process
69
+ {
70
+ If ($psEditor ) { Split-Path - Path $psEditor.GetEditorContext ().CurrentFile.Path - Leaf } # Visual Studio Code Host
71
+ ElseIf ($psEXE ) { [System.Diagnotics.Process ]::GetCurrentProcess.Name } # PS1 converted to EXE
72
+ ElseIf ($null -ne $HostInvocation ) { $HostInvocation.MyCommand.Name } # SAPIEN PowerShell Studio
73
+ ElseIf ($psISE ) { $psISE.CurrentFile.DisplayName.Trim (" *" ) } # Windows PowerShell ISE
74
+ ElseIf ($MyInvocation.PSCommandPath ) { Split-Path - Path $MyInvocation.PSCommandPath - Leaf } # Windows PowerShell
75
+ Else
76
+ {
77
+ Write-Host - Object " Uanble to resolve script's file name!" - ForegroundColor Red
78
+ Exit 1
79
+ }
80
+ }
81
+ }
82
+
83
+ Function Initialize-Module
84
+ {
85
+ <#
86
+ . SYNOPSIS
87
+ Initialize-Module install and import modules from PowerShell Galllery.
88
+ . OUTPUTS
89
+ System.String
90
+ #>
91
+ [CmdletBinding ()]
92
+ Param
93
+ (
94
+ [Parameter (Mandatory = $true )]
95
+ [string ]$Module
96
+ )
97
+ Write-Host - Object " Importing $Module module..." - ForegroundColor Green
98
+
99
+ # If module is imported say that and do nothing
100
+ If (Get-Module | Where-Object { $_.Name -eq $Module })
101
+ {
102
+ Write-Host - Object " Module $Module is already imported." - ForegroundColor Green
103
+ }
104
+ Else
105
+ {
106
+ # If module is not imported, but available on disk then import
107
+ If ( [boolean ](Get-Module - ListAvailable | Where-Object { $_.Name -eq $Module }) )
108
+
109
+ {
110
+ $InstalledModuleVersion = (Get-InstalledModule - Name $Module ).Version
111
+ $ModuleVersion = (Find-Module - Name $Module ).Version
112
+ $ModulePath = (Get-InstalledModule - Name $Module ).InstalledLocation
113
+ $ModulePath = (Get-Item - Path $ModulePath ).Parent.FullName
114
+ If ([version ]$ModuleVersion -gt [version ]$InstalledModuleVersion )
115
+ {
116
+ Update-Module - Name $Module - Force
117
+ Remove-Item - Path $ModulePath \$InstalledModuleVersion - Force - Recurse
118
+ Write-Host - Object " Module $Module was updated." - ForegroundColor Green
119
+ }
120
+ Import-Module - Name $Module - Force - Global - DisableNameChecking
121
+ Write-Host - Object " Module $Module was imported." - ForegroundColor Green
122
+ }
123
+ Else
124
+ {
125
+ # Install Nuget
126
+ If (-not (Get-PackageProvider - ListAvailable - Name NuGet))
127
+ {
128
+ Install-PackageProvider - Name NuGet - MinimumVersion 2.8 .5.201 - Force
129
+ Write-Host - Object " Package provider NuGet was installed." - ForegroundColor Green
130
+ }
131
+
132
+ # Add the Powershell Gallery as trusted repository
133
+ If ((Get-PSRepository - Name " PSGallery" ).InstallationPolicy -eq " Untrusted" )
134
+ {
135
+ Set-PSRepository - Name " PSGallery" - InstallationPolicy Trusted
136
+ Write-Host - Object " PowerShell Gallery is now a trusted repository." - ForegroundColor Green
137
+ }
138
+
139
+ # Update PowerShellGet
140
+ $InstalledPSGetVersion = (Get-PackageProvider - Name PowerShellGet).Version
141
+ $PSGetVersion = [version ](Find-PackageProvider - Name PowerShellGet).Version
142
+ If ($PSGetVersion -gt $InstalledPSGetVersion )
143
+ {
144
+ Install-PackageProvider - Name PowerShellGet - Force
145
+ Write-Host - Object " PowerShellGet Gallery was updated." - ForegroundColor Green
146
+ }
147
+
148
+ # If module is not imported, not available on disk, but is in online gallery then install and import
149
+ If (Find-Module - Name $Module | Where-Object { $_.Name -eq $Module })
150
+ {
151
+ # Install and import module
152
+ Install-Module - Name $Module - AllowClobber - Force - Scope AllUsers
153
+ Import-Module - Name $Module - Force - Global - DisableNameChecking
154
+ Write-Host - Object " Module $Module was installed and imported." - ForegroundColor Green
155
+ }
156
+ Else
157
+ {
158
+ # If the module is not imported, not available and not in the online gallery then abort
159
+ Write-Host - Object " Module $Module was not imported, not available and not in an online gallery, exiting." - ForegroundColor Red
160
+ EXIT 1
161
+ }
162
+ }
163
+ }
164
+ }
165
+
166
+ [string ]$appScriptPath = Get-ScriptPath # Get the current script path
167
+ [string ]$appScriptName = Get-ScriptName # Get the current script name
168
+
169
+ # Install and import modules list
170
+ Foreach ($Module in $Modules )
171
+ {
172
+ Initialize-Module - Module $Module
173
+ }
174
+
175
+ # endregion
176
+
177
+ # -----------------------------------------------------------[Functions]------------------------------------------------------------
178
+
179
+ # region Functions
180
+ # endregion
181
+
182
+ # ----------------------------------------------------------[Declarations]----------------------------------------------------------
183
+
184
+ # region Declarations
185
+
186
+ $appName = " PDF24 Creator"
187
+ $appProcesses = @ (" pdf24" , " pdf24-Assistant" , " pdf24-Compress" , " pdf24-Creator" , " pdf24-DocTool" , " pdf24-Fax" , " pdf24-Launcher" , " pdf24-Ocr" , " pdf24-Reader" , " pdf24-Settings" , " pdf24-Toolbox" , " javaw" , " msedgewebview2" )
188
+ $appLanguage = " French"
189
+ $appInstallParameters = " /QB"
190
+ $appAddParameters = " DESKTOPICONS=No FAXPRINTER=No AUTOUPDATE=No"
191
+ $Evergreen = Get-EvergreenApp - Name GeekSoftwarePDF24Creator | Where-Object { $_.Type -eq " msi" }
192
+ $appVersion = $Evergreen.Version
193
+ $appURL = $Evergreen.URI
194
+ $appSetup = Split-Path - Path $appURL - Leaf
195
+ $appDestination = " $env: ProgramFiles \PDF24"
196
+ [boolean ]$IsAppInstalled = [boolean ](Get-InstalledApplication - Name " $appName " - Exact)
197
+ $appInstalledVersion = ((Get-InstalledApplication - Name " $appName " - Exact).DisplayVersion)
198
+
199
+ # endregion
200
+
201
+ # -----------------------------------------------------------[Execution]------------------------------------------------------------
202
+
203
+ # region Execution
204
+
205
+ If ([version ]$appVersion -gt [version ]$appInstalledVersion )
206
+ {
207
+ Set-Location - Path $appScriptPath
208
+ If (-Not (Test-Path - Path $appVersion )) { New-Folder - Path $appVersion }
209
+ Set-Location - Path $appVersion
210
+
211
+ # Download latest setup file(s)
212
+ If (-Not (Test-Path - Path $appScriptPath \$appVersion \$appSetup ))
213
+ {
214
+ Write-Log - Message " Downloading $appName $appVersion ..." - Severity 1 - LogType CMTrace - WriteHost $True
215
+ Invoke-WebRequest - UseBasicParsing - Uri $appURL - OutFile $appSetup
216
+ }
217
+ Else
218
+ {
219
+ Write-Log - Message " File(s) already exists, download was skipped." - Severity 1 - LogType CMTrace - WriteHost $True
220
+ }
221
+
222
+ If ($IsAppInstalled )
223
+ {
224
+ Write-Log - Message " Uninstalling previous versions..." - Severity 1 - LogType CMTrace - WriteHost $True
225
+ # Uninstall previous versions
226
+ Remove-MSIApplications - Name $appName - Parameters $appInstallParameters
227
+ }
228
+
229
+ # Install latest version
230
+ Write-Log - Message " Installing $appName $appVersion ..." - Severity 1 - LogType CMTrace - WriteHost $True
231
+ Execute- MSI - Action Install - Path $appSetup - Parameters $appInstallParameters - AddParameters $appAddParameters
232
+
233
+ Write-Log - Message " Applying customizations..." - Severity 1 - LogType CMTrace - WriteHost $True
234
+
235
+ # Remove startup item
236
+ Remove-RegistryKey - Key " HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" - Name " PDF24"
237
+
238
+ # Load the Default User registry hive
239
+ Start-Sleep - Seconds 5
240
+ Execute- Process - Path " $envWinDir \System32\reg.exe" - Parameters " LOAD HKLM\DefaultUser $envSystemDrive \Users\Default\NTUSER.DAT" - WindowStyle Hidden
241
+ Set-RegistryKey - Key " HKLM:\DefaultUser\Software\PDF24" - Name " Language" - Value $appLanguage - Type String
242
+ Set-RegistryKey - Key " HKLM:\DefaultUser\Software\PDF24" - Name " NoScreenCapture" - Value " 1" - Type DWord
243
+ Set-RegistryKey - Key " HKLM:\DefaultUser\Software\PDF24" - Name " NoTrayIcon" - Value " 1" - Type DWord
244
+ Set-RegistryKey - Key " HKLM:\DefaultUser\Software\PDF24" - Name " NoFax" - Value " 1" - Type DWord
245
+
246
+ # Cleanup (to prevent access denied issue unloading the registry hive)
247
+ [GC ]::Collect()
248
+ Start-Sleep - Seconds 5
249
+
250
+ # Unload the Default User registry hive
251
+ Execute- Process - Path " $envWinDir \System32\reg.exe" - Parameters " UNLOAD HKLM\DefaultUser" - WindowStyle Hidden
252
+
253
+ # Cleanup temp files
254
+ Remove-Item - Path " $envSystemDrive \Users\Default\*.LOG1" - Force
255
+ Remove-Item - Path " $envSystemDrive \Users\Default\*.LOG2" - Force
256
+ Remove-Item - Path " $envSystemDrive \Users\Default\*.blf" - Force
257
+ Remove-Item - Path " $envSystemDrive \Users\Default\*.regtrans-ms" - Force
258
+
259
+
260
+ # Go back to the parent folder
261
+ Set-Location ..
262
+
263
+ Write-Log - Message " $appName $appVersion was installed successfully!" - Severity 1 - LogType CMTrace - WriteHost $True
264
+
265
+ }
266
+ Else
267
+ {
268
+ Write-Log - Message " $appName $appInstalledVersion is already installed." - Severity 1 - LogType CMTrace - WriteHost $True
269
+ }
270
+
271
+ # endregion
0 commit comments