KrzychG
KrzychG

Reputation: 47

GetEnumerator() with ForEach-Object loop

I want to search for a user in multiple AD localizations. I'd like to avoid using multiple if statements by use of hash table but I can't do it properly. My attempt:

$ADSzukajTu = [ordered]@{
'1' = 'OU=sales,DC=contoso,DC=pl'
'2' = 'OU=hr,DC=contoso,DC=pl'
'3' = 'OU=marketing,DC=contoso,DC=pl'
'4' = 'OU=production,DC=contoso,DC=pl'
}

function SzukajADUsera ($fSzukajADUseraKogo)
{
    $i = 0
    do
    {
        $ADSzukajTu.GetEnumerator() | ForEach-Object
        {
            $ADUser = Get-ADUser -Filter $fSzukajADUseraKogo -Server $ADDC -SearchBase $_.Value -Properties name, sAMAccountName, sn, givenName, l, title, displayName, company, department, StreetAddress, Office, OfficePhone, Manager, mail, PostalCode, State, Country -ErrorAction SilentlyContinue | Select-Object -Property name, sAMAccountName, sn, givenName, l, title, displayName, company, department, StreetAddress, Office, OfficePhone, Manager, mail, PostalCode, State, Country
            $i++
        }
    }
    While ((($ADUser | Measure-Object).Count -lt 1) -and ($i -le $ADSzukajTu.Count))
    Return $ADUser
}

When I run the script and call the function

SzukajADUsera -fSzukajADUseraKogo "sAMAccountName -eq '$($WzorUser.sAMAccountName)'"

I get the prompt "cmdlet ForEach-Object at command pipeline position 1" to "*Supply values for the following parameters: Process".

I successfully use similar approach in replacing language specific characters.

$PodmianyPLZnaki = [ordered]@{
    'ą' = 'a'
    'ć' = 'c'
    'ę' = 'e'
    'ł' = 'l'
    'ń' = 'n'
    'ó' = 'o'
    'ś' = 's'
    'ź' = 'z'
    'ż' = 'z'
}

$NoweMail = ($WzorUser.GivenName + '.' + $WzorUser.Surname).ToLower()
$PodmianyPLZnaki.GetEnumerator() | ForEach-Object
{
    $NoweMail = $NoweMail -replace $_.Key, $_.Value
}

What am I doing wrong?

Upvotes: 1

Views: 1705

Answers (2)

KrzychG
KrzychG

Reputation: 47

As Mathias R. Jessen and John Seerden pointed out, putting the opening curly brace on the same line as ForEach-Object resolved the problem (so I can't let the PowerShell Studio change code layout). Tkank you again.
Ironically the loop I pointed out as working example stopped to work. I have to do the same correction to make it run again.

And the variable value was returned from the function when I call it like this:

$ADUser = SzukajADUsera -fSzukajADUseraKogo "sAMAccountName -eq '$($WzorUser.sAMAccountName)'"

Upvotes: 0

John Seerden
John Seerden

Reputation: 166

As Mathias R. Jessen said, put the opening curly brace on the same line as ForEach-Object. This is not only required after ForEach-Object, but also a better practice and will help you troubleshoot these situations better in the future.

I also believe there are better, more efficient ways to achieve the result you are after. Please refer to the following example, I tried to retain the language used in the variables.

function SzukajADUsera () {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        $fSzukajADUseraKogo
    )

    $ADSzukajTuArray = @(
        'OU=sales,DC=contoso,DC=pl'
        'OU=hr,DC=contoso,DC=pl'
        'OU=marketing,DC=contoso,DC=pl'
        'OU=production,DC=contoso,DC=pl'
    )

    $ADUser = Get-ADUser -Filter $fSzukajADUseraKogo
    foreach($ADSzukajTu in $ADSzukajTuArray) {
        if($ADUser.DistinguishedName -match $ADSzukaj) {
            return $ADUser
        }
    }
}

SzukajADUsera -fSzukajADUseraKogo "sAMAccountName -eq 'test.user'"

Upvotes: 2

Related Questions