mardi 16 janvier 2018

Mock Get-ADUser with and without ParameterFilter

I'm pretty new to Pester so please bear with me. We're trying to make tests for a very large script that does some active directory queries and validates data. I've simplified it a bit to the following example:

Dummy.ps1

Param (
    [String]$OU,
    [String[]]$Groups
)

$AllOUusers = Get-ADUser -Filter * -SearchBase $OU

$GroupMemberUsers = foreach ($G in $Groups) {
    Get-ADGroupMember $G -Recursive | Get-ADUser -Properties whenCreated    
}

Dummy.Tests.ps1

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'

$Params = @{
    OU = 'OU=users,DC=contoso,DC=com'
    Groups = 'Group1', 'Group2'
}

Describe 'test' {
    Mock Get-ADGroupMember {
        [PSCustomObject]@{SamAccountName = 'Member1'}
        [PSCustomObject]@{SamAccountName = 'Member2'}
    }
    Mock Get-ADUser {
        [PSCustomObject]@{SamAccountName = 'User1'}
        [PSCustomObject]@{SamAccountName = 'User2'}
        [PSCustomObject]@{SamAccountName = 'User3'}
    }
    Mock Get-ADUser {
        [PSCustomObject]@{SamAccountName = 'User4'}
        [PSCustomObject]@{SamAccountName = 'User5'}
    } -ParameterFilter {$identity -eq 'User1'}

    ."$here\$sut" @Params

    it 'test 1' {
        ($AllOUusers | Measure-Object).Count | Should -BeExactly 3
    }
    it 'test 2' {
        ($GroupMemberUsers | Measure-Object).Count | Should -BeExactly 2
        'User4', 'User5' | Should -BeIn $GroupMemberUsers.SamAccountName
    }
}

Error:

Cannot validate argument on parameter 'Identity'.

In the case above we're trying to collect in the first place all user accounts in a specific OU and afterwards all the members of a specific security group. When we have the members we use the Get-ADUser CmdLet again to get more details for these specific members.

The end goal is to have a Mock for the first Get-ADUser that returns all the user accounts, and a second Mock of Get-ADUser that only returns a specific set of user accounts based on group membership (or simulated group membership).

Aucun commentaire:

Enregistrer un commentaire