PSCredentialStore/src/Helper/Test-Module.ps1
Marco Blessing 64af16cc08 Publish Pre-release (#1)
* adds basic module layout

* fix module manifest encoding

* fix callsign in appveyor helper

* adds challenge file related functions

* adds connection manager functions

* adds Test-ChallengeFile

* adds item related functions

* adds store related functions

* adds cSpell dictionary

* adds CredentialStore related Pester tests

* [WIP] test Pester file

* fix typo

* adds file dependencies

* [WIP] fix pester tests

* fix exception state

* [WIP] add file dependencies

* fix gitkeep filename

* set constant debug module version string

* adds Pester Tests for New-CredentialStoreItem

* adds basic readme file

* adds functions to export; adds meta data

* adds vscode debug config

* adds test for optional dependencies

* [WIP] Implements optional dependency test

* adds taskrunner definitions

* adds CBH

* add gitignore file

* adds basic Build tasks

* typo fixed

* adds build folder to ignore list

* adds Cisco and NetApp opt dependencies

* adds build task

* fix end of line dequence

* remove task.json error

* adds sources for optional modules

* enables Pester and posh-git

* prepare pre-release
2017-09-21 13:32:15 +02:00

105 lines
2.8 KiB
PowerShell

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
.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 {
}
}