Update tools and template

This commit is contained in:
2025-05-14 16:40:31 -05:00
parent 262c8fb095
commit 3a427ec324
2 changed files with 177 additions and 67 deletions

View File

@@ -22,10 +22,10 @@
Description of objects that are output by the script.
.EXAMPLE
Example of how to run the script.
.\Script-Name.ps1 -Output "Example of how to run the script"
.LINK
Links to further documentation.
https://example.com/docs
.NOTES
NAME:
@@ -46,8 +46,10 @@ param (
[Parameter(DontShow = $true)] [ValidateSet('System', 'User', 'Custom')] [String] $ScriptType = "System",
# Allows adding verbose logging and what-if command simulation to the script
[Parameter(DontShow = $true)] [Switch] $Dev,
# .PARAMETER ScriptDir
# Allows for the script logs to output to a nonstandard location. Overrides user/computer script paths
[Parameter(Mandatory = $false)] [String] $ScriptDir,
# .PARAMETER NoLog
# Flag to disable log file output
[Parameter(Mandatory = $false)] [Switch] $NoLog
#TODO: Add additional parameters, if appropriate
@@ -100,55 +102,6 @@ ForEach ($Uri in $AddonUris) {
Exit $UriStatusCode
}
}
# Load any modules required by script
If ($ModuleNames) {
If ([Net.ServicePointManager]::SecurityProtocol -ne [Net.SecurityProtocolType]::SystemDefault) {
Add-LogEntry "Upgrading TLS security protocol to 1.2"
Try { [Net.ServicePointManager]::SecurityProtocol = @([Net.SecurityProtocolType]::Tls, [Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls12) }
Catch {
Add-LogEntry "TLS upgrade failed, script is exiting" -As Error
Exit
}
}
If ((Get-PackageProvider).Name -notcontains "NuGet") {
Add-LogEntry "Installing NuGet package provider"
Try { Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -ErrorAction Stop | Out-Null }
Catch {
Add-LogEntry "Installation failed, script is exiting" -As Error
Exit
}
}
If ((Get-PSRepository).Name -notcontains "PSGallery") {
Add-LogEntry "Registering PSGallery as script source"
Try { Register-PSRepository -Default -InstallationPolicy Trusted -ErrorAction Stop }
Catch {
Add-LogEntry "Unable to register PSGallery, script is exiting" -As Error
Exit
}
}
ElseIf (Get-PSRepository | Where-Object { $_.Name -eq "PSGallery" -and $_.InstallationPolicy -ne "Trusted" }) {
Add-LogEntry "Trusting packages from PSGallery"
Try { Set-PSRepository PSGallery -InstallationPolicy Trusted -ErrorAction Stop }
Catch {
Add-LogEntry "Unable to set PSGallery as trusted, script is exiting" -As Error
Exit
}
}
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
}
}
Import-Module $Module
}
}
#endregion Imports
#region Functions
@@ -158,8 +111,8 @@ If ($ModuleNames) {
# Set the working directory for logs and additional files
$ThisScript = ([IO.FileInfo]$MyInvocation.MyCommand.Definition).BaseName
Switch ($ScriptType) {
"System" { $AppDir = "$Env:AppData\Mindfang\$ThisScript" }
"User" { $AppDir = "$Env:ProgramData\Mindfang\$ThisScript" }
"System" { $AppDir = "$Env:ProgramData\Mindfang\$ThisScript" }
"User" { $AppDir = "$Env:AppData\Mindfang\$ThisScript" }
"Custom" {
If ($ScriptDir) { $AppDir = $ScriptDir }
Else { $AppDir = ([IO.FileInfo]$MyInvocation.MyCommand.Definition).Directory }
@@ -178,6 +131,24 @@ If (-Not $NoLog) {
}
Add-LogEntry (Start-Transcript "$AppDir\$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