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

xADGroup: Fix Issue #166 #429

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
- Catch exception when the path property specifies a non-existing path
([issue #408](https://github.com/PowerShell/xActiveDirectory/issues/408)).
- Changes to xADUser
- Fixes exception when creating a user with an empty string property ([issue #407](https://github.com/PowerShell/xActiveDirectory/pull/407)).
- Fixes exception when updating `CommonName` and `Path` concurrently ([issue #402](https://github.com/PowerShell/xActiveDirectory/pull/402)).
- Fixes exception when creating a user with an empty string property
([issue #407](https://github.com/PowerShell/xActiveDirectory/issues/407)).
- Fixes exception when updating `CommonName` and `Path` concurrently
Expand All @@ -39,6 +41,8 @@
([issue #332](https://github.com/PowerShell/xActiveDirectory/pull/332)).
- Changes to xADRecycleBin
- Updated tests and remove unnecessary mocks of `Write-Error`.
- Changes to xADGroup
- Fixes wrong error message when attempting to add a non-existent object to a group ([issue #166](https://github.com/PowerShell/xActiveDirectory/issues/166)).

## 3.0.0.0

Expand Down
24 changes: 20 additions & 4 deletions DSCResources/MSFT_xADGroup/MSFT_xADGroup.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function Get-TargetResource

try
{
$adGroup = Get-ADGroup @adGroupParams -Property Name, GroupScope, GroupCategory, DistinguishedName, Description, DisplayName, ManagedBy, Info
$adGroup = Get-ADGroup @adGroupParams -Properties Name, GroupScope, GroupCategory, DistinguishedName, Description, DisplayName, ManagedBy, Info

Write-Verbose -Message ($script:localizedData.RetrievingGroupMembers -f $MembershipAttribute)

Expand Down Expand Up @@ -433,7 +433,7 @@ function Set-TargetResource
}
}

$adGroup = Get-ADGroup @adGroupParams -Property Name, GroupScope, GroupCategory, DistinguishedName, Description, DisplayName, ManagedBy, Info
$adGroup = Get-ADGroup @adGroupParams -Properties Name, GroupScope, GroupCategory, DistinguishedName, Description, DisplayName, ManagedBy, Info

if ($Ensure -eq 'Present')
{
Expand Down Expand Up @@ -520,7 +520,7 @@ function Set-TargetResource

Write-Verbose -Message ($script:localizedData.AddingGroupMembers -f $Members.Count, $GroupName)

Add-ADCommonGroupMember -Parameter $adGroupParams -Members $Members -MembersInMultipleDomains:$MembersInMultipleDomains
Add-ADCommonGroupMember -Parameters $adGroupParams -Members $Members -MembersInMultipleDomains:$MembersInMultipleDomains
}

if ($PSBoundParameters.ContainsKey('MembersToInclude') -and -not [system.string]::IsNullOrEmpty($MembersToInclude))
Expand All @@ -529,7 +529,7 @@ function Set-TargetResource

Write-Verbose -Message ($script:localizedData.AddingGroupMembers -f $MembersToInclude.Count, $GroupName)

Add-ADCommonGroupMember -Parameter $adGroupParams -Members $MembersToInclude -MembersInMultipleDomains:$MembersInMultipleDomains
Add-ADCommonGroupMember -Parameters $adGroupParams -Members $MembersToInclude -MembersInMultipleDomains:$MembersInMultipleDomains
}

if ($PSBoundParameters.ContainsKey('MembersToExclude') -and -not [system.string]::IsNullOrEmpty($MembersToExclude))
Expand All @@ -552,6 +552,22 @@ function Set-TargetResource
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
{
<#
Determine whether we're attempting to add a new group (continue on)
or attempting to add a nonexistent group member (throw). Issue #166

We retrieve the second group of the first match of the stack trace regex
to get the cmdlet that failed. If it's 'Add-ADGroupMember', we throw,
because the member object to be added is not found in the domain, and the
add member opertion will fail.
This is probably the result of a typo in the configuration.
#>
$errorTrace = $PSItem.ScriptStackTrace | Select-String -Pattern '(at )(.*)(,)'
if ($errorTrace.Matches[0].Groups[2].Value -eq 'Add-ADGroupMember')
{
New-ObjectNotFoundException -Message $PSItem
}

# The AD group doesn't exist
if ($Ensure -eq 'Present')
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ConvertFrom-StringData @'
GroupMembershipNotDesiredState = Group membership is NOT in the desired state.
AddingGroupMembers = Adding '{0}' member(s) to AD group '{1}'.
RemovingGroupMembers = Removing '{0}' member(s) from AD group '{1}'.
GroupMemberNotFound = Group member not found.
AddingGroup = Adding AD Group '{0}'
UpdatingGroup = Updating AD Group '{0}'
RemovingGroup = Removing AD Group '{0}'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ function Assert-Module
$ImportModule
)

if (-not (Get-Module -Name $ModuleName -ListAvailable))
if ((-not (Get-Module -Name $ModuleName -ListAvailable)) -and (-not (Get-Module -Name $ModuleName)))
{
$errorMessage = $script:localizedData.RoleNotFoundError -f $moduleName
New-ObjectNotFoundException -Message $errorMessage
Expand Down
Loading