Skip to content

Commit 8dcf79a

Browse files
author
Jonathan Pitre
committed
Update script header
Rename Get-ScriptDirectory to Get-ScriptPath Fix Get-ScriptPath when running with powershell.exe Add unblock of ps1 script Add Get-ScriptName Fix module import issue Add regions
1 parent 1e62fd5 commit 8dcf79a

File tree

85 files changed

+5385
-1882
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+5385
-1882
lines changed

7-Zip/Install.ps1

+68-21
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,83 @@
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
22

33
#Requires -Version 5.1
44
#Requires -RunAsAdministrator
55

66
#---------------------------------------------------------[Initialisations]--------------------------------------------------------
77

8+
#region Initialisations
89
$ProgressPreference = "SilentlyContinue"
910
$ErrorActionPreference = "SilentlyContinue"
1011
# Set the script execution policy for this process
1112
Try { Set-ExecutionPolicy -ExecutionPolicy 'ByPass' -Scope 'Process' -Force } Catch {}
13+
# Unblock ps1 script
14+
Get-ChildItem -Recurse *.ps*1 | Unblock-File
1215
$env:SEE_MASK_NOZONECHECKS = 1
1316
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
1417
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
1518
$Modules = @("PSADT", "Evergreen") # Modules list
1619

17-
Function Get-ScriptDirectory
20+
Function Get-ScriptPath
1821
{
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
2133
{
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
2544
Else
2645
{
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
2847
Exit 1
2948
}
3049
}
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
3264
{
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+
}
3579
}
3680
}
37-
38-
Function Initialize-Module
3981
{
4082
[CmdletBinding()]
4183
Param
@@ -53,7 +95,8 @@ Function Initialize-Module
5395
Else
5496
{
5597
# 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+
57100
{
58101
$InstalledModuleVersion = (Get-InstalledModule -Name $Module).Version
59102
$ModuleVersion = (Find-Module -Name $Module).Version
@@ -111,17 +154,21 @@ Function Initialize-Module
111154
}
112155
}
113156

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
116159

117160
# Install and import modules list
118161
Foreach ($Module in $Modules)
119162
{
120163
Initialize-Module -Module $Module
121164
}
165+
#endregion
122166

123167
#-----------------------------------------------------------[Functions]------------------------------------------------------------
124168

169+
#region Functions
170+
#endregion
171+
125172
#----------------------------------------------------------[Declarations]----------------------------------------------------------
126173

127174
$appName = "7-Zip"
@@ -143,12 +190,12 @@ $appInstalledVersion = ((Get-InstalledApplication -Name "$appName.*" -RegEx).Dis
143190

144191
If ([version]$appVersion -gt [version]$appInstalledVersion)
145192
{
146-
Set-Location -Path $appScriptDirectory
193+
Set-Location -Path $appScriptPath
147194
If (-Not(Test-Path -Path $appVersion)) { New-Folder -Path $appVersion }
148195
Set-Location -Path $appVersion
149196

150197
# Download latest setup file(s)
151-
If (-Not(Test-Path -Path $appScriptDirectory\$appVersion\$appSetup))
198+
If (-Not(Test-Path -Path $appScriptPath\$appVersion\$appSetup))
152199
{
153200
Write-Log -Message "Downloading $appName $appVersion..." -Severity 1 -LogType CMTrace -WriteHost $True
154201
Invoke-WebRequest -UseBasicParsing -Uri $appURL -OutFile $appSetup
@@ -169,10 +216,10 @@ If ([version]$appVersion -gt [version]$appInstalledVersion)
169216
}
170217

171218
# Download required transform file
172-
If (-Not(Test-Path -Path $appScriptDirectory\$appTransform))
219+
If (-Not(Test-Path -Path $appScriptPath\$appTransform))
173220
{
174221
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
176223
}
177224
Else
178225
{
@@ -181,7 +228,7 @@ If ([version]$appVersion -gt [version]$appInstalledVersion)
181228

182229
# Install latest version
183230
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"
185232

186233
Write-Log -Message "Applying customizations..." -Severity 1 -LogType CMTrace -WriteHost $True
187234

0 commit comments

Comments
 (0)