This is just a little helper function that will help you if you have a prerequisite of certain 3rd-party functions being loaded for your script to work. It will either give you are TRUE/FALSE, or a list of the missing functions. Stick it in somewhere at the beginning of your script, and give it an array of functions to test for, and handle the result however you like.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Test-Function { | |
<# | |
.SYNOPSIS | |
This function will check for the existence of loaded functions. | |
.DESCRIPTION | |
This function will look at the currently loaded functions and report a true or false | |
based on whether the functions are loaded or not, or list any missing functions if the | |
ShowMissing parameter is used. | |
.EXAMPLE | |
Test-Function 'My-Function' | |
Will return TRUE if 'My-Function' is found, or FALSE if it's not. | |
.EXAMPLE | |
Test-Function @('My-Function1','My-Function2') | |
Will return TRUE if both functions are found, or FALSE if one, or both, are missing. | |
.EXAMPLE | |
Test-Function @('My-Function1','My-Function2') -ShowMissing | |
Will return a list of any missing functions. | |
.NOTES | |
Author: Øyvind Kallstad | |
Date: 18.04.2014 | |
Version: 1.0 | |
#> | |
[CmdletBinding()] | |
param ( | |
# Name of function(s) to check. | |
[Parameter(ValueFromPipeline, Position = 0)] | |
[string[]] $Name, | |
# If 'true', will list any missing functions. | |
[Parameter()] | |
[switch] $ShowMissing = $false | |
) | |
BEGIN { | |
$return = $true | |
$missing = @() | |
} | |
PROCESS { | |
foreach ($functionName in $Name) { | |
if (-not(Test-Path –Path "function:\\$($functionName)")) { | |
$return = $false | |
$missing += $functionName | |
} | |
} | |
} | |
END { | |
if ($ShowMissing) { | |
Write-Output $missing | |
} | |
else { | |
Write-Output $return | |
} | |
} | |
} |