-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Assert-RequiredCommandParameter
and `Test-AccountRequirePasswor…
…d` (#94) - Added private function `Assert-RequiredCommandParameter` that throws an exception if a specified parameter is not assigned a value, and optionally throws only if a specific parameter is passed. - [Issue #92](#92) - Related to SqlServerDsc [Issue #1796](dsccommunity/SqlServerDsc#1796). - Added public function `Test-AccountRequirePassword` that returns true or false whether an account need a password to be passed - [Issue #93](#93) - Related to SqlServerDsc [Issue #1794](dsccommunity/SqlServerDsc#1794). - DscResource.Common - Updated Visual Studio Code project settings to configure testing for Pester 5. - `Assert-BoundParameter` - Now has a new parameter set that calls `Assert-RequiredCommandParameter` which will throw an exception if a specified parameter is not assigned a value, and optionally throws only if a specific parameter is passed. - Fixed unit tests for `Assert-ElevatedUser` and `Test-IsNumericType` so the public function is tested correctly using the exported function.
- Loading branch information
Showing
12 changed files
with
610 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<# | ||
.SYNOPSIS | ||
Assert that required parameters has been specified. | ||
.DESCRIPTION | ||
Assert that required parameters has been specified, and throws an exception if not. | ||
.PARAMETER BoundParameterList | ||
A hashtable containing the parameters to evaluate. Normally this is set to | ||
$PSBoundParameters. | ||
.PARAMETER RequiredParameter | ||
One or more parameter names that is required to have been specified. | ||
.PARAMETER IfParameterPresent | ||
One or more parameter names that if specified will trigger the evaluation. | ||
If neither of the parameter names has been specified the evaluation of required | ||
parameters are not made. | ||
.EXAMPLE | ||
Assert-RequiredCommandParameter -BoundParameter $PSBoundParameters -RequiredParameter @('PBStartPortRange', 'PBEndPortRange') | ||
Throws an exception if either of the two parameters are not specified. | ||
.EXAMPLE | ||
Assert-RequiredCommandParameter -BoundParameter $PSBoundParameters -RequiredParameter @('Property2', 'Property3') -IfParameterPresent @('Property1') | ||
Throws an exception if the parameter 'Property1' is specified and either of the required parameters are not. | ||
.OUTPUTS | ||
None. | ||
#> | ||
function Assert-RequiredCommandParameter | ||
{ | ||
[CmdletBinding()] | ||
param | ||
( | ||
[Parameter(Mandatory = $true)] | ||
[System.Collections.Hashtable] | ||
$BoundParameterList, | ||
|
||
[Parameter(Mandatory = $true)] | ||
[System.String[]] | ||
$RequiredParameter, | ||
|
||
[Parameter()] | ||
[System.String[]] | ||
$IfParameterPresent | ||
) | ||
|
||
$evaluateRequiredParameter = $true | ||
|
||
if ($PSBoundParameters.ContainsKey('IfParameterPresent')) | ||
{ | ||
$hasIfParameterPresent = $BoundParameterList.Keys.Where( { $_ -in $IfParameterPresent } ) | ||
|
||
if (-not $hasIfParameterPresent) | ||
{ | ||
$evaluateRequiredParameter = $false | ||
} | ||
} | ||
|
||
if ($evaluateRequiredParameter) | ||
{ | ||
foreach ($parameter in $RequiredParameter) | ||
{ | ||
if ($parameter -notin $BoundParameterList.Keys) | ||
{ | ||
$errorMessage = if ($PSBoundParameters.ContainsKey('IfParameterPresent')) | ||
{ | ||
$script:localizedData.RequiredCommandParameter_SpecificParametersMustAllBeSetWhenParameterExist -f ($RequiredParameter -join ''', '''), ($IfParameterPresent -join ''', ''') | ||
} | ||
else | ||
{ | ||
$script:localizedData.RequiredCommandParameter_SpecificParametersMustAllBeSet -f ($RequiredParameter -join ''', ''') | ||
} | ||
|
||
$PSCmdlet.ThrowTerminatingError( | ||
[System.Management.Automation.ErrorRecord]::new( | ||
$errorMessage, | ||
'ARCP0001', # cspell: disable-line | ||
[System.Management.Automation.ErrorCategory]::InvalidOperation, | ||
'Command parameters' | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.