diff --git a/HelperFunctions/LogTools.ps1 b/HelperFunctions/LogTools.ps1 deleted file mode 100644 index 6a68853..0000000 --- a/HelperFunctions/LogTools.ps1 +++ /dev/null @@ -1,30 +0,0 @@ -Function Add-LogEntry { - 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 - } -} \ No newline at end of file diff --git a/PSScriptTools/ScriptTools.ps1 b/PSScriptTools/ScriptTools.ps1 new file mode 100644 index 0000000..2657c92 --- /dev/null +++ b/PSScriptTools/ScriptTools.ps1 @@ -0,0 +1,179 @@ +#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 + Links to further documentation. + + .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 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