diff --git a/CHANGELOG.md b/CHANGELOG.md index 92c621204..9730326bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -113,8 +113,9 @@ - Remove unneeded example and resource designer files. - Added missing property schema descriptions ([issue #368](https://github.com/PowerShell/xActiveDirectory/issues/368)). - Code cleanup. - - It now set back the `$ErrorActionPreference` that was set prior to + - It now sets back the `$ErrorActionPreference` that was set prior to setting it to `'Stop'`. + - Replace Write-Error with the correct helper function ([issue #327](https://github.com/PowerShell/xActiveDirectory/issues/327)). - Changes to xADReplicationSiteLink - Fix ADIdentityNotFoundException when creating a new site link. - Code cleanup. diff --git a/DSCResources/MSFT_xADRecycleBin/MSFT_xADRecycleBin.psm1 b/DSCResources/MSFT_xADRecycleBin/MSFT_xADRecycleBin.psm1 index f9f5fda45..c118e2d0a 100644 --- a/DSCResources/MSFT_xADRecycleBin/MSFT_xADRecycleBin.psm1 +++ b/DSCResources/MSFT_xADRecycleBin/MSFT_xADRecycleBin.psm1 @@ -46,18 +46,18 @@ function Get-TargetResource } catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException], [Microsoft.ActiveDirectory.Management.ADServerDownException] { - Write-Error -Message ($script:localizedData.ForestNotFound -f $ForestFQDN) - throw $_ + $errorMessage = $script:localizedData.ForestNotFound -f $ForestFQDN + New-ObjectNotFoundException -Message $errorMessage -ErrorRecord $_ } catch [System.Security.Authentication.AuthenticationException] { - Write-Error -Message $script:localizedData.CredentialError - throw $_ + $errorMessage = $script:localizedData.CredentialError + New-InvalidArgumentException -Message $errorMessage -ArgumentName 'EnterpriseAdministratorCredential' } catch { - Write-Error -Message ($script:localizedData.GetUnhandledException -f $ForestFQDN) - throw $_ + $errorMessage = $script:localizedData.GetUnhandledException -f $ForestFQDN + New-InvalidOperationException -Message $errorMessage -ErrorRecord $_ } finally { @@ -112,18 +112,18 @@ function Set-TargetResource } catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException], [Microsoft.ActiveDirectory.Management.ADServerDownException] { - Write-Error -Message ($script:localizedData.ForestNotFound -f $ForestFQDN) - throw $_ + $errorMessage = $script:localizedData.ForestNotFound -f $ForestFQDN + New-ObjectNotFoundException -Message $errorMessage -ErrorRecord $_ } catch [System.Security.Authentication.AuthenticationException] { - Write-Error -Message $script:localizedData.CredentialError - throw $_ + $errorMessage = $script:localizedData.CredentialError + New-InvalidArgumentException -Message $errorMessage -ArgumentName 'EnterpriseAdministratorCredential' } catch { - Write-Error -Message ($script:localizedData.SetUnhandledException -f $ForestFQDN) - throw $_ + $errorMessage = $script:localizedData.SetUnhandledException -f $ForestFQDN + New-InvalidOperationException -Message $errorMessage -ErrorRecord $_ } finally { @@ -171,18 +171,18 @@ function Test-TargetResource } catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException], [Microsoft.ActiveDirectory.Management.ADServerDownException] { - Write-Error -Message ($script:localizedData.ForestNotFound -f $ForestFQDN) - throw $_ + $errorMessage = $script:localizedData.ForestNotFound -f $ForestFQDN + New-ObjectNotFoundException -Message $errorMessage -ErrorRecord $_ } catch [System.Security.Authentication.AuthenticationException] { - Write-Error -Message $script:localizedData.CredentialError - throw $_ + $errorMessage = $script:localizedData.CredentialError + New-InvalidArgumentException -Message $errorMessage -ArgumentName 'EnterpriseAdministratorCredential' } catch { - Write-Error -Message ($script:localizedData.TestUnhandledException -f $ForestFQDN) - throw $_ + $errorMessage = $script:localizedData.TestUnhandledException -f $ForestFQDN + New-InvalidOperationException -Message $errorMessage -ErrorRecord $_ } finally { diff --git a/Tests/Unit/MSFT_xADRecycleBin.Tests.ps1 b/Tests/Unit/MSFT_xADRecycleBin.Tests.ps1 index c91a44536..0676ff77f 100644 --- a/Tests/Unit/MSFT_xADRecycleBin.Tests.ps1 +++ b/Tests/Unit/MSFT_xADRecycleBin.Tests.ps1 @@ -114,23 +114,35 @@ try Mock -CommandName Write-Error It 'Should throw ADIdentityNotFoundException' { - Mock -CommandName Get-ADObject -MockWith { throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException) } - { Get-TargetResource @targetResourceParameters } | Should -Throw Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException + Mock -CommandName Get-ADObject -MockWith { + throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException) + } + $expectedError = $script:localizedData.ForestNotFound -f $ForestFQDN + { Get-TargetResource @targetResourceParameters } | Should -Throw $expectedError } It 'Should throw ADServerDownException' { - Mock -CommandName Get-ADObject -MockWith { throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADServerDownException) } - { Get-TargetResource @targetResourceParameters } | Should -Throw Microsoft.ActiveDirectory.Management.ADServerDownException + Mock -CommandName Get-ADObject -MockWith { + throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADServerDownException) + } + $expectedError = $script:localizedData.ForestNotFound -f $ForestFQDN + { Get-TargetResource @targetResourceParameters } | Should -Throw $expectedError } It 'Should throw AuthenticationException' { - Mock -CommandName Get-ADObject -MockWith { throw (New-Object -TypeName System.Security.Authentication.AuthenticationException) } - { Get-TargetResource @targetResourceParameters } | Should -Throw 'System error' + Mock -CommandName Get-ADObject -MockWith { + throw (New-Object -TypeName System.Security.Authentication.AuthenticationException) + } + $expectedError = $script:localizedData.CredentialError + { Get-TargetResource @targetResourceParameters } | Should -Throw $expectedError } It 'Should throw UnhandledException' { - Mock -CommandName Get-ADObject -MockWith { throw Unhandled.Exception } - { Get-TargetResource @targetResourceParameters } | Should -Throw Unhandled.Exception + Mock -CommandName Get-ADObject -MockWith { + throw Unhandled.Exception + } + $expectedError = $script:localizedData.GetUnhandledException -f $ForestFQDN + { Get-TargetResource @targetResourceParameters } | Should -Throw $expectedError } } } @@ -158,23 +170,35 @@ try Mock -CommandName Write-Error It 'Should throw ADIdentityNotFoundException' { - Mock -CommandName Get-ADObject -MockWith { throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException) } - { Test-TargetResource @targetResourceParameters } | Should -Throw Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException + Mock -CommandName Get-ADObject -MockWith { + throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException) + } + $expectedError = $script:localizedData.ForestNotFound -f $ForestFQDN + { Test-TargetResource @targetResourceParameters } | Should -Throw $expectedError } It 'Should throw ADServerDownException' { - Mock -CommandName Get-ADObject -MockWith { throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADServerDownException) } - { Test-TargetResource @targetResourceParameters } | Should -Throw Microsoft.ActiveDirectory.Management.ADServerDownException + Mock -CommandName Get-ADObject -MockWith { + throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADServerDownException) + } + $expectedError = $script:localizedData.ForestNotFound -f $ForestFQDN + { Test-TargetResource @targetResourceParameters } | Should -Throw $expectedError } It 'Should throw AuthenticationException' { - Mock -CommandName Get-ADObject -MockWith { throw (New-Object -TypeName System.Security.Authentication.AuthenticationException) } - { Test-TargetResource @targetResourceParameters } | Should -Throw 'System error' + Mock -CommandName Get-ADObject -MockWith { + throw (New-Object -TypeName System.Security.Authentication.AuthenticationException) + } + $expectedError = $script:localizedData.CredentialError + { Test-TargetResource @targetResourceParameters } | Should -Throw $expectedError } It 'Should throw UnhandledException' { - Mock -CommandName Get-ADObject -MockWith { throw Unhandled.Exception } - { Test-TargetResource @targetResourceParameters } | Should -Throw Unhandled.Exception + Mock -CommandName Get-ADObject -MockWith { + throw Unhandled.Exception + } + $expectedError = $script:localizedData.TestUnhandledException -f $ForestFQDN + { Test-TargetResource @targetResourceParameters } | Should -Throw $expectedError } } } @@ -210,23 +234,35 @@ try Mock -CommandName Write-Error It 'Should throw ADIdentityNotFoundException' { - Mock -CommandName Get-ADForest -MockWith { throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException) } - { Set-TargetResource @targetResourceParameters } | Should -Throw Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException + Mock -CommandName Get-ADForest -MockWith { + throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException) + } + $expectedError = $script:localizedData.ForestNotFound -f $ForestFQDN + { Set-TargetResource @targetResourceParameters } | Should -Throw $expectedError } It 'Should throw ADServerDownException' { - Mock -CommandName Get-ADForest -MockWith { throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADServerDownException) } - { Set-TargetResource @targetResourceParameters } | Should -Throw Microsoft.ActiveDirectory.Management.ADServerDownException + Mock -CommandName Get-ADForest -MockWith { + throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADServerDownException) + } + $expectedError = $script:localizedData.ForestNotFound -f $ForestFQDN + { Set-TargetResource @targetResourceParameters } | Should -Throw $expectedError } It 'Should throw AuthenticationException' { - Mock -CommandName Get-ADForest -MockWith { throw (New-Object -TypeName System.Security.Authentication.AuthenticationException) } - { Set-TargetResource @targetResourceParameters } | Should -Throw 'System error' + Mock -CommandName Get-ADForest -MockWith { + throw (New-Object -TypeName System.Security.Authentication.AuthenticationException) + } + $expectedError = $script:localizedData.CredentialError + { Set-TargetResource @targetResourceParameters } | Should -Throw $expectedError } It 'Should throw UnhandledException' { - Mock -CommandName Get-ADForest -MockWith { throw Unhandled.Exception } - { Set-TargetResource @targetResourceParameters } | Should -Throw Unhandled.Exception + Mock -CommandName Get-ADForest -MockWith { + throw Unhandled.Exception + } + $expectedError = $script:localizedData.SetUnhandledException -f $ForestFQDN + { Set-TargetResource @targetResourceParameters } | Should -Throw $expectedError } } }