Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
72dc269
add set-entra user sponsor cmdlet
Mar 9, 2025
cc07ac0
add docs
Mar 10, 2025
204bec0
Merge branch 'main' into add_set_entra_user_command
KenitoInc Mar 11, 2025
0f51fa6
add help message functions
Mar 12, 2025
f498f8e
Merge branch 'add_set_entra_user_command' of https://github.com/micro…
Mar 12, 2025
0d21241
add set-entra user sponsor cmdlet
Mar 9, 2025
f9d9763
add docs
Mar 10, 2025
a541200
add help message functions
Mar 12, 2025
8ec38bf
Update module/docs/entra-powershell-beta/Users/Set-EntraBetaUserSpons…
DButoyez Mar 14, 2025
903f2e6
Update module/docs/entra-powershell-beta/Users/Set-EntraBetaUserSpons…
DButoyez Mar 14, 2025
55a1a24
Update module/docs/entra-powershell-v1.0/Users/Set-EntraUserSponsor.md
DButoyez Mar 14, 2025
b203215
Update module/docs/entra-powershell-v1.0/Users/Set-EntraUserSponsor.md
DButoyez Mar 14, 2025
f3c5684
address PR feedback
Mar 18, 2025
4103978
address PR feedback
Mar 18, 2025
7825003
Merge branch 'main' into add_set_entra_user_command
SteveMutungi254 Mar 18, 2025
8c6f3e9
add header tests
Mar 19, 2025
22ccac3
Merge branch 'add_set_entra_user_command' of https://github.com/micro…
Mar 19, 2025
eb31ec0
Merge branch 'main' into add_set_entra_user_command
SteveMutungi254 Mar 22, 2025
617898a
Set-EntraUserSponsor - Review suggestions (#1425)
SteveMutungi254 Mar 25, 2025
ca281f9
Merge branch 'main' into add_set_entra_user_command
SteveMutungi254 Mar 25, 2025
cb21a82
Update Set-EntraBetaUserSponsor.md
DButoyez Mar 26, 2025
109c6e0
update Set-EntraUserSponsor doc
Mar 26, 2025
6fc2381
Update module/Entra/Microsoft.Entra/Users/Set-EntraUserSponsor.ps1
DButoyez Mar 26, 2025
2d4f752
Update module/Entra/Microsoft.Entra/Users/Set-EntraUserSponsor.ps1
DButoyez Mar 26, 2025
62d793c
Update module/EntraBeta/Microsoft.Entra.Beta/Users/Set-EntraBetaUserS…
DButoyez Mar 26, 2025
3c6d15a
Update module/docs/entra-powershell-beta/Users/Set-EntraBetaUserSpons…
DButoyez Mar 26, 2025
ad1ad98
Update module/docs/entra-powershell-beta/Users/Set-EntraBetaUserSpons…
DButoyez Mar 26, 2025
c850bd8
Update module/docs/entra-powershell-beta/Users/Set-EntraBetaUserSpons…
DButoyez Mar 26, 2025
a778188
Update module/docs/entra-powershell-beta/Users/Set-EntraBetaUserSpons…
DButoyez Mar 26, 2025
3e3ca00
Update module/docs/entra-powershell-beta/Users/Set-EntraBetaUserSpons…
DButoyez Mar 26, 2025
f51fd1e
Update module/docs/entra-powershell-v1.0/Users/Set-EntraUserSponsor.md
DButoyez Mar 26, 2025
d1722e4
Update module/EntraBeta/Microsoft.Entra.Beta/Users/Set-EntraBetaUserS…
DButoyez Mar 26, 2025
4c8868c
Update Set-EntraBetaUserSponsor.md
DButoyez Mar 26, 2025
43d0b08
Update module/docs/entra-powershell-v1.0/Users/Set-EntraUserSponsor.md
DButoyez Mar 26, 2025
361055c
Update module/docs/entra-powershell-v1.0/Users/Set-EntraUserSponsor.md
DButoyez Mar 26, 2025
283feaa
Update Set-EntraUserSponsor.md
DButoyez Mar 26, 2025
9ab2223
Update module/docs/entra-powershell-v1.0/Users/Set-EntraUserSponsor.md
DButoyez Mar 26, 2025
eed49bc
Update module/docs/entra-powershell-v1.0/Users/Set-EntraUserSponsor.md
DButoyez Mar 26, 2025
dcd3347
Merge branch 'main' into add_set_entra_user_command
SteveMutungi254 Mar 26, 2025
651d3f7
Update module/docs/entra-powershell-beta/Users/Set-EntraBetaUserSpons…
DButoyez Mar 27, 2025
f7da517
Update module/docs/entra-powershell-beta/Users/Set-EntraBetaUserSpons…
DButoyez Mar 27, 2025
1cbb8e6
Update module/docs/entra-powershell-beta/Users/Set-EntraBetaUserSpons…
DButoyez Mar 27, 2025
272b8b2
Update module/docs/entra-powershell-v1.0/Users/Set-EntraUserSponsor.md
DButoyez Mar 27, 2025
0f78906
Update module/docs/entra-powershell-v1.0/Users/Set-EntraUserSponsor.md
DButoyez Mar 27, 2025
dec8052
Update module/docs/entra-powershell-v1.0/Users/Set-EntraUserSponsor.md
DButoyez Mar 27, 2025
3cb6c50
Update module/docs/entra-powershell-beta/Users/Set-EntraBetaUserSpons…
DButoyez Mar 27, 2025
07a1839
Update module/docs/entra-powershell-v1.0/Users/Set-EntraUserSponsor.md
DButoyez Mar 27, 2025
f495537
Update module/docs/entra-powershell-v1.0/Users/Set-EntraUserSponsor.md
DButoyez Mar 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions module/Entra/Microsoft.Entra/Users/Set-EntraUserSponsor.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# ------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All Rights Reserved.
# Licensed under the MIT License. See License in the project root for license information.
# ------------------------------------------------------------------------------

function Set-EntraUserSponsor {
[CmdletBinding(DefaultParameterSetName = "SetUserSponsor")]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "The unique identifier (User ID or User Principal Name) of the user whose sponsor information you want to set.")]
[Alias('ObjectId', 'UPN', 'Identity', 'UserPrincipalName')]
[ValidateNotNullOrEmpty()]
[System.String] $UserId,

