Home > Software design >  Write-Host is not returning my variable data properly
Write-Host is not returning my variable data properly

Time:05-05

I am trying to use a CSV file to add a list of users to an AzureAD group I have set up in our tenant. It appears that the script is working, but for some reason one particular write-host in one of my If statements is not showing my variable data as expected. I am using the same variable in all of my other write-hosts and they all work so I'm not sure what I'm missing here.

My CSV looks like this if it matters:

Name,InvitedUserEmailAddress
Test User01,[email protected]
Test User02,[email protected]

Here is my simplified PS snippet.

$users = import-csv "D:\UserListTest1.csv"
$groupID = Get-AzureADGroup -SearchString "TestGroup" | Select-Object ObjectId, displayname

foreach ($email in $users) {
    # Pull usersAAD email list from AzureAD
    $usersAAD = Get-AzureADUser -SearchString $($email.InvitedUserEmailAddress) | Select-Object ObjectId, displayname, mail

    # Users from CSV not in AzureAD
    if ($usersAAD.mail -eq $null) {
        Write-Host "User $($usersAAD.displayname) does not exist in AzureAD" -ForegroundColor Red
    }
    else {
        # Pull AzureAD user group membership from users that exist in AzureAD
        $ExistingGroups = Get-AzureADUserMembership -ObjectId $usersAAD.ObjectId | Select-Object displayname, objectid

        # Users that are already members of the AzureAD group
        if ($ExistingGroups.ObjectId -eq $groupID.objectId) {
            Write-Host "$($usersAAD.displayname) already exists in $($groupID.displayname)" -ForeGroundColor Yellow
        }
        else {
            # Add users to AzureAD group if they are not already part of AzureAD group
            Add-AzureADGroupMember -ObjectId $groupID.ObjectId -RefObjectId $usersAAD.ObjectId
            Write-Host "Added $($usersAAD.displayname) to $($GroupID.displayname)" -ForeGroundColor Green
        }
    }
}

The issue is with the write-host results from the If statements below which happens when the user is already in the group.

# Users from CSV not in AzureAD
if ($usersAAD.mail -eq $null) {
    Write-Host "User $($usersAAD.displayname) does not exist in AzureAD" -ForegroundColor Red
}

In my example, [email protected] does not exist in my AzureAD tenant, so I'm expecting a red text showing "User [email protected] does not exist in AzureAD" for this user. Instead I see the following output. Test User01 is working fine but my Test User02 is not. Sorry for the bad formatting.

Test User01 already exists in TestGroup  
User  does not exist in AzureAD 

Why would it have a null value for users that are already part of the group? It even adds a space to the output. I have tried removing the .displayname object as well but it doesn't help anything.

One odd thing that may have something to do with it is my $usersAAD variable seems to be empty after I run the whole thing. If I do a write-host $usersAAD after the whole thing runs (even though it invites users properly), it doesn't return any results.

CodePudding user response:

As explained in comments, "User $($usersAAD.displayname) does not... is referring to an object that does not exist ($null) when the condition $usersAAD.mail -eq $null is $true, which is why in your output you get User does not exist in AzureAD. To fix this, you can refer to the item ($email) in your collection (the Csv).

Here is my take on your code and said fix included as well as some inline comments to help you with the thought process.

$groupID = Get-AzureADGroup -SearchString "TestGroup"

foreach ($email in Import-Csv "D:\UserListTest1.csv") {
    # if this user exists in Azure AD
    if ($usersAAD = Get-AzureADUser -SearchString $email.InvitedUserEmailAddress) {
        # get the membership
        $ExistingGroups = Get-AzureADUserMembership -ObjectId $usersAAD.ObjectId
        # and check if the test group is part of the user membership
        # (notice `-contains` here is faster than `-eq` !!!)
        if ($ExistingGroups.ObjectId -contains $groupID.objectId) {
            Write-Host "$($usersAAD.displayname) already exists in $($groupID.displayname)" -ForeGroundColor Yellow
            # if this condition was `$true` just go to the next item in our loop
            continue 
        }
        # if we're here above condition was `$false`, so add this user to the test group
        Add-AzureADGroupMember -ObjectId $groupID.ObjectId -RefObjectId $usersAAD.ObjectId
        Write-Host "Added $($usersAAD.displayname) to $($GroupID.displayname)" -ForeGroundColor Green
        # and go to next item in loop
        continue 
    }
    # if we're here we can assume the user did not exist in Azure AD, hence:
    Write-Host "User $($email.Name) does not exist in AzureAD" -ForegroundColor Red
}
  • Related