Skip to content

Commit

Permalink
Added status message
Browse files Browse the repository at this point in the history
  • Loading branch information
merill committed Aug 22, 2024
1 parent 62290be commit 1f7e080
Showing 1 changed file with 19 additions and 29 deletions.
48 changes: 19 additions & 29 deletions src/Update-MsIdInvitedUserSponsorsFromInvitedBy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ Invite api - https://learn.microsoft.com/en-us/graph/api/resources/invitation?vi
ELM - https://learn.microsoft.com/en-us/graph/api/resources/entitlementmanagement-overview?view=graph-rest-beta
Invited BY - https://learn.microsoft.com/en-us/graph/api/user-list-invitedby?view=graph-rest-beta
.EXAMPLE
Update-MsIdInvitedUserSponsorsFromInvitedBy
Enumerate all invited users in the Tenant and update Sponsors using InvitedBy value
.EXAMPLE
Update-MsIdInvitedUserSponsorsFromInvitedBy -All
Enumerate all invited users in the Tenant and update Sponsors using InvitedBy value
.EXAMPLE
Update-MsIdInvitedUserSponsorsFromInvitedBy -UserId [email protected],[email protected]
Expand All @@ -33,7 +39,7 @@ function Update-MsIdInvitedUserSponsorsFromInvitedBy {
[Parameter(ParameterSetName = 'ByUsers')]
[String[]]
$UserId,
# Enumerate and Update All Guest Users
# Enumerate and Update All Guest Users.
[Parameter(ParameterSetName = 'AllInvitedGuests')]
[switch]
$All
Expand All @@ -46,36 +52,35 @@ function Update-MsIdInvitedUserSponsorsFromInvitedBy {
if (!(Test-MgCommandPrerequisites 'Get-Mguser', 'Update-Mguser' -MinimumVersion 2.8.0 -ErrorVariable CriticalError)) { return }

$guestFilter = "(CreationType eq 'Invitation')"

}

process {
if ($CriticalError) { return }
if ($All) {
if ($null -eq $UserId -and !$All) {
Write-Error "Please specify either -UserId or -All"
return
}

if ($All) {
$InvitedUsers = Get-MgUser -Filter $guestFilter -All -ExpandProperty Sponsors
}
else {
foreach ($user in $userId) {

$InvitedUsers += Get-MgUser -UserId $user -ExpandProperty Sponsors
}
}

if ($null -eq $InvitedUsers) {
Write-Information "No Guest Users to Process!"
Write-Error "No guest users to process"
}
else {
foreach ($InvitedUser in $InvitedUsers) {
$invitedBy = $null

$splatArgumentsGetInvitedBy = @{

Method = 'Get'
Uri = ((Get-MgEnvironment -Name (Get-MgContext).Environment).GraphEndpoint +
"/beta/users/" + $InvitedUser.Id + "/invitedBy")


}

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

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

if ($PSCmdlet.ShouldProcess(("{0} - {1}" -f $InvitedUser.displayName, $InvitedUser.id), "Update Sponsors")) {
if ($PSCmdlet.ShouldProcess(("$($InvitedUser.displayName) ($($InvitedUser.UserPrincipalName) - $($InvitedUser.id))"), "Update Sponsors")) {
try {

$sponsosUrl = $null
$dirobj = $null
$sponsorsRequestBody = $null

$sponsorUrl = ("https://graph.microsoft.com/beta/users/{0}" -f $invitedBy.value.id)
$dirObj = @{"[email protected]" = @($sponsorUrl) }
$sponsorsRequestBody = $dirObj | ConvertTo-Json


Update-MgUser -UserId $InvitedUser.Id -BodyParameter $sponsorsRequestBody
Write-Verbose ("Sponsors Updated for {0}" -f $InvitedUser.DisplayName)

Write-Output "$($InvitedUser.UserPrincipalName) - Sponsor updated succesfully for this user."
}
catch {

Write-Output "$($InvitedUser.UserPrincipalName) - Failed updating sponsor for this user."
Write-Error $_

}
}


}
else {
Write-Verbose ("------------> Sponsors already contains the user who invited them!")
Write-Output "$($InvitedUser.UserPrincipalName) - Sponsor already exists for this user."
}
}
else {
write-verbose ("------->InvitedBy is not available for this user!")
Write-Output "$($InvitedUser.UserPrincipalName) - Invited user information not available for this user."
}


}
}

}

end {

Write-Verbose "Complete!"

}
}

0 comments on commit 1f7e080

Please sign in to comment.