简体   繁体   中英

ForEach-Object loop containing Get-ADUser and filter

My company has a script to handle licenses for O365, and current it assigns the same country to all users such as

$Location = "US"
$E2Users = Get-ADGroupMember lg.o365.Office | Select SamAccountName
$E2Users | ForEach-Object{
$UPN = $E2Users[$Counter].SamAccountName + "@company.com"
Set-MsolUser -UserPrincipalName $UPN -UsageLocation $Location
##lots of other code

I need to retrieve the country of each user and use that rather than the string. I've set up a little test to ensure I have the correct country by using out-put to see the results, however I can't seem to wrap my head around it to get it working. I saw the $username = $_ in another forum and tried to use it.

$E2Users = Get-ADGroupMember lg.o365.Office | Select SamAccountName
$E2Users | ForEach-Object{
$username = $_
$Location = Get-ADUSer -filter {samaccountname -like "*$username*"} -properties * | Select country
write-output $Location
}

Or do I even need a filter? Using the following gives me output of countries, but how to ensure they are matched up correctly with each user. Some resulting lines had blanks using country or c, and when checking the user account those attributes have values.

$E2Users = Get-ADGroupMember lg.o365.Office.PlanK1 | Select SamAccountName
$E2Users | ForEach-Object{
$Location = Get-ADUSer -filter * -properties * | Select country
write-output $Location
}

The end result would be something that looks like the following

$E2Users = Get-ADGroupMember lg.o365.Office | Select SamAccountName
$E2Users | ForEach-Object{
$UPN = $E2Users[$Counter].SamAccountName + "@company.com"
$Location = Get-ADUSer -filter {not sure what to use} -properties * | Select country    
Set-MsolUser -UserPrincipalName $UPN -UsageLocation $Location
##lots of other code

What am I missing and not understanding. Thank you for any help.

$E2Users = Get-ADGroupMember lg.o365.Office | Select -ExpandProperty SamAccountName
$E2Users | ForEach-Object {
    $username = $_ + '@company.com'
    $Location = Get-ADUSer -filter 'samaccountname -eq "$_"' -properties country | Select country
    Set-MsolUser -UserPrincipalName $username -UsageLocation $Location
}

If I understood your request correctly, feel free to ask if something isn't clear.

The problem here is that each member of the $E2Users array is an object with a single property called samaccountname and then you assign that object to the $username variable in your loop, which cant be used as a filter. There are a few ways to fix it but the simplest is probably to just expand the property, so change your first line to

$E2Users = Get-ADGroupMember lg.o365.Office | Select -expandproperty SamAccountName

I'd also chance your filter from a -like to a -eq and drop the "**" around it as they are not required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM