Skip to content

Commit

Permalink
ADGroup: Add integration tests (dsccommunity#495)
Browse files Browse the repository at this point in the history
- Changes to ADGroup
  - Now Get-TargetResource returns correct values when the group does not
    exist.
  - Added integration tests (issue dsccommunity#350).
  • Loading branch information
johlju authored Sep 1, 2019
1 parent 583704e commit 2490788
Show file tree
Hide file tree
Showing 4 changed files with 1,303 additions and 40 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
- Changes to ADDomainController
- Add InstallDns parameter to enable promotion without installing local
DNS Server Service ([issue #87](https://github.com/PowerShell/xActiveDirectory/issues/87)).
- Changes to ADGroup
- Now Get-TargetResource returns correct value when the group does not
exist.
- Added integration tests ([issue #350](https://github.com/PowerShell/ActiveDirectoryDsc/issues/350)).

## 4.0.0.0

Expand Down
82 changes: 42 additions & 40 deletions DSCResources/MSFT_ADGroup/MSFT_ADGroup.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,27 @@ function Get-TargetResource

Assert-Module -ModuleName 'ActiveDirectory'

$adGroupParams = Get-ADCommonParameters @PSBoundParameters
$getTargetResourceReturnValue = @{
Ensure = 'Absent'
GroupName = $GroupName
GroupScope = $null
Category = $null
Path = $null
Description = $null
DisplayName = $null
Members = @()
MembersToInclude = $MembersToInclude
MembersToExclude = $MembersToExclude
MembershipAttribute = $MembershipAttribute
ManagedBy = $null
Notes = $null
}

$adGroupParameters = Get-ADCommonParameters @PSBoundParameters

try
{
$adGroup = Get-ADGroup @adGroupParams -Properties @(
$adGroup = Get-ADGroup @adGroupParameters -Properties @(
'Name',
'GroupScope',
'GroupCategory',
Expand All @@ -165,52 +181,29 @@ function Get-TargetResource

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

# Retrieve the current list of members, returning the specified membership attribute
[System.Array] $adGroupMembers = (Get-ADGroupMember @adGroupParams).$MembershipAttribute

$targetResource = @{
GroupName = $adGroup.Name
GroupScope = $adGroup.GroupScope
Category = $adGroup.GroupCategory
Path = Get-ADObjectParentDN -DN $adGroup.DistinguishedName
Description = $adGroup.Description
DisplayName = $adGroup.DisplayName
Members = $adGroupMembers
MembersToInclude = $MembersToInclude
MembersToExclude = $MembersToExclude
MembershipAttribute = $MembershipAttribute
ManagedBy = $adGroup.ManagedBy
Notes = $adGroup.Info
Ensure = 'Absent'
}

if ($adGroup)
{
$targetResource['Ensure'] = 'Present'
# Retrieve the current list of members, returning the specified membership attribute
[System.Array] $adGroupMembers = (Get-ADGroupMember @adGroupParameters).$MembershipAttribute

$getTargetResourceReturnValue['Ensure'] = 'Present'
$getTargetResourceReturnValue['GroupName'] = $adGroup.Name
$getTargetResourceReturnValue['GroupScope'] = $adGroup.GroupScope
$getTargetResourceReturnValue['Category'] = $adGroup.GroupCategory
$getTargetResourceReturnValue['Path'] = Get-ADObjectParentDN -DN $adGroup.DistinguishedName
$getTargetResourceReturnValue['Description'] = $adGroup.Description
$getTargetResourceReturnValue['DisplayName'] = $adGroup.DisplayName
$getTargetResourceReturnValue['Members'] = $adGroupMembers
$getTargetResourceReturnValue['ManagedBy'] = $adGroup.ManagedBy
$getTargetResourceReturnValue['Notes'] = $adGroup.Info
}
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
{
Write-Verbose -Message ($script:localizedData.GroupNotFound -f $GroupName)

$targetResource = @{
GroupName = $GroupName
GroupScope = $GroupScope
Category = $Category
Path = $Path
Description = $Description
DisplayName = $DisplayName
Members = @()
MembersToInclude = $MembersToInclude
MembersToExclude = $MembersToExclude
MembershipAttribute = $MembershipAttribute
ManagedBy = $ManagedBy
Notes = $Notes
Ensure = 'Absent'
}
}

return $targetResource
return $getTargetResourceReturnValue
} #end function Get-TargetResource

<#
Expand Down Expand Up @@ -624,39 +617,48 @@ function Set-TargetResource
if ($PSBoundParameters.ContainsKey('Category') -and $Category -ne $adGroup.GroupCategory)
{
Write-Verbose -Message ($script:localizedData.UpdatingGroupProperty -f 'Category', $Category)

$setADGroupParams['GroupCategory'] = $Category
}

if ($PSBoundParameters.ContainsKey('GroupScope') -and $GroupScope -ne $adGroup.GroupScope)
{
# Cannot change DomainLocal to Global or vice versa directly. Need to change them to a Universal group first!
Set-ADGroup -Identity $adGroup.DistinguishedName -GroupScope Universal

Write-Verbose -Message ($script:localizedData.UpdatingGroupProperty -f 'GroupScope', $GroupScope)

$setADGroupParams['GroupScope'] = $GroupScope
}

if ($Description -and ($Description -ne $adGroup.Description))
{
Write-Verbose -Message ($script:localizedData.UpdatingGroupProperty -f 'Description', $Description)

$setADGroupParams['Description'] = $Description
}

if ($DisplayName -and ($DisplayName -ne $adGroup.DisplayName))
{
Write-Verbose -Message ($script:localizedData.UpdatingGroupProperty -f 'DisplayName', $DisplayName)

$setADGroupParams['DisplayName'] = $DisplayName
}

if ($ManagedBy -and ($ManagedBy -ne $adGroup.ManagedBy))
{
Write-Verbose -Message ($script:localizedData.UpdatingGroupProperty -f 'ManagedBy', $ManagedBy)

$setADGroupParams['ManagedBy'] = $ManagedBy
}

if ($Notes -and ($Notes -ne $adGroup.Info))
{
Write-Verbose -Message ($script:localizedData.UpdatingGroupProperty -f 'Notes', $Notes)
$setADGroupParams['Replace'] = @{ Info = $Notes }

$setADGroupParams['Replace'] = @{
Info = $Notes
}
}

Write-Verbose -Message ($script:localizedData.UpdatingGroup -f $GroupName)
Expand Down
Loading

0 comments on commit 2490788

Please sign in to comment.