Move PSScriptTools
This commit is contained in:
220
PSUtilities/ScriptTools.ps1
Normal file
220
PSUtilities/ScriptTools.ps1
Normal file
@@ -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)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user