From b31c201a1c1d2903f598b7f6a74b9e623f32d933 Mon Sep 17 00:00:00 2001 From: OCram85 Date: Wed, 20 Sep 2017 16:08:48 +0200 Subject: [PATCH] adds test for optional dependencies --- src/Dependency.json | 26 ++++++++ src/Helper/Resolve-Dependency.ps1 | 37 +++++++++++ src/Helper/Test-Module.ps1 | 105 ++++++++++++++++++++++++++++++ 3 files changed, 168 insertions(+) create mode 100644 src/Dependency.json create mode 100644 src/Helper/Resolve-Dependency.ps1 create mode 100644 src/Helper/Test-Module.ps1 diff --git a/src/Dependency.json b/src/Dependency.json new file mode 100644 index 0000000..6e1e2bd --- /dev/null +++ b/src/Dependency.json @@ -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": [] + } + ] +} diff --git a/src/Helper/Resolve-Dependency.ps1 b/src/Helper/Resolve-Dependency.ps1 new file mode 100644 index 0000000..c3bb794 --- /dev/null +++ b/src/Helper/Resolve-Dependency.ps1 @@ -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 { + } +} diff --git a/src/Helper/Test-Module.ps1 b/src/Helper/Test-Module.ps1 new file mode 100644 index 0000000..174dd32 --- /dev/null +++ b/src/Helper/Test-Module.ps1 @@ -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 { + + } +}