From c014bc7ec5f82492c93d9ac5cdac23c19010057d Mon Sep 17 00:00:00 2001 From: Mindfang Date: Tue, 28 Jan 2025 12:14:03 -0600 Subject: [PATCH] Add additional function, require PS 6.2 requirement --- PSUtilities/ScriptTools.ps1 | 102 +++++++++++++++++++++++++----------- 1 file changed, 72 insertions(+), 30 deletions(-) diff --git a/PSUtilities/ScriptTools.ps1 b/PSUtilities/ScriptTools.ps1 index 79e825c..77b4f20 100644 --- a/PSUtilities/ScriptTools.ps1 +++ b/PSUtilities/ScriptTools.ps1 @@ -1,9 +1,7 @@ -#Requires -Version 6.2 - Function Test-CommandExists { <# .SYNOPSIS - A brief description of the function or script. + Returns true if a command is present in PowerShell .DESCRIPTION A longer description. @@ -93,29 +91,17 @@ Function Add-LogEntry { [Parameter(Mandatory = $false)] [ValidateSet('Notify', 'Warning', 'Error')]$As, [Parameter(Mandatory = $false)] [Switch]$NewLine ) - $Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" - Switch ($As) { - "Notify" { - $FGC = "Cyan" - } - "Warning" { - $FGC = "Yellow" - } - "Error" { - $FGC = "Red" - } - Default { - $FGC = "White" - } + "Notify" { $FGC = "Cyan" } + "Warning" { $FGC = "Yellow" } + "Error" { $FGC = "Red" } + Default { $FGC = "White" } } + If ($Newline) { $Output += "`n" } - If ($Newline) { - Write-Host "`n[$Timestamp] $Message" -ForegroundColor $FGC - } - Else { - Write-Host "[$Timestamp] $Message" -ForegroundColor $FGC - } + $Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" + $Output += "[$Timestamp] $Message" + Write-Host $Output -ForegroundColor $FGC } Function Add-ListItem () { @@ -154,9 +140,13 @@ Function Add-ListItem () { #> Param ( - [Parameter(Mandatory = $true)] [ValidateNotNullorEmpty()]$Message + [Parameter(Mandatory = $true)] [ValidateNotNullorEmpty()]$Message, + [Parameter(Mandatory = $false)] [Int16] $Indent = 0 ) - Write-Host " * $Message" + + For ($Counter = 0; $Counter -le $Indent; $Counter++) { $Output += "`t" } + $Output += "* $Message" + Write-Host $Output } Function Start-Logging { @@ -210,11 +200,63 @@ Function Start-Logging { } If ($Append) { - Add-LogEntry (Start-Transcript -Path "$LogDir\$ScriptName.log" -UseMinimalHeader -Append) + Add-LogEntry (Start-Transcript -Path "$LogDir\$ScriptName.log" -Append) } Else { - Add-LogEntry (Start-Transcript -Path "$LogDir\$ScriptName.log" -UseMinimalHeader) + Add-LogEntry (Start-Transcript -Path "$LogDir\$ScriptName.log") + } +} + +Function Import-JSONConfig () { + <# + .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)] [ValidateNotNullOrEmpty()] $ConfigFile + ) + If (Test-Path -Path $ConfigFile) { + Try { + Return (Get-Content -Raw -Path $ConfigFile | ConvertFrom-Json) + } + Catch { + Add-LogEntry "Error loading config file! Please make sure the correct path was provided, and that it is a properly formatted JSON file" -As Error + Write-Host $_ + Exit 1 + } } - - -} \ No newline at end of file + Else { + Add-LogEntry "Error loading config file! Please make sure the correct path was provided, and that it is a properly formatted JSON file" -As Error + Write-Host $_ + Exit 1 + } +}