adds test for optional dependencies
This commit is contained in:
parent
d246ada19f
commit
b31c201a1c
26
src/Dependency.json
Normal file
26
src/Dependency.json
Normal file
@ -0,0 +1,26 @@
|
||||
{
|
||||
"Version": 0.1,
|
||||
"Mandatory": {},
|
||||
"Optional": [
|
||||
{
|
||||
"Name": "VMware",
|
||||
"Modules": [
|
||||
"VMware.VimAutomation.Core"
|
||||
]
|
||||
},
|
||||
{
|
||||
"Name": "CiscoUCS",
|
||||
"Modules": []
|
||||
},
|
||||
{
|
||||
"Name": "FTP",
|
||||
"Modules": [
|
||||
"WinSCP"
|
||||
]
|
||||
},
|
||||
{
|
||||
"Name": "NetAppFAS",
|
||||
"Modules": []
|
||||
}
|
||||
]
|
||||
}
|
37
src/Helper/Resolve-Dependency.ps1
Normal file
37
src/Helper/Resolve-Dependency.ps1
Normal file
@ -0,0 +1,37 @@
|
||||
function Resolve-Dependency {
|
||||
[OutputType([bool])]
|
||||
[CmdletBinding()]
|
||||
param (
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$Name
|
||||
)
|
||||
|
||||
begin {
|
||||
$ModuleRootDir = $MyInvocation.MyCommand.Module.ModuleBase
|
||||
$DepFilePath = Join-Path -Path $ModuleRootDir -ChildPath "Dependency.json"
|
||||
if (Test-Path -Path $DepFilePath) {
|
||||
$Dependency = Get-Content -Path $DepFilePath -Raw -Encoding UTF8 | ConvertFrom-Json
|
||||
}
|
||||
else {
|
||||
Write-Warning ("Could not find the dependency file: {0}" -f $DepFilePath)
|
||||
}
|
||||
$res = @()
|
||||
}
|
||||
|
||||
process {
|
||||
$SelectedDependency = $Dependency.Optional | Where-Object {$_.Name -match $Name}
|
||||
|
||||
foreach ($Module in $SelectedDependency.Modules) {
|
||||
$res += Test-Module -Name $Module
|
||||
}
|
||||
if ($res -contains $false) {
|
||||
return $false
|
||||
}
|
||||
else {
|
||||
return $true
|
||||
}
|
||||
}
|
||||
|
||||
end {
|
||||
}
|
||||
}
|
105
src/Helper/Test-Module.ps1
Normal file
105
src/Helper/Test-Module.ps1
Normal file
@ -0,0 +1,105 @@
|
||||
function Test-Module {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Internal helper to check optional dependencies.
|
||||
|
||||
.DESCRIPTION
|
||||
Test-Dependency checks if the given module or pssnapin is available on the system. It returns a bool value
|
||||
So it is possible to use this function in a if condition.
|
||||
|
||||
.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
|
||||
|
||||
.NOTES
|
||||
File Name : Get-RandomKey.ps1
|
||||
Author : Marco Blessing - marco.blessing@googlemail.com
|
||||
Requires :
|
||||
|
||||
.LINK
|
||||
https://github.com/OCram85/PSCredentialStore
|
||||
#>
|
||||
[OutputType([bool])]
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[string]$Name,
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[ValidateSet('Module', 'PSSnapin', 'Custom')]
|
||||
[string]$Type = 'Module',
|
||||
|
||||
[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
|
||||
)
|
||||
begin {
|
||||
|
||||
}
|
||||
|
||||
process {
|
||||
$Message = $MessagePattern -f $Type, $Name
|
||||
Write-Debug $Message
|
||||
switch ($Type) {
|
||||
'Module' {
|
||||
if (Get-Module -Name $Name -ListAvailable) {
|
||||
return $true
|
||||
}
|
||||
else {
|
||||
if ($StopIfFails) {
|
||||
Write-Error -Message $Message -ErrorAction Stop -Category NotInstalled
|
||||
}
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
'PSSnapin' {
|
||||
if (Get-PSSnapin -Name $Name -Registered) {
|
||||
return $true
|
||||
}
|
||||
else {
|
||||
if ($StopIfFails) {
|
||||
Write-Error -Message $Message -ErrorAction Stop -Category NotInstalled
|
||||
return $false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
'Custom' {
|
||||
Throw 'Custom tests are not implemented yet!'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
end {
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user