-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInviteGuestUserAndAcceptToTeams.ps1
45 lines (35 loc) · 1.25 KB
/
InviteGuestUserAndAcceptToTeams.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Connect-AzureAD
Connect-MicrosoftTeams
$GroupId = "ENTER_YOUR_TEAMS_ID_HERE"
$users = Import-Csv -LiteralPath "..\PartnersPreviewCSV.csv"
$TeamGuests = Get-Teamuser -GroupId $GroupId -Role Guest
foreach($user in $users)
{
Write-Host Retrieving $user.Email
$emailValue = $user.Email
$userDetails = Get-AzureADUser -Filter "Mail eq '$emailValue'"
if($userDetails -eq $null)
{
Write-Host $emailValue not found in the tenant. Inviting via B2B...
New-AzureADMSInvitation -InvitedUserEmailAddress $emailValue -InviteRedirectUrl "https://www.microsoft.com/" -SendInvitationMessage $true
}
if($userDetails.UserState -eq "Accepted")
{
Write-Host $emailValue has status of $userDetails.UserState ... adding to the team.
$upnValue = $userDetails.UserPrincipalName
$userExists = $TeamGuests | Where-Object { $_.User -eq $userDetails.UserPrincipalName }
if($userExists -eq $null)
{
Add-Teamuser -GroupId $GroupId -User $emailValue -Role Member
}
else
{
Write-Host $emailValue already exists in the team
}
}
else
{
Write-Host $emailValue has B2B invite status of $userDetails.UserState
}
}
Write-Host "Done for today!"