[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Type of sponsor to assign to a User. Can be either 'User' or 'Group'.")]
[ValidateSet("User", "Group")]
[System.String] $Type,

[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "List of sponsors to assign to the user.")]
[System.String[]] $SponsorIds
)

begin {

# Ensure connection to Microsoft Entra
if (-not (Get-EntraContext)) {
$errorMessage = "Not connected to Microsoft Graph. Use 'Connect-Entra -Scopes User.ReadWrite.All' to authenticate."
Write-Error -Message $errorMessage -ErrorAction Stop
return
}

}

PROCESS {
# Set up headers
$customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand
$customHeaders['Content-Type'] = 'application/json'

$batchEndpoint = "https://graph.microsoft.com/v1.0/`$batch"

# Initialize request collection
$requests = @()

# Determine target endpoint based on parameter set
$targetResource = if ($Type -eq "User") { "users" } else { "groups" }
$targetEndpoint = "users/$UserId/sponsors/`$ref"

foreach ($sponsorId in $SponsorIds) {
$request = @{
id = $sponsorId
method = "POST"
url = "/$targetEndpoint"
body = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/$targetResource/$sponsorId"
}
headers = @{
"Content-Type" = "application/json"
}
}
$requests += $request
}

# Execute batch request with all sponsors
if ($requests.Count -gt 0) {
$batchBody = @{
requests = $requests
}

$response = Invoke-MgGraphRequest -Method POST -Uri $batchEndpoint -Body ($batchBody | ConvertTo-Json -Depth 4) -Headers $customHeaders | ConvertTo-Json -Depth 10 | ConvertFrom-Json

$batchResponse = $response.responses | ForEach-Object {
if ($_.status -ne 204) {
[PSCustomObject]@{
Id = $_.id
Error = $_.body.error.message
}
}
}
$batchResponse
}
else {
Write-Error "No sponsors to add."
}
}
}
Set-Alias -Name Update-EntraUserSponsor -Value Set-EntraUserSponsor -Description "Set or update the sponsor information for a user." -Scope Global -Force
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# ------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All Rights Reserved.
# Licensed under the MIT License. See License in the project root for license information.
# ------------------------------------------------------------------------------

function Set-EntraBetaUserSponsor {
[CmdletBinding(DefaultParameterSetName = "SetUserSponsor")]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "The unique identifier (User ID or User Principal Name) of the user whose sponsor information you want to set.")]
[Alias('ObjectId', 'UPN', 'Identity', 'UserPrincipalName')]
[ValidateNotNullOrEmpty()]
[System.String] $UserId,

[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Type of sponsor to assign to a User. Can be either 'User' or 'Group'.")]
[ValidateSet("User", "Group")]
[System.String] $Type,

[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "List of sponsors to assign to the user.")]
[System.String[]] $SponsorIds
)

begin {

# Ensure connection to Microsoft Entra
if (-not (Get-EntraContext)) {
$errorMessage = "Not connected to Microsoft Graph. Use 'Connect-Entra -Scopes User.ReadWrite.All' to authenticate."
Write-Error -Message $errorMessage -ErrorAction Stop
return
}

}

