1
- # Standalone application install script for VDI environment - (C)2022 Jonathan Pitre, inspired by xenappblog.com
1
+ # Standalone application install script for VDI environment - (C)2023 Jonathan Pitre
2
2
3
3
# Requires -Version 5.1
4
4
# Requires -RunAsAdministrator
5
5
6
6
# ---------------------------------------------------------[Initialisations]--------------------------------------------------------
7
7
8
+ # region Initialisations
8
9
$ProgressPreference = " SilentlyContinue"
9
10
$ErrorActionPreference = " SilentlyContinue"
10
11
# Set the script execution policy for this process
11
12
Try { Set-ExecutionPolicy - ExecutionPolicy ' ByPass' - Scope ' Process' - Force } Catch {}
13
+ # Unblock ps1 script
14
+ Get-ChildItem - Recurse * .ps* 1 | Unblock-File
12
15
$env: SEE_MASK_NOZONECHECKS = 1
13
16
[Net.ServicePointManager ]::SecurityProtocol = [Net.SecurityProtocolType ]::Tls12
14
17
[System.Net.WebRequest ]::DefaultWebProxy.Credentials = [System.Net.CredentialCache ]::DefaultCredentials
15
18
$Modules = @ (" PSADT" , " Evergreen" ) # Modules list
16
19
17
- Function Get-ScriptDirectory
20
+ Function Get-ScriptPath
18
21
{
19
- Remove-Variable appScriptDirectory
20
- Try
22
+ <#
23
+ . SYNOPSIS
24
+ Get-ScriptPath returns the path of the current script.
25
+ . OUTPUTS
26
+ System.String
27
+ #>
28
+ [CmdletBinding ()]
29
+ [OutputType ([string ])]
30
+ Param ()
31
+
32
+ Begin
21
33
{
22
- If ($psEditor ) { Split-Path $psEditor.GetEditorContext ().CurrentFile.Path } # Visual Studio Code Host
23
- ElseIf ($psISE ) { Split-Path $psISE.CurrentFile.FullPath } # Windows PowerShell ISE Host
24
- ElseIf ($PSScriptRoot ) { $PSScriptRoot } # Windows PowerShell 3.0-5.1
34
+ Remove-Variable appScriptPath
35
+ }
36
+ Process
37
+ {
38
+ If ($psEditor ) { Split-Path - Path $psEditor.GetEditorContext ().CurrentFile.Path } # Visual Studio Code
39
+ ElseIf ($MyInvocation.MyCommand.CommandType -eq " ExternalScript" ) { Split-Path - Path $My $MyInvocation.MyCommand.Source } # PS1 converted to EXE
40
+ ElseIf ($null -ne $HostInvocation ) { $HostInvocation.MyCommand.Path } # SAPIEN PowerShell Studio
41
+ ElseIf ($psISE ) { Split-Path - Path $psISE.CurrentFile.FullPath } # Windows PowerShell ISE
42
+ ElseIf ($MyInvocation.PSScriptRoot ) { $MyInvocation.PSScriptRoot } # Windows PowerShell 3.0+
43
+ ElseIf ($MyInvocation.MyCommand.Path ) { Split-Path - Path $MyInvocation.MyCommand.Path - Parent } # Windows PowerShell
25
44
Else
26
45
{
27
- Write-Host - Object " Cannot resolve script file 's path" - ForegroundColor Red
46
+ Write-Host - Object " Unable to resolve script's file path! " - ForegroundColor Red
28
47
Exit 1
29
48
}
30
49
}
31
- Catch
50
+ }
51
+
52
+ Function Get-ScriptName
53
+ {
54
+ <#
55
+ . SYNOPSIS
56
+ Get-ScriptName returns the name of the current script.
57
+ . OUTPUTS
58
+ System.String
59
+ #>
60
+ [CmdletBinding ()]
61
+ [OutputType ([string ])]
62
+ Param ()
63
+ Begin
32
64
{
33
- Write-Host - Object " Caught Exception: $ ( $Error [0 ].Exception.Message) " - ForegroundColor Red
34
- Exit 2
65
+ Remove-Variable appScriptName
66
+ }
67
+ Process
68
+ {
69
+ If ($psEditor ) { Split-Path - Path $psEditor.GetEditorContext ().CurrentFile.Path - Leaf } # Visual Studio Code Host
70
+ ElseIf ($psEXE ) { [System.Diagnotics.Process ]::GetCurrentProcess.Name } # PS1 converted to EXE
71
+ ElseIf ($null -ne $HostInvocation ) { $HostInvocation.MyCommand.Name } # SAPIEN PowerShell Studio
72
+ ElseIf ($psISE ) { $psISE.CurrentFile.DisplayName.Trim (" *" ) } # Windows PowerShell ISE
73
+ ElseIf ($MyInvocation.MyCommand.Name ) { $MyInvocation.MyCommand.Name } # Windows PowerShell
74
+ Else
75
+ {
76
+ Write-Host - Object " Uanble to resolve script's file name!" - ForegroundColor Red
77
+ Exit 1
78
+ }
35
79
}
36
80
}
37
-
38
- Function Initialize-Module
39
81
{
40
82
[CmdletBinding ()]
41
83
Param
@@ -53,7 +95,8 @@ Function Initialize-Module
53
95
Else
54
96
{
55
97
# If module is not imported, but available on disk then import
56
- If (Get-Module - ListAvailable | Where-Object { $_.Name -eq $Module })
98
+ If ( [boolean ](Get-Module - ListAvailable | Where-Object { $_.Name -eq $Module }) )
99
+
57
100
{
58
101
$InstalledModuleVersion = (Get-InstalledModule - Name $Module ).Version
59
102
$ModuleVersion = (Find-Module - Name $Module ).Version
@@ -111,17 +154,21 @@ Function Initialize-Module
111
154
}
112
155
}
113
156
114
- # Get the current script directory
115
- $appScriptDirectory = Get-ScriptDirectory
157
+ [ string ] $appScriptPath = Get-ScriptPath # Get the current script path
158
+ [ string ] $appScriptName = Get-ScriptName # Get the current script name
116
159
117
160
# Install and import modules list
118
161
Foreach ($Module in $Modules )
119
162
{
120
163
Initialize-Module - Module $Module
121
164
}
165
+ # endregion
122
166
123
167
# -----------------------------------------------------------[Functions]------------------------------------------------------------
124
168
169
+ # region Functions
170
+ # endregion
171
+
125
172
# ----------------------------------------------------------[Declarations]----------------------------------------------------------
126
173
127
174
$appName = " 7-Zip"
@@ -143,12 +190,12 @@ $appInstalledVersion = ((Get-InstalledApplication -Name "$appName.*" -RegEx).Dis
143
190
144
191
If ([version ]$appVersion -gt [version ]$appInstalledVersion )
145
192
{
146
- Set-Location - Path $appScriptDirectory
193
+ Set-Location - Path $appScriptPath
147
194
If (-Not (Test-Path - Path $appVersion )) { New-Folder - Path $appVersion }
148
195
Set-Location - Path $appVersion
149
196
150
197
# Download latest setup file(s)
151
- If (-Not (Test-Path - Path $appScriptDirectory \$appVersion \$appSetup ))
198
+ If (-Not (Test-Path - Path $appScriptPath \$appVersion \$appSetup ))
152
199
{
153
200
Write-Log - Message " Downloading $appName $appVersion ..." - Severity 1 - LogType CMTrace - WriteHost $True
154
201
Invoke-WebRequest - UseBasicParsing - Uri $appURL - OutFile $appSetup
@@ -169,10 +216,10 @@ If ([version]$appVersion -gt [version]$appInstalledVersion)
169
216
}
170
217
171
218
# Download required transform file
172
- If (-Not (Test-Path - Path $appScriptDirectory \$appTransform ))
219
+ If (-Not (Test-Path - Path $appScriptPath \$appTransform ))
173
220
{
174
221
Write-Log - Message " Downloading $appName Transform.." - Severity 1 - LogType CMTrace - WriteHost $True
175
- Invoke-WebRequest - UseBasicParsing - Uri $appTransformURL - OutFile $appScriptDirectory \$appTransform
222
+ Invoke-WebRequest - UseBasicParsing - Uri $appTransformURL - OutFile $appScriptPath \$appTransform
176
223
}
177
224
Else
178
225
{
@@ -181,7 +228,7 @@ If ([version]$appVersion -gt [version]$appInstalledVersion)
181
228
182
229
# Install latest version
183
230
Write-Log - Message " Installing $appName $appVersion ..." - Severity 1 - LogType CMTrace - WriteHost $True
184
- Execute- MSI - Action Install - Path $appSetup - Parameters $appInstallParameters - Transform " $appScriptDirectory \$appTransform "
231
+ Execute- MSI - Action Install - Path $appSetup - Parameters $appInstallParameters - Transform " $appScriptPath \$appTransform "
185
232
186
233
Write-Log - Message " Applying customizations..." - Severity 1 - LogType CMTrace - WriteHost $True
187
234
0 commit comments