Skip to content

Commit

Permalink
xADOrganizationalUnit: Catch Exception When the Path Property specifi…
Browse files Browse the repository at this point in the history
…es a Non-Existing Path (#413)

- Changes to xADOrganizationalUnit
  - Catch exception when the path property specifies a non-existing path (issue #408)
  • Loading branch information
X-Guardian authored and johlju committed Jul 5, 2019
1 parent 07c8785 commit 46c6cf2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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}'.
Expand All @@ -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.
'@
15 changes: 15 additions & 0 deletions Tests/Unit/MSFT_xADOrganizationalUnit.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 46c6cf2

Please sign in to comment.