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

DSC_xGroupResource: Fix for the issue #750 #754

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- xPSDesiredStateConfiguration
- Updated CI pipeline to remove Azure DevOps deprecated Windows Server 2016
image and add Windows Server 2022 - Fixes [Issue #752](https://github.com/dsccommunity/xPSDesiredStateConfiguration/issues/752).
- xGroup
- Workaround added for the [Issue #750](https://github.com/dsccommunity/xPSDesiredStateConfiguration/issues/750) The resource could fail with the 'Exception calling "Add" with "1" argument(s): "The network path was not found."' The workaround is to catch the exception and try to use another programmatic method to update group members.

### Fixed

Expand Down
18 changes: 17 additions & 1 deletion source/DSCResources/DSC_xGroupResource/DSC_xGroupResource.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -2508,6 +2508,11 @@

.PARAMETER MemberAsPrincipal
The member to add to the group as a principal.
.NOTES
There is an issue reported at https://github.com/PowerShell/PSDscResources/issues/82
If local group already has members from trusted forests/domains, the Add method fails
The exact reason of the failure is unknown at the moment and as a workaround try-catch
block is used to fallback to ADSI WinNT provider.
#>
function Add-GroupMember
{
Expand All @@ -2525,7 +2530,18 @@
$MemberAsPrincipal
)

$Group.Members.Add($MemberAsPrincipal)
try
{
$Group.Members.Add($MemberAsPrincipal)

Check warning on line 2535 in source/DSCResources/DSC_xGroupResource/DSC_xGroupResource.psm1

View check run for this annotation

Codecov / codecov/patch

source/DSCResources/DSC_xGroupResource/DSC_xGroupResource.psm1#L2535

Added line #L2535 was not covered by tests
}
catch
{
Write-Verbose -Message $script:localizedData.PrincipalCollectionAddMethodException
Write-Verbose -Message $_.ToString()
Write-Verbose -Message $script:localizedData.WinNTProviderFallback
[ADSI] $adsiGroup = ("WinNT://$env:COMPUTERNAME/$($Group.Name),group")
$adsiGroup.Add("WinNT://$($MemberAsPrincipal.Sid)")

Check warning on line 2543 in source/DSCResources/DSC_xGroupResource/DSC_xGroupResource.psm1

View check run for this annotation

Codecov / codecov/patch

source/DSCResources/DSC_xGroupResource/DSC_xGroupResource.psm1#L2539-L2543

Added lines #L2539 - L2543 were not covered by tests
}
}

<#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ ConvertFrom-StringData @'
SetTargetResourceEndMessage = End executing Set functionality on the group {0}.
MembersToIncludeEmpty = MembersToInclude is empty. No group member additions are needed.
MembersToExcludeEmpty = MembersToExclude is empty. No group member removals are needed.
PrincipalCollectionAddMethodException = The Add method of the System.DirectoryServices.AccountManagement.PrincipalCollection class has thrown an exception.
WinNTProviderFallback = Falling back to ADSI WinNT provider.
'@