diff --git a/CHANGELOG.md b/CHANGELOG.md index 091e3b434..680834810 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ - Added a requirement to README stating "Group Managed Service Accounts need at least one Windows Server 2012 Domain Controller" ([issue #399](https://github.com/PowerShell/xActiveDirectory/pull/399)). - Changes to xADComputer - Fixed the GUID in Example 3-AddComputerAccountSpecificPath_Config. ([issue #410](https://github.com/PowerShell/xActiveDirectory/pull/410)) +- Changes to xADOrganizationalUnit + - Catch exception when the path property specifies a non-existing path ([issue #408](https://github.com/PowerShell/xActiveDirectory/pull/408)) ## 3.0.0.0 diff --git a/DSCResources/MSFT_xADOrganizationalUnit/MSFT_xADOrganizationalUnit.psm1 b/DSCResources/MSFT_xADOrganizationalUnit/MSFT_xADOrganizationalUnit.psm1 index 8aa742605..a47df6a79 100644 --- a/DSCResources/MSFT_xADOrganizationalUnit/MSFT_xADOrganizationalUnit.psm1 +++ b/DSCResources/MSFT_xADOrganizationalUnit/MSFT_xADOrganizationalUnit.psm1 @@ -23,9 +23,21 @@ function Get-TargetResource Assert-Module -ModuleName 'ActiveDirectory' - Write-Verbose ($script:localizedData.RetrievingOU -f $Name) + Write-Verbose ($script:localizedData.RetrievingOU -f $Name, $Path) - $ou = Get-ADOrganizationalUnit -Filter { Name -eq $Name } -SearchBase $Path -SearchScope OneLevel -Properties ProtectedFromAccidentalDeletion, Description + try + { + $ou = Get-ADOrganizationalUnit -Filter { Name -eq $Name } -SearchBase $Path -SearchScope OneLevel -Properties ProtectedFromAccidentalDeletion, Description + } + catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] + { + $errorMessage = $script:localizedData.PathNotFoundError -f $Path + New-ObjectNotFoundException -Message $errorMessage + } + catch + { + throw $_ + } if ($null -eq $ou) { diff --git a/DSCResources/MSFT_xADOrganizationalUnit/en-US/MSFT_xADOrganizationalUnit.strings.psd1 b/DSCResources/MSFT_xADOrganizationalUnit/en-US/MSFT_xADOrganizationalUnit.strings.psd1 index 0727960ec..8182ddf74 100644 --- a/DSCResources/MSFT_xADOrganizationalUnit/en-US/MSFT_xADOrganizationalUnit.strings.psd1 +++ b/DSCResources/MSFT_xADOrganizationalUnit/en-US/MSFT_xADOrganizationalUnit.strings.psd1 @@ -1,6 +1,6 @@ # culture="en-US" ConvertFrom-StringData @' - RetrievingOU = Retrieving OU '{0}'. + RetrievingOU = Retrieving OU '{0}' from path '{1}'. UpdatingOU = Updating OU '{0}'. DeletingOU = Deleting OU '{0}'. CreatingOU = Creating OU '{0}'. @@ -10,4 +10,5 @@ ConvertFrom-StringData @' OUExistsButShouldNot = OU '{0}' exists when it should not exist. OUDoesNotExistButShould = OU '{0}' does not exist when it should exist. OUDoesNotExistAndShouldNot = OU '{0}' does not exist and is in the desired state. + PathNotFoundError = The Path '{0}' was not found. '@ diff --git a/Tests/Unit/MSFT_xADOrganizationalUnit.Tests.ps1 b/Tests/Unit/MSFT_xADOrganizationalUnit.Tests.ps1 index 817cb1886..eac77cda5 100644 --- a/Tests/Unit/MSFT_xADOrganizationalUnit.Tests.ps1 +++ b/Tests/Unit/MSFT_xADOrganizationalUnit.Tests.ps1 @@ -154,6 +154,21 @@ try $targetResource.Description | Should -BeNullOrEmpty } + It 'Should throw the correct error if the path does not exist' { + Mock -CommandName Assert-Module + Mock -CommandName Get-ADOrganizationalUnit -MockWith { throw New-Object Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException } + + $errorMessage = $script:localizedData.PathNotFoundError -f $testPresentParams.Path + { Get-TargetResource -Name $testPresentParams.Name -Path $testPresentParams.Path } | Should -Throw $errorMessage + } + + It 'Should throw the correct error if an unkwon error occurs' { + $error = 'Unknown Error' + Mock -CommandName Assert-Module + Mock -CommandName Get-ADOrganizationalUnit -MockWith { throw $error } + + { Get-TargetResource -Name $testPresentParams.Name -Path $testPresentParams.Path } | Should -Throw $error + } } #endregion