1

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?

3
  • 4
    Move the opening { up on the same line as |ForEach-Object, otherwise the parser won't recognize it as a parameter argument Commented Aug 28, 2018 at 11:23
  • That's it. I use Sapien PowerShell Studio. It tends to put opening brackets in new line when I make some changes to the code. I was tired fighting with it. Especially it made no differency untill now. Commented Aug 28, 2018 at 12:44
  • How can I return $ADUser value to the calling program? Return doesn't work. Commented Aug 28, 2018 at 12:48

2 Answers 2

2

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'"
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You for your effort. Our AD have almost 20.000 user accounts so I dont't want to search in the entire structure. My accounts are in 4-5 OUs so I would rather narrow my search to them starting from the largest down to the smallest. And the same question as to Mathias R. Jessen: $ADUser value is not returned to the calling program. Why?
0

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)'"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.