-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChangeB2BUserType.ps1
49 lines (40 loc) · 1.69 KB
/
ChangeB2BUserType.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
46
47
48
49
<#
.SYNOPSIS
This Azure Automation runbook finds all members of an Azure AD Security Group and changes the usertype to guest for those who are set as Member.
We're using PowerShell for Microsoft Graph
AUTHOR: Jorge Lopez
LASTEDIT: January 2022
Change GroupID accordingly
#>
#Obtain AccessToken for Microsoft Graph via the managed identity/Define Other Variables
$resourceURI = "https://graph.microsoft.com/"
$tokenAuthURI = $env:IDENTITY_ENDPOINT + "?resource=$resourceURI&api-version=2019-08-01"
$tokenResponse = Invoke-RestMethod -Method GET -Headers @{"X-IDENTITY-HEADER"="$env:IDENTITY_HEADER"} -Uri $tokenAuthURI
$accessToken = $tokenResponse.access_token
$UTmemberscount = 0
$Userschanged = 0
$GroupID = <<Your_GroupID_Here>>
#Define the desired graph endpoint
Select-MgProfile -Name 'beta'
#Connect to the Microsoft Graph using the aquired AccessToken
Connect-Graph -AccessToken $accessToken
#Get group name and Group Members
$groupName = (Get-MGGroup -groupid $GroupID).displayName
$Group_Members = Get-MgGroupMember -groupid $GroupID | ForEach-Object { Get-MgUser -UserId $_.Id }
$Group_Members_count = $Group_Members.count
Write-Output ("Checking members of group $groupName")
foreach ($user in $Group_Members)
{
if ($user.UserType -eq 'Member') {
Update-MgUser -UserId $user.UserPrincipalName -UserType "Guest"
$userUPN = $user.UserPrincipalName
Write-Output ("Converted "+ $userUPN + " to Guest User")
$Userschanged++
}
else {
$UTmemberscount++
}
}
Write-Output ("Users changed to Guest: $Userschanged")
Write-Output ("Users not Changed: $UTmemberscount")
Write-Output ("Total Users Checked: $Group_Members_count")