Skip to content

Commit 1f7e080

Browse files
committed
Added status message
1 parent 62290be commit 1f7e080

File tree

1 file changed

+19
-29
lines changed

1 file changed

+19
-29
lines changed

src/Update-MsIdInvitedUserSponsorsFromInvitedBy.ps1

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ Invite api - https://learn.microsoft.com/en-us/graph/api/resources/invitation?vi
1414
ELM - https://learn.microsoft.com/en-us/graph/api/resources/entitlementmanagement-overview?view=graph-rest-beta
1515
Invited BY - https://learn.microsoft.com/en-us/graph/api/user-list-invitedby?view=graph-rest-beta
1616
17+
.EXAMPLE
18+
Update-MsIdInvitedUserSponsorsFromInvitedBy
19+
20+
Enumerate all invited users in the Tenant and update Sponsors using InvitedBy value
21+
1722
.EXAMPLE
1823
Update-MsIdInvitedUserSponsorsFromInvitedBy -All
1924
2025
Enumerate all invited users in the Tenant and update Sponsors using InvitedBy value
26+
2127
.EXAMPLE
2228
Update-MsIdInvitedUserSponsorsFromInvitedBy -UserId user1@contoso.com,user2@contoso.com
2329
@@ -33,7 +39,7 @@ function Update-MsIdInvitedUserSponsorsFromInvitedBy {
3339
[Parameter(ParameterSetName = 'ByUsers')]
3440
[String[]]
3541
$UserId,
36-
# Enumerate and Update All Guest Users
42+
# Enumerate and Update All Guest Users.
3743
[Parameter(ParameterSetName = 'AllInvitedGuests')]
3844
[switch]
3945
$All
@@ -46,36 +52,35 @@ function Update-MsIdInvitedUserSponsorsFromInvitedBy {
4652
if (!(Test-MgCommandPrerequisites 'Get-Mguser', 'Update-Mguser' -MinimumVersion 2.8.0 -ErrorVariable CriticalError)) { return }
4753

4854
$guestFilter = "(CreationType eq 'Invitation')"
49-
5055
}
5156

5257
process {
5358
if ($CriticalError) { return }
54-
if ($All) {
59+
if ($null -eq $UserId -and !$All) {
60+
Write-Error "Please specify either -UserId or -All"
61+
return
62+
}
5563

64+
if ($All) {
5665
$InvitedUsers = Get-MgUser -Filter $guestFilter -All -ExpandProperty Sponsors
5766
}
5867
else {
5968
foreach ($user in $userId) {
60-
6169
$InvitedUsers += Get-MgUser -UserId $user -ExpandProperty Sponsors
6270
}
6371
}
6472

6573
if ($null -eq $InvitedUsers) {
66-
Write-Information "No Guest Users to Process!"
74+
Write-Error "No guest users to process"
6775
}
6876
else {
6977
foreach ($InvitedUser in $InvitedUsers) {
7078
$invitedBy = $null
7179

7280
$splatArgumentsGetInvitedBy = @{
73-
7481
Method = 'Get'
7582
Uri = ((Get-MgEnvironment -Name (Get-MgContext).Environment).GraphEndpoint +
7683
"/beta/users/" + $InvitedUser.Id + "/invitedBy")
77-
78-
7984
}
8085

8186
$invitedBy = Invoke-MgGraphRequest @splatArgumentsGetInvitedBy
@@ -86,50 +91,35 @@ function Update-MsIdInvitedUserSponsorsFromInvitedBy {
8691
Write-Verbose ("InvitedBy for Guest User {0}: {1}" -f $InvitedUser.DisplayName, $invitedBy.value.id)
8792

8893
if (($null -like $InvitedUser.Sponsors) -or ($InvitedUser.Sponsors.id -notcontains $invitedBy.value.id)) {
89-
Write-Verbose ("Sponsors does not contain the user who invited them!")
94+
Write-Verbose "Sponsors does not contain the user who invited them!"
9095

91-
if ($PSCmdlet.ShouldProcess(("{0} - {1}" -f $InvitedUser.displayName, $InvitedUser.id), "Update Sponsors")) {
96+
if ($PSCmdlet.ShouldProcess(("$($InvitedUser.displayName) ($($InvitedUser.UserPrincipalName) - $($InvitedUser.id))"), "Update Sponsors")) {
9297
try {
93-
94-
$sponsosUrl = $null
95-
$dirobj = $null
96-
$sponsorsRequestBody = $null
97-
9898
$sponsorUrl = ("https://graph.microsoft.com/beta/users/{0}" -f $invitedBy.value.id)
9999
$dirObj = @{"sponsors@odata.bind" = @($sponsorUrl) }
100100
$sponsorsRequestBody = $dirObj | ConvertTo-Json
101101

102-
103102
Update-MgUser -UserId $InvitedUser.Id -BodyParameter $sponsorsRequestBody
104-
Write-Verbose ("Sponsors Updated for {0}" -f $InvitedUser.DisplayName)
105-
103+
Write-Output "$($InvitedUser.UserPrincipalName) - Sponsor updated succesfully for this user."
106104
}
107105
catch {
108-
106+
Write-Output "$($InvitedUser.UserPrincipalName) - Failed updating sponsor for this user."
109107
Write-Error $_
110-
111108
}
112109
}
113-
114-
115110
}
116111
else {
117-
Write-Verbose ("------------> Sponsors already contains the user who invited them!")
112+
Write-Output "$($InvitedUser.UserPrincipalName) - Sponsor already exists for this user."
118113
}
119114
}
120115
else {
121-
write-verbose ("------->InvitedBy is not available for this user!")
116+
Write-Output "$($InvitedUser.UserPrincipalName) - Invited user information not available for this user."
122117
}
123-
124-
125118
}
126119
}
127-
128120
}
129121

130122
end {
131-
132123
Write-Verbose "Complete!"
133-
134124
}
135125
}

0 commit comments

Comments
 (0)