From 0c0899394549fd3b8591d3ba88b08a6ad1d0b73a Mon Sep 17 00:00:00 2001 From: Mindfang Date: Mon, 25 Nov 2024 14:16:25 -0600 Subject: [PATCH] Move PSScriptTools --- PSUtilities/ScriptTools.ps1 | 220 ++++++++++++++++++++++++++++++++++++ 1 file changed, 220 insertions(+) create mode 100644 PSUtilities/ScriptTools.ps1 diff --git a/PSUtilities/ScriptTools.ps1 b/PSUtilities/ScriptTools.ps1 new file mode 100644 index 0000000..79e825c --- /dev/null +++ b/PSUtilities/ScriptTools.ps1 @@ -0,0 +1,220 @@ +#Requires -Version 6.2 + +Function Test-CommandExists { + <# + .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 + https://devblogs.microsoft.com/scripting/use-a-powershell-function-to-see-if-a-command-exists/ + + .NOTES + Detail on what the script does, if this is needed. + + #> + + Param ( + $Command + ) + $OldPreference = $ErrorActionPreference + $ErrorActionPreference = 'Stop' + Try { + If (Get-Command $Command) { + Return $true + } + } + Catch { + Return $false + } + Finally { + $ErrorActionPreference = $OldPreference + } +} + +Function Add-LogEntry { + <# + .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()]$Message, + [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" + } + } + + If ($Newline) { + Write-Host "`n[$Timestamp] $Message" -ForegroundColor $FGC + } + Else { + Write-Host "[$Timestamp] $Message" -ForegroundColor $FGC + } +} + +Function Add-ListItem () { + <# + .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()]$Message + ) + Write-Host " * $Message" +} + +Function Start-Logging { + <# + .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)][String]$ScriptName, + [String]$LogDir, + [Switch]$User, + [Switch]$Append + ) + If (Not $LogDir -eq $null) { + If ($User) { + $LogDir = "$Env:USERPROFILE\Mindfang\$ScriptName" + } + Else { + $LogDir = "$Env:ProgramData\Mindfang\$ScriptName" + } + } + + If ($Append) { + Add-LogEntry (Start-Transcript -Path "$LogDir\$ScriptName.log" -UseMinimalHeader -Append) + } + Else { + Add-LogEntry (Start-Transcript -Path "$LogDir\$ScriptName.log" -UseMinimalHeader) + } + + +} \ No newline at end of file