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?
{up on the same line as|ForEach-Object, otherwise the parser won't recognize it as a parameter argument