PROCESS {
# Set up headers
$customHeaders = New-EntraBetaCustomHeaders -Command $MyInvocation.MyCommand
$customHeaders['Content-Type'] = 'application/json'

$batchEndpoint = "https://graph.microsoft.com/beta/`$batch"

# Initialize request collection
$requests = @()

# Determine target endpoint based on parameter set
$targetResource = if ($Type -eq "User") { "users" } else { "groups" }
$targetEndpoint = "users/$UserId/sponsors/`$ref"

foreach ($sponsorId in $SponsorIds) {
$request = @{
id = $sponsorId
method = "POST"
url = "/$targetEndpoint"
body = @{
"@odata.id" = "https://graph.microsoft.com/beta/$targetResource/$sponsorId"
}
headers = @{
"Content-Type" = "application/json"
}
}
$requests += $request
}

# Execute batch request with all sponsors
if ($requests.Count -gt 0) {
$batchBody = @{
requests = $requests
}

$response = Invoke-MgGraphRequest -Method POST -Uri $batchEndpoint -Body ($batchBody | ConvertTo-Json -Depth 4) -Headers $customHeaders | ConvertTo-Json -Depth 10 | ConvertFrom-Json

$batchResponse = $response.responses | ForEach-Object {
if ($_.status -ne 204) {
[PSCustomObject]@{
Id = $_.id
Error = $_.body.error.message
}
}
}
$batchResponse
}
else {
Write-Error "No sponsors to add."
}
}
}
Set-Alias -Name Update-EntraBetaUserSponsor -Value Set-EntraBetaUserSponsor -Description "Set or update the sponsor information for a user." -Scope Global -Force
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,7 @@ This cmdlet supports the common parameters: `-Debug`, `-ErrorAction`, `-ErrorVar
## Notes

## Related Links

[Set-EntraBetaUserSponsor](Set-EntraBetaUserSponsor.md)

[Remove-EntraBetaUserSponsor](Remove-EntraBetaUserSponsor.md)
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,8 @@ This cmdlet supports the common parameters: `-Debug`, `-ErrorAction`, `-ErrorVar

## Notes

## Related Links
## Related Links

[Get-EntraBetaUserSponsor](Get-EntraBetaUserSponsor.md)

[Set-EntraBetaUserSponsor](Set-EntraBetaUserSponsor.md)
168 changes: 168 additions & 0 deletions module/docs/entra-powershell-beta/Users/Set-EntraBetaUserSponsor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
---
title: Set-EntraBetaUserSponsor
description: This article provides details on the Set-EntraBetaUserSponsor command.

ms.topic: reference
ms.date: 03/10/2025
ms.author: eunicewaweru
ms.reviewer: dbutoyi
manager: CelesteDG
author: msewaweru

external help file: Microsoft.Entra.Beta.Users-Help.xml
Module Name: Microsoft.Entra.Beta
online version: https://learn.microsoft.com/powershell/module/Microsoft.Entra.Beta/Set-EntraBetaUserSponsor

schema: 2.0.0
---

# Set-EntraBetaUserSponsor

## Synopsis

Sets a user's sponsors (users or groups).

## Syntax

### SetUserSponsor (Default)

```powershell
Set-EntraBetaUserSponsor
-UserId <String>
-Type <String>
-SponsorIds <String[]>
[<CommonParameters>]
```

## Description

The `Set-EntraBetaUserSponsor` cmdlet sets a user's sponsors (users or groups). The sponsor feature tracks who is responsible for each guest user by assigning a person or group, ensuring accountability.

`Update-EntraBetaUserSponsor` is an alias for `Set-EntraBetaUserSponsor`.

In delegated scenarios with work or school accounts, the signed-in user must have a supported Microsoft Entra role or a custom role that includes the `microsoft.directory/users/sponsors/update` permission.

The following least privileged roles support this operation:

- Directory Writers
- User Administrator

## Examples

### Example 1: Assign single user sponsor

```powershell
Connect-Entra -Scopes 'User.ReadWrite.All'
$sponsor = Get-EntraBetaUser -UserId 'SponsorEmail@contoso.com'
Set-EntraBetaUserSponsor -UserId 'JohnstoneH@fabrikam.com' -Type User -SponsorIds $sponsor.Id
```

This example demonstrates how to assign a single user, as a sponsor to a target user account.

- The `-UserId` parameter specifies the User ID or User Principal Name. You can use `-UserPrincipalName`, `-Identity`, `-UPN`, `-ObjectId` as an alias for `-UserId`.
- The `-Type` parameter specifies the type of sponsor being assigned to the user. Supported sponsor types are (User or Group).
- The `-SponsorIds` parameter specifies the Ids of sponsors to be assigned to the user.

