Additional rework of template

This commit is contained in:
2025-03-01 17:29:35 -06:00
parent e7c447d265
commit aa5b347f4f

View File

@@ -1,10 +1,32 @@
<# <#
.SYNOPSIS .SYNOPSIS
Provide a brief description of script purpose A brief description of the function or script.
.DESCRIPTION .DESCRIPTION
Provide a more detailed description of script purpose 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
Provide an example usage for the script Example of how to run the script.
.LINK
Links to further documentation.
.NOTES .NOTES
NAME: NAME:
AUTHOR: AUTHOR:
@@ -16,6 +38,10 @@
param ( param (
# Allows modules required by the script to be predefined, and loaded or installed as needed # Allows modules required by the script to be predefined, and loaded or installed as needed
[Parameter(DontShow = $true)] [Array] $ModuleNames = @(), [Parameter(DontShow = $true)] [Array] $ModuleNames = @(),
# Allows import of addon scripts
[Parameter(Dontshow = $true)] [Array] $AddonUris = @(
"https://gitea.taco.quest/Mindfang/ProjectTools/raw/branch/main/PSUtilities/ScriptTools.ps1"
),
# Sets the script storage location # Sets the script storage location
[Parameter(DontShow = $true)] [ValidateSet('System', 'User', 'Custom')] [String] $ScriptType = "System", [Parameter(DontShow = $true)] [ValidateSet('System', 'User', 'Custom')] [String] $ScriptType = "System",
# Allows adding verbose logging and what-if command simulation to the script # Allows adding verbose logging and what-if command simulation to the script
@@ -57,37 +83,24 @@ If ($Env:PROCESSOR_ARCHITECTURE -ne "AMD64") {
#endregion Architecture Check #endregion Architecture Check
#region Imports #region Imports
# Import Test-CommandExists, Add-LogEntry, Add-ListItem, Import-JSONConfig, Import-YAMLConfig, Set-RegistryKey, Test-AsAdmin, Format-Hyperlink # Import script addons, provided by URI
Invoke-WebRequest "https://gitea.taco.quest/Mindfang/ProjectTools/raw/branch/main/PSUtilities/ScriptTools.ps1" -OutFile "$Env:Temp\ScriptTools.ps1" ForEach ($Uri in $AddonUris) {
Import-Module -Name "$Env:Temp\ScriptTools.ps1" -Force $UriStatusCode = (Invoke-WebRequest -Uri $Uri -UseBasicParsing -Method Head -SkipHttpErrorCheck).StatusCode
#endregion Imports If ($UriStatusCode -eq 200) {
Try {
#region Functions Invoke-WebRequest -Uri $Uri -OutFile "$Env:Temp\$(Split-Path -Path $Uri -Leaf)" -ErrorAction Stop
#endregion Functions Write-Host "Importing addon: $(Split-Path -Path $Uri -Leaf)"
Import-Module -Name "$Env:Temp\$(Split-Path -Path $Uri -Leaf)" -Force
#region Prep }
# Set the working directory for logs and additional files Catch {
$ThisScript = ([IO.FileInfo]$MyInvocation.MyCommand.Definition).BaseName Write-Host "Error encountered importing addon"
Switch ($ScriptType) { Exit $LASTEXITCODE
"System" { $AppDir = "$Env:AppData\Mindfang\$ThisScript" }
"User" { $AppDir = "$Env:ProgramData\Mindfang\$ThisScript" }
"Custom" {
If ($ScriptDir) { $AppDir = $ScriptDir }
Else { $AppDir = $PWD }
} }
} }
Else {
# Create specified appdir if it does not already exist Write-Host "Unable to import addon `"$(Split-Path -Path $Uri -Leaf)`" - verify URI is correct and that this device has an internet connection"
If (-Not (Test-Path $AppDir)) { Exit $UriStatusCode
New-Item $AppDir -ItemType Directory
} }
# Begin recording transcript file
If (-Not $NoLog) {
If (-not $LogDir) {
$LogDir = $AppDir
}
Add-LogEntry (Start-Transcript "$AppDir\$ThisScript.log" -Append)
} }
# Load any modules required by script # Load any modules required by script
@@ -132,22 +145,46 @@ If ($ModuleNames) {
} }
Catch { Catch {
Add-LogEntry "Module failed to install automatically! Manaully install the module, then re-run the script." -As Error Add-LogEntry "Module failed to install automatically! Manaully install the module, then re-run the script." -As Error
Exit 1 Exit
} }
} }
Import-Module $Module Import-Module $Module
} }
} }
#endregion Imports
#region Functions
#endregion Functions
#region Prep
# 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" }
"Custom" {
If ($ScriptDir) { $AppDir = $ScriptDir }
Else { $AppDir = ([IO.FileInfo]$MyInvocation.MyCommand.Definition).Directory }
}
}
# Create specified appdir if it does not already exist
If (-Not (Test-Path $AppDir)) {
New-Item $AppDir -ItemType Directory
}
# Begin recording transcript file
If (-Not $NoLog) {
If (-not $LogDir) {
$LogDir = $AppDir
}
Add-LogEntry (Start-Transcript "$AppDir\$ThisScript.log" -Append)
}
#endregion Prep #endregion Prep
#region Execution #region Execution
Try { Try {
If (-Not $Uninstall) { #TODO: Add script actions here
#TODO: "Install actions" for the script go here
}
Else {
#TODO: "Uninstall actions" for the script go here
}
} }
Catch { Catch {
# Write error to logs, if an exception is caught # Write error to logs, if an exception is caught
@@ -155,12 +192,13 @@ Catch {
Write-Host $_ Write-Host $_
Write-Host $_.InvocationInfo Write-Host $_.InvocationInfo
Write-Host $_.ScriptStackTrace Write-Host $_.ScriptStackTrace
Exit 1 Exit $LASTEXITCODE
} }
Finally { Finally {
# Stop transcript, even if an error has occurred # Stop transcript, even if an error has occurred
If (-Not $NoLog) { If (-Not $NoLog) {
Stop-Transcript Stop-Transcript
} }
#TODO: Perform any additional disconnects, if needed
} }
#endregion Execution #endregion Execution