263 lines
6.9 KiB
PowerShell
263 lines
6.9 KiB
PowerShell
Function Test-CommandExists {
|
|
<#
|
|
.SYNOPSIS
|
|
Returns true if a command is present in PowerShell
|
|
|
|
.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
|
|
)
|
|
Switch ($As) {
|
|
"Notify" { $FGC = "Cyan" }
|
|
"Warning" { $FGC = "Yellow" }
|
|
"Error" { $FGC = "Red" }
|
|
Default { $FGC = "White" }
|
|
}
|
|
If ($Newline) { $Output += "`n" }
|
|
|
|
$Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
|
$Output += "[$Timestamp] $Message"
|
|
Write-Host $Output -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,
|
|
[Parameter(Mandatory = $false)] [Int16] $Indent = 0
|
|
)
|
|
|
|
For ($Counter = 0; $Counter -le $Indent; $Counter++) { $Output += "`t" }
|
|
$Output += "* $Message"
|
|
Write-Host $Output
|
|
}
|
|
|
|
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" -Append)
|
|
}
|
|
Else {
|
|
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
|
|
}
|
|
}
|
|
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
|
|
}
|
|
}
|