diff --git a/PSUtilities/ScriptTemplate.ps1 b/PSUtilities/ScriptTemplate.ps1 index 1ffe756..8f4556c 100644 --- a/PSUtilities/ScriptTemplate.ps1 +++ b/PSUtilities/ScriptTemplate.ps1 @@ -1,21 +1,47 @@ <# -.SYNOPSIS - Provide a brief description of script purpose -.DESCRIPTION - Provide a more detailed description of script purpose -.EXAMPLE - Provide an example usage for the script -.NOTES - NAME: - AUTHOR: - LASTEDIT: - VERSION: + .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 + NAME: + AUTHOR: + LASTEDIT: + VERSION: #> #region Parameters param ( # Allows modules required by the script to be predefined, and loaded or installed as needed [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 [Parameter(DontShow = $true)] [ValidateSet('System', 'User', 'Custom')] [String] $ScriptType = "System", # 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 #region Imports -# Import Test-CommandExists, Add-LogEntry, Add-ListItem, Import-JSONConfig, Import-YAMLConfig, Set-RegistryKey, Test-AsAdmin, Format-Hyperlink -Invoke-WebRequest "https://gitea.taco.quest/Mindfang/ProjectTools/raw/branch/main/PSUtilities/ScriptTools.ps1" -OutFile "$Env:Temp\ScriptTools.ps1" -Import-Module -Name "$Env:Temp\ScriptTools.ps1" -Force -#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 = $PWD } +# Import script addons, provided by URI +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 + Write-Host "Importing addon: $(Split-Path -Path $Uri -Leaf)" + Import-Module -Name "$Env:Temp\$(Split-Path -Path $Uri -Leaf)" -Force + } + Catch { + Write-Host "Error encountered importing addon" + Exit $LASTEXITCODE + } } -} - -# 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 + Else { + Write-Host "Unable to import addon `"$(Split-Path -Path $Uri -Leaf)`" - verify URI is correct and that this device has an internet connection" + Exit $UriStatusCode } - Add-LogEntry (Start-Transcript "$AppDir\$ThisScript.log" -Append) } # Load any modules required by script @@ -132,22 +145,46 @@ If ($ModuleNames) { } Catch { Add-LogEntry "Module failed to install automatically! Manaully install the module, then re-run the script." -As Error - Exit 1 + Exit } } 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 #region Execution Try { - If (-Not $Uninstall) { - #TODO: "Install actions" for the script go here - } - Else { - #TODO: "Uninstall actions" for the script go here - } + #TODO: Add script actions here } Catch { # Write error to logs, if an exception is caught @@ -155,12 +192,13 @@ Catch { Write-Host $_ Write-Host $_.InvocationInfo Write-Host $_.ScriptStackTrace - Exit 1 + Exit $LASTEXITCODE } Finally { # Stop transcript, even if an error has occurred If (-Not $NoLog) { Stop-Transcript } + #TODO: Perform any additional disconnects, if needed } #endregion Execution \ No newline at end of file