2017-09-21 13:32:15 +02:00
|
|
|
function Test-Module {
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Tests if the given module exists on the local system.
|
|
|
|
|
|
|
|
.DESCRIPTION
|
|
|
|
Tests if the given module is installed on the local system. It returns a bool value as result.
|
|
|
|
|
|
|
|
.PARAMETER Name
|
|
|
|
Define a item name you need to test
|
|
|
|
|
|
|
|
.PARAMETER Type
|
|
|
|
Define the dependency type. This could be a Module or PSnapin.
|
|
|
|
|
|
|
|
.PARAMETER MessagePattern
|
|
|
|
You an optionally adjust the message pattern for the error message itself.
|
|
|
|
The available placeholders are:
|
|
|
|
- {0} : Type
|
|
|
|
- {1} : Name
|
|
|
|
|
|
|
|
.PARAMETER StopIfFails
|
|
|
|
This switch forces the entire script to stop if the given dependency object fails.
|
|
|
|
|
|
|
|
.INPUTS
|
|
|
|
[None]
|
|
|
|
|
|
|
|
.OUTPUTS
|
|
|
|
[Bool]
|
|
|
|
|
|
|
|
.EXAMPLE
|
|
|
|
.\Test-Dependency -Name 'VMware.PowerCLI' -Type 'Module'
|
|
|
|
|
|
|
|
.EXAMPLE
|
|
|
|
.\Test-Dependency -Name 'VMware.PowerCLI' -Type 'Module' -StopIfFails
|
|
|
|
#>
|
2022-06-28 08:56:33 +02:00
|
|
|
|
2017-09-21 13:32:15 +02:00
|
|
|
[OutputType([bool])]
|
|
|
|
[CmdletBinding()]
|
2022-06-28 08:56:33 +02:00
|
|
|
param (
|
2017-09-21 13:32:15 +02:00
|
|
|
[Parameter(Mandatory = $true)]
|
|
|
|
[ValidateNotNullOrEmpty()]
|
|
|
|
[string]$Name,
|
|
|
|
|
|
|
|
[Parameter(Mandatory = $false)]
|
|
|
|
[ValidateNotNullOrEmpty()]
|
|
|
|
[string]$MessagePattern = @"
|
|
|
|
Could not find the required {0} called {1}. Please install the required {0} to run this function!
|
|
|
|
"@,
|
|
|
|
[Parameter(Mandatory = $false)]
|
|
|
|
[switch]$StopIfFails
|
|
|
|
)
|
2022-06-28 08:56:33 +02:00
|
|
|
|
|
|
|
begin {}
|
2017-09-21 13:32:15 +02:00
|
|
|
|
|
|
|
process {
|
|
|
|
$Message = $MessagePattern -f $Type, $Name
|
|
|
|
Write-Debug $Message
|
|
|
|
|
2019-01-16 12:55:29 +01:00
|
|
|
if (Get-Module -Name $Name -ListAvailable) {
|
|
|
|
return $true
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ($StopIfFails) {
|
|
|
|
Write-Error -Message $Message -ErrorAction Stop -Category NotInstalled
|
2017-09-21 13:32:15 +02:00
|
|
|
}
|
2019-01-16 12:55:29 +01:00
|
|
|
return $false
|
2017-09-21 13:32:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
end {}
|
2017-09-21 13:32:15 +02:00
|
|
|
}
|