From cd2aebf1fff3aa6d3352650dac1aac2b76c3f448 Mon Sep 17 00:00:00 2001 From: Mindfang Date: Wed, 4 Jun 2025 16:43:25 -0500 Subject: [PATCH] More changes to make platform-agnostic --- PSUtilities/ScriptTemplate.ps1 | 24 +++------------ PSUtilities/ScriptTools.ps1 | 55 ++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 20 deletions(-) diff --git a/PSUtilities/ScriptTemplate.ps1 b/PSUtilities/ScriptTemplate.ps1 index 0d65a08..3e420b1 100644 --- a/PSUtilities/ScriptTemplate.ps1 +++ b/PSUtilities/ScriptTemplate.ps1 @@ -88,9 +88,9 @@ ForEach ($Uri in $AddonUris) { $UriStatusCode = (Invoke-WebRequest -Uri $Uri -UseBasicParsing -Method Head -SkipHttpErrorCheck).StatusCode If ($UriStatusCode -eq 200) { Try { - Invoke-WebRequest -Uri $Uri -OutFile "$Env:Temp\$(Split-Path -Path $Uri -Leaf)" -ErrorAction Stop + Invoke-WebRequest -Uri $Uri -OutFile "$([System.IO.Path]::GetTempPath())\$(Split-Path -Path $Uri -Leaf)" -ErrorAction Stop Write-Host "Importing addon: $(Split-Path -Path $Uri -Leaf)" - Import-Module -Name "$Env:Temp\$(Split-Path -Path $Uri -Leaf)" -Force + Import-Module -Name "$([System.IO.Path]::GetTempPath())\$(Split-Path -Path $Uri -Leaf)" -Force } Catch { Write-Host "Error encountered importing addon" @@ -102,6 +102,8 @@ ForEach ($Uri in $AddonUris) { Exit $UriStatusCode } } + +Import-Modules $ModuleNames #endregion Imports #region Functions @@ -131,24 +133,6 @@ If (-Not $NoLog) { } Add-LogEntry (Start-Transcript "$LogDir\$ThisScript.log" -Append) } - -# Load any modules required by script -If ($ModuleNames) { - Initialize-PSGallery - ForEach ($Module in $ModuleNames) { - If (-Not (Get-Module -ListAvailable -Name $Module)) { - Try { - Add-LogEntry "Module `"$Module`" not found, installing" - Install-Module -Name $Module -Scope CurrentUser -Force -AllowClobber - } - Catch { - Add-LogEntry "Module failed to install automatically! Manaully install the module, then re-run the script." -As Error - Exit 1 - } - } - Import-Module $Module - } -} #endregion Prep #region Execution diff --git a/PSUtilities/ScriptTools.ps1 b/PSUtilities/ScriptTools.ps1 index b3d44a5..528a56a 100644 --- a/PSUtilities/ScriptTools.ps1 +++ b/PSUtilities/ScriptTools.ps1 @@ -218,6 +218,61 @@ Function Initialize-PSGallery { } } +Import-Modules { + <# + .SYNOPSIS + A brief description of the function or script. + + .DESCRIPTION + A longer description. + + .PARAMETER FirstParameter + Description of each of the parameters. + Note: + To make it easier to keep the comments synchronized with changes to the parameters, + the preferred location for parameter documentation comments is not here, + but within the param block, directly above each parameter. + + .PARAMETER SecondParameter + Description of each of the parameters. + + .INPUTS + Description of objects that can be piped to the script. + + .OUTPUTS + Description of objects that are output by the script. + + .EXAMPLE + Example of how to run the script. + + .LINK + Links to further documentation. + + .NOTES + Detail on what the script does, if this is needed. + + #> + Param ( + [Parameter(Mandatory=$true)] $ModuleList + ) + If ($ModuleList) { + Initialize-PSGallery + ForEach ($Module in $ModuleList) { + If (-Not (Get-Module -ListAvailable -Name $Module)) { + Try { + Add-LogEntry "Module `"$Module`" not found, installing" + Install-Module -Name $Module -Scope CurrentUser -Force -AllowClobber + } + Catch { + Add-LogEntry "Module failed to install automatically! Manaully install the module, then re-run the script." -As Error + Exit 1 + } + } + Import-Module $Module + } +} +} + Function Import-JSONConfig () { <# .SYNOPSIS