### Example 2: Assign single group sponsor

```powershell
Connect-Entra -Scopes 'User.ReadWrite.All', 'GroupMember.Read.All'
$group = Get-EntraBetaGroup -SearchString 'Sales and Marketing'
Set-EntraBetaUserSponsor -UserId 'JohnstoneH@fabrikam.com' -Type Group -SponsorIds $group.Id
```

This example demonstrates how to assign a single group, as a sponsor to a target user account.

- The `-UserId` parameter specifies the User ID or User Principal Name. You can use `-UserPrincipalName`, `-Identity`, `-UPN`, `-ObjectId` as an alias for `-UserId`.
- The `-Type` parameter specifies the type of sponsor being assigned to the user. Supported sponsor types are (User or Group).
- The `-SponsorIds` parameter specifies the Ids of sponsors to be assigned to the user.

### Example 3: Assign multiple user sponsors

```powershell
Connect-Entra -Scopes 'User.ReadWrite.All'
Set-EntraBetaUserSponsor -UserId 'JohnstoneH@fabrikam.com' -Type User -SponsorIds @("db0c6l50-93ee-4b22-9bb9-c8454875d990","c9db38b9-e5b8-4b5e-ak78-9812230af58d")
```

This example demonstrates how to assign multiple users as sponsors to a target user account.

- The `-UserId` parameter specifies the User ID or User Principal Name. You can use `-UserPrincipalName`, `-Identity`, `-UPN`, `-ObjectId` as an alias for `-UserId`.
- The `-Type` parameter specifies the type of sponsor being assigned to the user. Supported sponsor types are (User or Group).
- The `-SponsorIds` parameter specifies the Ids of sponsors to be assigned to the user.

### Example 4: Assign multiple group sponsors

```powershell
Connect-Entra -Scopes 'User.ReadWrite.All'
Set-EntraBetaUserSponsor -UserId 'JohnstoneH@fabrikam.com' -Type Group -SponsorIds @("db0c6f50-93ee-4b22-9bb9-c8454875d990","c9db38b9-e5v8-4b5e-ae78-9812230af58d")
```

This example demonstrates how to assign multiple groups as sponsors to a target user account.

- The `-UserId` parameter specifies the User ID or User Principal Name. You can use `-UserPrincipalName`, `-Identity`, `-UPN`, `-ObjectId` as an alias for `-UserId`.
- The `-Type` parameter specifies the type of sponsor being assigned to the user. Supported sponsor types are (User or Group).
- The `-SponsorIds` parameter specifies the Ids of sponsors to be assigned to the user.

## Parameters

### -Type

Specifies the type of sponsors being assigned to the user. Supported sponsor types are (User or Group).

```yaml
Type: System.String
Parameter Sets: (All)

Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName, ByValue)
Accept wildcard characters: False
```

### -UserId

Specifies the ID (as a UserPrincipalName or UserId) of a user in Microsoft Entra ID.

```yaml
Type: System.String
Parameter Sets: (All)
Aliases: ObjectId, UPN, Identity, UserPrincipalName

Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName, ByValue)
Accept wildcard characters: False
```

### -SponsorId

Specifies the sponsor ID (user or group ID) to assign to the specific user target.

```yaml
Type: System.String[]
Parameter Sets: (All)

Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName, ByValue)
Accept wildcard characters: False
```

### CommonParameters

This cmdlet supports the common parameters: `-Debug`, `-ErrorAction`, `-ErrorVariable`, `-InformationAction`, `-InformationVariable`, `-OutVariable`, `-OutBuffer`, `-PipelineVariable`, `-Verbose`, `-WarningAction`, and `-WarningVariable`. For more information, see [about_CommonParameters](https://go.microsoft.com/fwlink/?LinkID=113216).

## Inputs

## Outputs

## Notes

## Related Links

[Get-EntraBetaUserSponsor](Get-EntraBetaUserSponsor.md)

[Remove-EntraBetaUserSponsor](Remove-EntraBetaUserSponsor.md)
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,7 @@ This cmdlet supports the common parameters: `-Debug`, `-ErrorAction`, `-ErrorVar
## Notes

## Related Links

[Set-EntraUserSponsor](Set-EntraUserSponsor.md)

[Remove-EntraUserSponsor](Remove-EntraUserSponsor.md)
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,8 @@ This cmdlet supports the common parameters: `-Debug`, `-ErrorAction`, `-ErrorVar

## Notes

## Related Links
## Related Links

[Get-EntraUserSponsor](Get-EntraUserSponsor.md)

[Set-EntraUserSponsor](Set-EntraUserSponsor.md)
Loading