Skip to content

Commit

Permalink
Fix named parameters on Where-Object
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju committed Jun 15, 2019
1 parent 65016dd commit e7e001a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 40 deletions.
4 changes: 1 addition & 3 deletions DSCResources/MSFT_xADKDSKey/MSFT_xADKDSKey.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,7 @@ function Get-ADRootDomainDN
{
[CmdletBinding()]
[OutputType([System.String])]
param
(
)
param ()

$rootDomainDN = (New-Object -TypeName System.DirectoryServices.DirectoryEntry('LDAP://RootDSE')).Get('rootDomainNamingContext')
Write-Verbose -Message ($script:localizedData.RetrievedRootDomainDN -f $rootDomainDN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,13 @@ function Test-TargetResource
$PSBoundParameters['MembershipAttribute'] = $MembershipAttribute

$compareTargetResourceNonCompliant = Compare-TargetResourceState @PSBoundParameters |
Where-Object { $_.Pass -eq $false }
Where-Object -FilterScript { $_.Pass -eq $false }

# Check if Absent, if so then we don't need to propagate any other parameters
if ($Ensure -eq 'Absent')
{
$ensureState = $compareTargetResourceNonCompliant |
Where-Object { $_.Parameter -eq 'Ensure' }
Where-Object -FilterScript { $_.Parameter -eq 'Ensure' }

if ($ensureState)
{
Expand Down Expand Up @@ -402,7 +402,7 @@ function Set-TargetResource

$compareTargetResource = Compare-TargetResourceState @PSBoundParameters
$compareTargetResourceNonCompliant = @($compareTargetResource |
Where-Object { $_.Pass -eq $false })
Where-Object -FilterScript { $_.Pass -eq $false })

$adServiceAccountParameters = Get-ADCommonParameters @PSBoundParameters
$setServiceAccountParameters = $adServiceAccountParameters.Clone()
Expand All @@ -414,7 +414,7 @@ function Set-TargetResource
{
$isEnsureNonCompliant = $false
if ($compareTargetResourceNonCompliant |
Where-Object { $_.Parameter -eq 'Ensure' })
Where-Object -FilterScript { $_.Parameter -eq 'Ensure' })
{
$isEnsureNonCompliant = $true
}
Expand All @@ -429,7 +429,7 @@ function Set-TargetResource
{
#region Check if AccountType is compliant
$accountTypeState = $compareTargetResourceNonCompliant |
Where-Object { $_.Parameter -eq 'AccountType' }
Where-Object -FilterScript { $_.Parameter -eq 'AccountType' }

# Account already exist, need to update parameters that are not in compliance
if ($accountTypeState)
Expand All @@ -450,13 +450,13 @@ function Set-TargetResource

# Remove AccountType since we don't want to enumerate down below
$compareTargetResourceNonCompliant = @($compareTargetResourceNonCompliant |
Where-Object { $_.Parameter -ne 'AccountType' })
Where-Object -FilterScript { $_.Parameter -ne 'AccountType' })
#endregion Check if AccountType is compliant

#region Check if Path is compliant
$isPathNonCompliant = $false
if ($compareTargetResourceNonCompliant |
Where-Object { $_.Parameter -eq 'Path' })
Where-Object -FilterScript { $_.Parameter -eq 'Path' })
{
$isPathNonCompliant = $true
}
Expand All @@ -465,15 +465,15 @@ function Set-TargetResource
{
Write-Verbose -Message ($script:localizedData.MovingManagedServiceAccount -f $ServiceAccountName, $Path)
$distinguishedNameObject = $compareTargetResource |
Where-Object { $_.Parameter -eq 'DistinguishedName' }
Where-Object -FilterScript { $_.Parameter -eq 'DistinguishedName' }

$moveADObjectParameters['Identity'] = $distinguishedNameObject.Actual

Move-ADObject @moveADObjectParameters -TargetPath $Path
}

$compareTargetResourceNonCompliant = @($compareTargetResourceNonCompliant |
Where-Object { $_.Parameter -ne 'Path' })
Where-Object -FilterScript { $_.Parameter -ne 'Path' })
#endregion Check if Path is compliant

#region Check if other parameters are compliant
Expand Down Expand Up @@ -511,7 +511,7 @@ function Set-TargetResource
{
$isEnsureNonCompliant = $false
if ($compareTargetResourceNonCompliant |
Where-Object { $_.Parameter -eq 'Ensure' })
Where-Object -FilterScript { $_.Parameter -eq 'Ensure' })
{
$isEnsureNonCompliant = $true
}
Expand Down
2 changes: 1 addition & 1 deletion DSCResources/MSFT_xADUser/MSFT_xADUser.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,7 @@ function Set-TargetResource
{
# Find the associated AD property
$adProperty = $adPropertyMap |
Where-Object { $_.Parameter -eq $parameter }
Where-Object -FilterScript { $_.Parameter -eq $parameter }

if ([System.String]::IsNullOrEmpty($adProperty))
{
Expand Down
12 changes: 6 additions & 6 deletions Tests/Unit/MSFT_xADKDSKey.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ try
$compareTargetResourceResult = Compare-TargetResourceState @compareTargetResourceParametersFuture
$testCases = @()
# Need to remove parameters that will always be true
$compareTargetResourceResult = $compareTargetResourceResult | Where-Object {
$compareTargetResourceResult = $compareTargetResourceResult | Where-Object -FilterScript {
$_.Parameter -ne 'EffectiveTime' -and
$_.Parameter -ne 'DistinguishedName'
}
Expand Down Expand Up @@ -549,7 +549,7 @@ try
Context -Name "When the system is in the desired state and 'Ensure' is 'Absent'" {
It "Should pass when 'Ensure' is set to 'Absent" {
$mockKDSRootKeyFutureCompareEnsureAbsent = Copy-ArrayObjects $mockKDSRootKeyFutureCompare
$objectEnsure = $mockKDSRootKeyFutureCompareEnsureAbsent | Where-Object {$_.Parameter -eq 'Ensure'}
$objectEnsure = $mockKDSRootKeyFutureCompareEnsureAbsent | Where-Object -FilterScript {$_.Parameter -eq 'Ensure'}
$objectEnsure.Actual = 'Absent'
$objectEnsure.Pass = $true

Expand Down Expand Up @@ -580,7 +580,7 @@ try
$testCases = @()
foreach($incorrectParameter in $testIncorrectParameters.GetEnumerator())
{
$objectParameter = $mockKDSRootKeyFutureCompareNotCompliant | Where-Object { $_.Parameter -eq $incorrectParameter.Name }
$objectParameter = $mockKDSRootKeyFutureCompareNotCompliant | Where-Object -FilterScript { $_.Parameter -eq $incorrectParameter.Name }
$objectParameter.Expected = $incorrectParameter.Value
$objectParameter.Pass = $false

Expand Down Expand Up @@ -648,7 +648,7 @@ try

Context -Name 'When the system is in the desired state and KDS Root Key is Absent' {
$mockKDSRootKeyFutureCompareEnsureAbsent = Copy-ArrayObjects $mockKDSRootKeyFutureCompare
$objectEnsure = $mockKDSRootKeyFutureCompareEnsureAbsent | Where-Object {$_.Parameter -eq 'Ensure'}
$objectEnsure = $mockKDSRootKeyFutureCompareEnsureAbsent | Where-Object -FilterScript {$_.Parameter -eq 'Ensure'}
$objectEnsure.Expected = 'Absent'
$objectEnsure.Pass = $false

Expand Down Expand Up @@ -676,7 +676,7 @@ try
Context -Name 'When the system is NOT in the desired state and need to remove KDS Root Key' {
BeforeEach {
$mockKDSRootKeyFutureCompareEnsureAbsent = Copy-ArrayObjects $mockKDSRootKeyFutureCompare
$objectEnsure = $mockKDSRootKeyFutureCompareEnsureAbsent | Where-Object {$_.Parameter -eq 'Ensure'}
$objectEnsure = $mockKDSRootKeyFutureCompareEnsureAbsent | Where-Object -FilterScript {$_.Parameter -eq 'Ensure'}
$objectEnsure.Actual = 'Present'
$objectEnsure.Pass = $false

Expand Down Expand Up @@ -768,7 +768,7 @@ try
Context -Name 'When the system is NOT in the desired state and need to add KDS Root Key' {
BeforeEach {
$mockKDSRootKeyCompareEnsureAbsent = Copy-ArrayObjects $mockKDSRootKeyFutureCompare
$objectEnsure = $mockKDSRootKeyCompareEnsureAbsent | Where-Object {$_.Parameter -eq 'Ensure'}
$objectEnsure = $mockKDSRootKeyCompareEnsureAbsent | Where-Object -FilterScript {$_.Parameter -eq 'Ensure'}
$objectEnsure.Actual = 'Absent'
$objectEnsure.Pass = $false

Expand Down
40 changes: 20 additions & 20 deletions Tests/Unit/MSFT_xADManagedServiceAccount.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ try
$getTargetResourceResult = Compare-TargetResourceState @testResourceParametersSingleNotCompliant
$testCases = @()
# Need to remove parameters that will always be true
$getTargetResourceResult = $getTargetResourceResult | Where-Object {
$getTargetResourceResult = $getTargetResourceResult | Where-Object -FilterScript {
$_.Parameter -ne 'ServiceAccountName' -and
$_.Parameter -ne 'DistinguishedName' -and
$_.Parameter -ne 'MembershipAttribute'
Expand Down Expand Up @@ -750,7 +750,7 @@ try
$getTargetResourceResult = Compare-TargetResourceState @testResourceParametersGroup
$testCases = @()
# Need to remove parameters that will always be true
$getTargetResourceResult = $getTargetResourceResult | Where-Object {
$getTargetResourceResult = $getTargetResourceResult | Where-Object -FilterScript {
$_.Parameter -ne 'ServiceAccountName' -and
$_.Parameter -ne 'DistinguishedName' -and
$_.Parameter -ne 'MembershipAttribute'
Expand Down Expand Up @@ -793,7 +793,7 @@ try

$getTargetResourceResultSAM = Compare-TargetResourceState @testResourceParametersGroupSAM

$membersState = $getTargetResourceResultSAM | Where-Object {$_.Parameter -eq 'Members'}
$membersState = $getTargetResourceResultSAM | Where-Object -FilterScript {$_.Parameter -eq 'Members'}
$membersState.Expected | Should -Not -BeExactly $membersState.Actual
$membersState.Pass | Should -BeFalse
}
Expand All @@ -808,7 +808,7 @@ try

$getTargetResourceResultDN = Compare-TargetResourceState @testResourceParametersGroupDN

$membersState = $getTargetResourceResultDN | Where-Object {$_.Parameter -eq 'Members'}
$membersState = $getTargetResourceResultDN | Where-Object -FilterScript {$_.Parameter -eq 'Members'}
$membersState.Expected | Should -Not -BeExactly $membersState.Actual
$membersState.Pass | Should -BeFalse
}
Expand All @@ -823,7 +823,7 @@ try

$getTargetResourceResultSID = Compare-TargetResourceState @testResourceParametersGroupSID

$membersState = $getTargetResourceResultSID | Where-Object {$_.Parameter -eq 'Members'}
$membersState = $getTargetResourceResultSID | Where-Object -FilterScript {$_.Parameter -eq 'Members'}
$membersState.Expected | Should -Not -BeExactly $membersState.Actual
$membersState.Pass | Should -BeFalse
}
Expand All @@ -838,7 +838,7 @@ try

$getTargetResourceResultGUID = Compare-TargetResourceState @testResourceParametersGroupGUID

$membersState = $getTargetResourceResultGUID | Where-Object {$_.Parameter -eq 'Members'}
$membersState = $getTargetResourceResultGUID | Where-Object -FilterScript {$_.Parameter -eq 'Members'}
$membersState.Expected | Should -Not -BeExactly $membersState.Actual
$membersState.Pass | Should -BeFalse
}
Expand Down Expand Up @@ -897,7 +897,7 @@ try
Context -Name "When the system is in the desired state and 'Ensure' is 'Absent' (Both)" {
It "Should pass when 'Ensure' is set to 'Absent" {
$mockCompareSingleServiceAccountEnsureAbsent = $mockCompareSingleServiceAccount.Clone()
$objectEnsure = $mockCompareSingleServiceAccountEnsureAbsent | Where-Object {$_.Parameter -eq 'Ensure'}
$objectEnsure = $mockCompareSingleServiceAccountEnsureAbsent | Where-Object -FilterScript {$_.Parameter -eq 'Ensure'}
$objectEnsure.Actual = 'Absent'
$objectEnsure.Pass = $true

Expand Down Expand Up @@ -931,7 +931,7 @@ try
$testCases = @()
foreach($incorrectParameter in $testIncorrectParameters.GetEnumerator())
{
$objectParameter = $mockCompareSingleServiceAccountNotCompliant | Where-Object { $_.Parameter -eq $incorrectParameter.Name }
$objectParameter = $mockCompareSingleServiceAccountNotCompliant | Where-Object -FilterScript { $_.Parameter -eq $incorrectParameter.Name }
$objectParameter.Expected = $incorrectParameter.Value
$objectParameter.Pass = $false

Expand Down Expand Up @@ -983,7 +983,7 @@ try
$testCases = @()
foreach($incorrectParameter in $testIncorrectParameters.GetEnumerator())
{
$objectParameter = $mockCompareGroupServiceAccountNotCompliant | Where-Object { $_.Parameter -eq $incorrectParameter.Name }
$objectParameter = $mockCompareGroupServiceAccountNotCompliant | Where-Object -FilterScript { $_.Parameter -eq $incorrectParameter.Name }
$objectParameter.Expected = $incorrectParameter.Value
$objectParameter.Pass = $false

Expand Down Expand Up @@ -1133,7 +1133,7 @@ try

Context -Name "When the system is in the desired state and 'Ensure' is 'Absent' (Both)" {
$mockCompareSingleServiceAccountEnsureAbsent = $mockCompareSingleServiceAccount.Clone()
$objectEnsure = $mockCompareSingleServiceAccountEnsureAbsent | Where-Object {$_.Parameter -eq 'Ensure'}
$objectEnsure = $mockCompareSingleServiceAccountEnsureAbsent | Where-Object -FilterScript {$_.Parameter -eq 'Ensure'}
$objectEnsure.Actual = 'Absent'
$objectEnsure.Pass = $true

Expand Down Expand Up @@ -1167,7 +1167,7 @@ try
$mockCompareSingleServiceAccountNotCompliantEnsure = Copy-ArrayObjects $mockCompareSingleServiceAccount

#region Incorrect Path setup
$objectPath = $mockCompareSingleServiceAccountNotCompliantPath | Where-Object {$_.Parameter -eq 'Path'}
$objectPath = $mockCompareSingleServiceAccountNotCompliantPath | Where-Object -FilterScript {$_.Parameter -eq 'Path'}
$objectPath.Expected = 'WrongPath'
$objectPath.Pass = $false

Expand Down Expand Up @@ -1203,7 +1203,7 @@ try
foreach($incorrectParameter in $testIncorrectParameters.GetEnumerator())
{
$objectParameter = $mockCompareSingleServiceAccountNotCompliantOtherParameters |
Where-Object { $_.Parameter -eq $incorrectParameter.Name }
Where-Object -FilterScript { $_.Parameter -eq $incorrectParameter.Name }
$objectParameter.Expected = $incorrectParameter.Value
$objectParameter.Pass = $false

Expand Down Expand Up @@ -1245,7 +1245,7 @@ try
}

#region Incorrect Account type setup
$objectAccountType = $mockCompareSingleServiceAccountNotCompliantAccountType | Where-Object {$_.Parameter -eq 'AccountType'}
$objectAccountType = $mockCompareSingleServiceAccountNotCompliantAccountType | Where-Object -FilterScript {$_.Parameter -eq 'AccountType'}
$objectAccountType.Expected = 'Group'
$objectAccountType.Pass = $false

Expand Down Expand Up @@ -1291,7 +1291,7 @@ try
}

#region Incorrect Ensure setup
$objectEnsure = $mockCompareSingleServiceAccountNotCompliantEnsure | Where-Object {$_.Parameter -eq 'Ensure'}
$objectEnsure = $mockCompareSingleServiceAccountNotCompliantEnsure | Where-Object -FilterScript {$_.Parameter -eq 'Ensure'}
$objectEnsure.Expected = 'Absent'
$objectEnsure.Pass = $false

Expand Down Expand Up @@ -1327,7 +1327,7 @@ try
$mockCompareGroupServiceAccountNotCompliantEnsure = Copy-ArrayObjects $mockCompareGroupServiceAccount

#region Incorrect Path setup
$objectPath = $mockCompareGroupServiceAccountNotCompliantPath | Where-Object {$_.Parameter -eq 'Path'}
$objectPath = $mockCompareGroupServiceAccountNotCompliantPath | Where-Object -FilterScript {$_.Parameter -eq 'Path'}
$objectPath.Expected = 'WrongPath'
$objectPath.Pass = $false

Expand Down Expand Up @@ -1365,7 +1365,7 @@ try
foreach($incorrectParameter in $testIncorrectParameters.GetEnumerator())
{
$objectParameter = $mockCompareGroupServiceAccountNotCompliantOtherParameters |
Where-Object { $_.Parameter -eq $incorrectParameter.Name }
Where-Object -FilterScript { $_.Parameter -eq $incorrectParameter.Name }
$objectParameter.Expected = $incorrectParameter.Value
$objectParameter.Pass = $false

Expand Down Expand Up @@ -1408,7 +1408,7 @@ try
}

#region Incorrect Account type setup
$objectAccountType = $mockCompareGroupServiceAccountNotCompliantAccountType | Where-Object {$_.Parameter -eq 'AccountType'}
$objectAccountType = $mockCompareGroupServiceAccountNotCompliantAccountType | Where-Object -FilterScript {$_.Parameter -eq 'AccountType'}
$objectAccountType.Expected = 'Single'
$objectAccountType.Pass = $false

Expand Down Expand Up @@ -1453,7 +1453,7 @@ try
}

#region Incorrect Ensure setup
$objectEnsure = $mockCompareGroupServiceAccountNotCompliantEnsure | Where-Object {$_.Parameter -eq 'Ensure'}
$objectEnsure = $mockCompareGroupServiceAccountNotCompliantEnsure | Where-Object -FilterScript {$_.Parameter -eq 'Ensure'}
$objectEnsure.Expected = 'Absent'
$objectEnsure.Pass = $false

Expand Down Expand Up @@ -1486,7 +1486,7 @@ try
$mockCompareSingleServiceAccountNotEnsure = Copy-ArrayObjects $mockCompareSingleServiceAccount

#region Incorrect Ensure setup
$objectEnsure = $mockCompareSingleServiceAccountNotEnsure | Where-Object {$_.Parameter -eq 'Ensure'}
$objectEnsure = $mockCompareSingleServiceAccountNotEnsure | Where-Object -FilterScript {$_.Parameter -eq 'Ensure'}
$objectEnsure.Expected = 'Present'
$objectEnsure.Actual = 'Absent'
$objectEnsure.Pass = $false
Expand Down Expand Up @@ -1524,7 +1524,7 @@ try
$mockCompareSingleServiceAccountNotCompliantPath = Copy-ArrayObjects $mockCompareSingleServiceAccount

#region Incorrect Path setup
$objectPath = $mockCompareSingleServiceAccountNotCompliantPath | Where-Object {$_.Parameter -eq 'Path'}
$objectPath = $mockCompareSingleServiceAccountNotCompliantPath | Where-Object -FilterScript {$_.Parameter -eq 'Path'}
$objectPath.Expected = 'WrongPath'
$objectPath.Pass = $false

Expand Down

0 comments on commit e7e001a

Please sign in to comment.