Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xADOrganizationalUnit: Catch Exception When the Path Property specifies a Non-Existing Path #413

Merged
merged 5 commits into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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