Home > Net >  Setting variables based on value taken from CSV within a Foreach loop using an if else statement
Setting variables based on value taken from CSV within a Foreach loop using an if else statement

Time:03-25

I've created a script that takes new user data from a CSV file and connects to AzureAd and ExchangeOnline to create their AzureAD account, assigns them an Office license (not shown), and updates their Mailbox Office field.

CSV headers are Name, Department, OfficeLocation. The CSV used to contain a Domain and Company header. I removed those headers and added an if elseif statement to provide the logic to set those variables within the script. Prior to this addition, the script worked without any issues.

Now, the $company and $domain values are only updating for $main_offices and $corporate_offices Contoso and @contoso.com even when the OfficeLocation value is San Francisco or Austin and those values should be West/South Acme and west/south.acme.com.

Why are my $company and $domain values not being updated within the ForEach-Object loop as it iterates through the CSV? I confirmed that $company and $domain update properly when not reading in CSV data with ForEach-Object:

$new_users = Import-Csv -Path .\new-users.csv

...

$main_offices = 'New York','Los Angeles','Houston','Atlanta','Chicago'
$corporate_offices = 'Corporate Office (NY)','Corporate Office (LA)'
$west_office = 'San Francisco'
$south_office = 'Austin'

$new_users | ForEach-Object {
    $first, $last = $_.Name.Split()
    $mailnickname = $(($first.Substring(0,1)   $last).ToLower())
    $password_profile = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile
    $password_profile.Password = 'XXXXXXXXXXXXXXXXXX'
    $password_profile.ForceChangePasswordNextLogin = $false
    $off_loc = $_.OfficeLocation

    if ($off_loc -in $main_offices -or $corporate_offices) {
        $company = 'Contoso'
        $domain = '@contoso.com'
    } elseif ($off_loc -eq $west_office) {
        $company = 'West Acme'
        $domain = '@west.acme.com'
    } elseif ($off_loc -eq $south_office) {
        $company = 'South Acme'
        $domain = '@south.acme.com'
    } else { $off_loc = Read-Host 'Type an office location' } #CSV OfficeLocation field either missing or has a spelling error

    $attr_new_user = @{
        AccountEnabled = $true
        CompanyName = $company
        Department = $_.Department
        DisplayName = $_.Name
        GivenName = $first
        MailNickname = $mailnickname
        PasswordProfile = $password_profile
        Surname = $last
        UsageLocation = 'US'
        UserPrincipalName = $mailnickname   $domain
    }
    try {
        Write-Host ('>> Creating account for '   $attr_new_user.DisplayName) -ForegroundColor Yellow
        New-AzureADUser @attr_new_user | Out-Null
        $user_upn = Get-AzureADUser -ObjectId $attr_new_user.UserPrincipalName | Select-Object -ExpandProperty UserPrincipalName
        Write-Host ('>> '   $user_upn   ' has been created') -ForegroundColor Green
    }
    catch {
        Write-Host ('>> Something went wrong') -ForegroundColor Red
        Write-Warning $Error[0]
    }

...

    try {
        Write-Host ('>> Adding email alias: '   $alternate_email   ' and office: '   $off_loc   ' to '   $user_upn) -ForegroundColor Yellow
        Set-Mailbox -Identity $user_upn -EmailAddresses @{ add = $alternate_email } -Office $off_loc
        Write-Host ('>> Email Alias: '   $alternate_email   ' and office: '   $off_loc   ' added to '   $user_upn) -ForegroundColor Green
    }
    catch {
        Write-Host ('>> Something went wrong') -ForegroundColor Red
        Write-Warning $Error[0]
    }

I've run the script and the $off_loc value is being inputted correctly in the Office field of the Mailbox settings. Which is why I am having trouble understanding how to get this information to create the user with the correct the $company and $domain fields.

Any insight into a solution to this issue is appreciated, thank you for taking the time to answer my question.

CodePudding user response:

Per @SantiagoSquarzon:

This condition $off_loc -in $main_offices -or $corporate_offices will always be $true because $corporate_offices is not $null or empty string. It should be $off_loc -in $main_offices -or $off_loc -in $corporate_offices

Confirmed this resolved the issue.

  • Related