2017-09-21 13:32:15 +02:00
|
|
|
function Resolve-Dependency {
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Tests defined optional dependencies and returns the result as bool.
|
|
|
|
|
|
|
|
.DESCRIPTION
|
|
|
|
Use this function to test for optional modules. You can use it if you provide functions which needs special
|
|
|
|
modules but you don't want to make them required.
|
|
|
|
|
|
|
|
Place a file called Dependency.json in your module root dir. The default format is:
|
|
|
|
|
|
|
|
{
|
|
|
|
"Version": 0.1,
|
|
|
|
"Mandatory": {},
|
|
|
|
"Optional": [
|
|
|
|
{
|
|
|
|
"Name": "VMware",
|
|
|
|
"Modules": [
|
|
|
|
"VMware.VimAutomation.Core"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Name": "CiscoUCS",
|
|
|
|
"Modules": []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
.PARAMETER Name
|
|
|
|
Select the dependency item name you defined in the dependency.json.
|
|
|
|
.INPUTS
|
|
|
|
[None]
|
|
|
|
|
|
|
|
.OUTPUTS
|
|
|
|
[bool]
|
|
|
|
|
|
|
|
.EXAMPLE
|
|
|
|
If (-not (Resolve-Dependency -Name 'VMware')) {
|
|
|
|
Write-Error -Message ("Could not resolve the optional dependencies defined for {0}" -f 'VMware') -ErrorAction 'Stop'
|
|
|
|
}
|
|
|
|
|
|
|
|
.NOTES
|
2017-10-23 10:53:52 +02:00
|
|
|
```
|
2017-09-21 13:32:15 +02:00
|
|
|
File Name : ResolveDependency.ps1
|
|
|
|
Author : Marco Blessing - marco.blessing@googlemail.com
|
|
|
|
Requires :
|
2017-10-23 10:53:52 +02:00
|
|
|
```
|
2017-09-21 13:32:15 +02:00
|
|
|
|
|
|
|
.LINK
|
|
|
|
https://github.com/OCram85/PSCredentialStore
|
|
|
|
#>
|
|
|
|
[OutputType([bool])]
|
|
|
|
[CmdletBinding()]
|
|
|
|
param (
|
|
|
|
[Parameter(Mandatory = $true)]
|
2018-03-09 14:02:51 +01:00
|
|
|
[ValidateNotNullOrEmpty()]
|
2017-09-21 13:32:15 +02:00
|
|
|
[string]$Name
|
|
|
|
)
|
|
|
|
|
|
|
|
begin {
|
2018-03-09 14:02:51 +01:00
|
|
|
$ModuleRootDir = Get-ModuleBase
|
2017-09-21 13:32:15 +02:00
|
|
|
$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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
process {
|
|
|
|
$SelectedDependency = $Dependency.Optional | Where-Object {$_.Name -match $Name}
|
2019-03-15 12:49:43 +01:00
|
|
|
# return true if there is no dependency defined
|
|
|
|
if ($null -eq $SelectedDependency) {
|
|
|
|
return $true
|
|
|
|
}
|
|
|
|
|
2018-03-09 14:02:51 +01:00
|
|
|
$res = @()
|
2017-09-21 13:32:15 +02:00
|
|
|
foreach ($Module in $SelectedDependency.Modules) {
|
|
|
|
$res += Test-Module -Name $Module
|
|
|
|
}
|
2018-03-09 14:02:51 +01:00
|
|
|
# return false if there was not module at all
|
|
|
|
if (($res -contains $false) -or ($res.Count -eq 0)) {
|
2017-09-21 13:32:15 +02:00
|
|
|
return $false
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
end {
|
|
|
|
}
|
|
|
|
}
|