Skip to content

Commit

Permalink
Extended Assert-Module to import modules as well (#223)
Browse files Browse the repository at this point in the history
- Changes to xADCommon
  - Assert-Module has been extended with a parameter ImportModule to also
    import the module (issue #218).
- Changes to xADDomain
  - xADDomain makes use of new parameter ImportModule of Assert-Module
    in order to import the ADDSDeployment module (issue #218).
  • Loading branch information
nyanhp authored and johlju committed Aug 10, 2018
1 parent 9f9f199 commit 93444d1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
15 changes: 13 additions & 2 deletions DSCResources/MSFT_xADCommon/MSFT_xADCommon.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ function Assert-Module
[CmdletBinding()]
param
(
[Parameter()] [ValidateNotNullOrEmpty()]
[System.String] $ModuleName = 'ActiveDirectory'
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String]
$ModuleName = 'ActiveDirectory',

[Parameter()]
[switch]
$ImportModule
)

if (-not (Get-Module -Name $ModuleName -ListAvailable))
Expand All @@ -37,6 +43,11 @@ function Assert-Module
$errorMessage = $localizedString.RoleNotFoundError -f $moduleName;
ThrowInvalidOperationError -ErrorId $errorId -ErrorMessage $errorMessage;
}

if ($ImportModule)
{
Import-Module -Name $ModuleName
}
} #end function Assert-Module

# Internal function to test whether computer is a member of a domain
Expand Down
2 changes: 1 addition & 1 deletion DSCResources/MSFT_xADDomain/MSFT_xADDomain.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function Get-TargetResource
[String] $DomainMode
)

Assert-Module -ModuleName 'ADDSDeployment';
Assert-Module -ModuleName 'ADDSDeployment' -ImportModule
$domainFQDN = Resolve-DomainFQDN -DomainName $DomainName -ParentDomainName $ParentDomainName;
$isDomainMember = Test-DomainMember;

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@ The xADServicePrincipalName DSC resource will manage service principal names.

### Unreleased

* Changes to xADCommon
* Assert-Module has been extended with a parameter ImportModule to also import the module ([issue #218](https://github.com/PowerShell/xActiveDirectory/issues/218)). [Jan-Hendrik Peters (@nyanhp)](https://github.com/nyanhp)
* Changes to xADDomain
* xADDomain makes use of new parameter ImportModule of Assert-Module in order to import the ADDSDeployment module ([issue #218](https://github.com/PowerShell/xActiveDirectory/issues/218)). [Jan-Hendrik Peters (@nyanhp)](https://github.com/nyanhp)

### 2.20.0.0

* Changes to xActiveDirectory
Expand Down
10 changes: 10 additions & 0 deletions Tests/Unit/MSFT_xADCommon.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ try
{ Assert-Module -ModuleName $testModuleName } | Should Not Throw;
}

It 'Should call Import-Module when the module is installed and ImportModule is specified' {
$testModuleName = 'TestModule'
Mock -CommandName Get-Module -ParameterFilter { $Name -eq $testModuleName } -MockWith { return $true; }
Mock -CommandName Import-Module -ParameterFilter { $Name -eq $testModuleName }

Assert-Module -ModuleName $testModuleName -ImportModule

Assert-MockCalled -CommandName Import-Module
}

It 'Throws when module is not installed' {
$testModuleName = 'TestModule';
Mock -CommandName Get-Module -ParameterFilter { $Name -eq $testModuleName }
Expand Down

0 comments on commit 93444d1

Please sign in to comment.