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

ADGroup: Add integration tests #495

Merged
merged 2 commits into from
Sep 1, 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
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