Update tools and template
This commit is contained in:
@@ -16,10 +16,12 @@
|
||||
param (
|
||||
# Allows modules required by the script to be predefined, and loaded or installed as needed
|
||||
[Parameter(DontShow = $true)] [Array] $ModuleNames = @(),
|
||||
# Sets the script storage directory into the user profile instead of in the ProgramData folder
|
||||
[Parameter(Mandatory = $false)] [Switch] $UserScript = $false,
|
||||
# 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
|
||||
[Parameter(DontShow = $true)] [Switch] $Dev,
|
||||
# Allows for the script logs to output to a nonstandard location. Overrides user/computer script paths
|
||||
[Parameter(Mandatory = $false)] [String] $LogDir,
|
||||
[Parameter(Mandatory = $false)] [String] $ScriptDir,
|
||||
# Flag to disable log file output
|
||||
[Parameter(Mandatory = $false)] [Switch] $NoLog,
|
||||
# Allows for uninstallation of script
|
||||
@@ -55,7 +57,7 @@ If ($Env:PROCESSOR_ARCHITECTURE -ne "AMD64") {
|
||||
#endregion Architecture Check
|
||||
|
||||
#region Imports
|
||||
# Import Test-CommandExists, Add-LogEntry, Add-ListItem, Start-Logging, Import-JSONConfig
|
||||
# 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
|
||||
@@ -66,11 +68,13 @@ Import-Module -Name "$Env:Temp\ScriptTools.ps1" -Force
|
||||
#region Prep
|
||||
# Set the working directory for logs and additional files
|
||||
$ThisScript = ([IO.FileInfo]$MyInvocation.MyCommand.Definition).BaseName
|
||||
If ($UserScript) {
|
||||
$AppDir = "$Env:AppData\Mindfang\$ThisScript"
|
||||
}
|
||||
Else {
|
||||
$AppDir = "$Env:ProgramData\Mindfang\$ThisScript"
|
||||
Switch ($ScriptType) {
|
||||
"System" { $AppDir = "$Env:AppData\Mindfang\$ThisScript" }
|
||||
"User" { $AppDir = "$Env:ProgramData\Mindfang\$ThisScript" }
|
||||
"Custom" {
|
||||
If ($ScriptDir) { $AppDir = $ScriptDir }
|
||||
Else { $AppDir = $PWD }
|
||||
}
|
||||
}
|
||||
|
||||
# Create specified appdir if it does not already exist
|
||||
@@ -79,7 +83,7 @@ If (-Not (Test-Path $AppDir)) {
|
||||
}
|
||||
|
||||
# Begin recording transcript file
|
||||
If (-not $NoLog) {
|
||||
If (-Not $NoLog) {
|
||||
If (-not $LogDir) {
|
||||
$LogDir = $AppDir
|
||||
}
|
||||
@@ -87,24 +91,58 @@ If (-not $NoLog) {
|
||||
}
|
||||
|
||||
# Load any modules required by script
|
||||
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
|
||||
}
|
||||
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 "Module failed to install automatically! Manaully install the module, then re-run the script." -As Error
|
||||
Exit 1
|
||||
Add-LogEntry "TLS upgrade failed, script is exiting" -As Error
|
||||
Exit
|
||||
}
|
||||
}
|
||||
Import-Module $Module
|
||||
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 }
|
||||
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 1
|
||||
}
|
||||
}
|
||||
Import-Module $Module
|
||||
}
|
||||
}
|
||||
#endregion Prep
|
||||
|
||||
#region Execution
|
||||
Try {
|
||||
If ( -not $Uninstall) {
|
||||
If (-Not $Uninstall) {
|
||||
#TODO: "Install actions" for the script go here
|
||||
}
|
||||
Else {
|
||||
@@ -113,14 +151,16 @@ Try {
|
||||
}
|
||||
Catch {
|
||||
# Write error to logs, if an exception is caught
|
||||
Write-Output "Script execution failed!"
|
||||
Write-Output $_
|
||||
Write-Output $_.InvocationInfo
|
||||
Write-Output $_.ScriptStackTrace
|
||||
Write-Host "Script execution failed!"
|
||||
Write-Host $_
|
||||
Write-Host $_.InvocationInfo
|
||||
Write-Host $_.ScriptStackTrace
|
||||
Exit 1
|
||||
}
|
||||
Finally {
|
||||
# Stop transcript, even if an error has occurred
|
||||
Stop-Transcript
|
||||
If (-Not $NoLog) {
|
||||
Stop-Transcript
|
||||
}
|
||||
}
|
||||
#endregion Execution
|
Reference in New Issue
Block a user