Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get-EntraUserSponsor review suggestions #1369

Open
wants to merge 4 commits into
base: dbutoyi/user_sponsor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
43 changes: 17 additions & 26 deletions module/Entra/Microsoft.Entra/Users/Get-EntraUserSponsor.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,11 @@ function Get-EntraUserSponsor {
$params["Uri"] += "&$f=$Filter"
}
Write-Debug("============================ TRANSFORMATIONS ============================")
$params.Keys | ForEach-Object {"$_ : $($params[$_])" } | Write-Debug
$params.Keys | ForEach-Object { "$_ : $($params[$_])" } | Write-Debug
Write-Debug("=========================================================================`n")
$response = Invoke-GraphRequest -Headers $customHeaders -Uri $($params.Uri) -Method GET | ConvertTo-Json -Depth 10 | ConvertFrom-Json
try {
$data = $response.value | ConvertTo-Json -Depth 10 | ConvertFrom-Json
$directoryObjectList = @()
$all = $All.IsPresent
$increment = $topCount - $data.Count

Expand All @@ -74,31 +73,23 @@ function Get-EntraUserSponsor {
$response = Invoke-GraphRequest -Headers $customHeaders -Uri $URI -Method $Method | ConvertTo-Json -Depth 10 | ConvertFrom-Json
$data += $response.value | ConvertTo-Json -Depth 10 | ConvertFrom-Json
}
} catch {}

foreach ($item in $data) {
if ($null -ne $item) {
# Determine the type based on @odata.type
switch ($item.'@odata.type') {
'#microsoft.graph.user' {
$directoryObject = [Microsoft.Graph.PowerShell.Models.MicrosoftGraphUser]::new()
}
'#microsoft.graph.group' {
$directoryObject = [Microsoft.Graph.PowerShell.Models.MicrosoftGraphGroup]::new()
}
default {
Write-Warning "Unknown type: $($item.'@odata.type')"
continue
}
}
$item.PSObject.Properties | ForEach-Object {
$propertyName = $_.Name
$propertyValue = $_.Value
$directoryObject | Add-Member -MemberType NoteProperty -Name $propertyName -Value $propertyValue -Force
}
$directoryObjectList += $directoryObject
}
catch {}
if ($data) {
$memberList = @()
foreach ($response in $data) {
$memberType = New-Object Microsoft.Graph.PowerShell.Models.MicrosoftGraphDirectoryObject
if (-not ($response -is [PSObject])) {
$response = [PSCustomObject]@{ Value = $response }
}
$response.PSObject.Properties | ForEach-Object {
$propertyName = $_.Name
$propertyValue = $_.Value
$memberType | Add-Member -MemberType NoteProperty -Name $propertyName -Value $propertyValue -Force
}
$memberList += $memberType
}
$directoryObjectList
return $memberList
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function Get-EntraBetaUserSponsor {
$params["Uri"] += "&$f=$Filter"
}
Write-Debug("============================ TRANSFORMATIONS ============================")
$params.Keys | ForEach-Object {"$_ : $($params[$_])" } | Write-Debug
$params.Keys | ForEach-Object { "$_ : $($params[$_])" } | Write-Debug
Write-Debug("=========================================================================`n")
$response = Invoke-GraphRequest -Headers $customHeaders -Uri $($params.Uri) -Method GET | ConvertTo-Json -Depth 10 | ConvertFrom-Json
try {
Expand All @@ -74,31 +74,24 @@ function Get-EntraBetaUserSponsor {
$response = Invoke-GraphRequest -Headers $customHeaders -Uri $URI -Method $Method | ConvertTo-Json -Depth 10 | ConvertFrom-Json
$data += $response.value | ConvertTo-Json -Depth 10 | ConvertFrom-Json
}
} catch {}
}
catch {}

foreach ($item in $data) {
if ($null -ne $item) {
# Determine the type based on @odata.type
switch ($item.'@odata.type') {
'#microsoft.graph.user' {
$directoryObject = [Microsoft.Graph.PowerShell.Models.MicrosoftGraphUser]::new()
}
'#microsoft.graph.group' {
$directoryObject = [Microsoft.Graph.PowerShell.Models.MicrosoftGraphGroup]::new()
}
default {
Write-Warning "Unknown type: $($item.'@odata.type')"
continue
}
}
$item.PSObject.Properties | ForEach-Object {
$propertyName = $_.Name
$propertyValue = $_.Value
$directoryObject | Add-Member -MemberType NoteProperty -Name $propertyName -Value $propertyValue -Force
}
$directoryObjectList += $directoryObject
if ($data) {
$memberList = @()
foreach ($response in $data) {
$memberType = New-Object Microsoft.Graph.PowerShell.Models.MicrosoftGraphDirectoryObject
if (-not ($response -is [PSObject])) {
$response = [PSCustomObject]@{ Value = $response }
}
$response.PSObject.Properties | ForEach-Object {
$propertyName = $_.Name
$propertyValue = $_.Value
$memberType | Add-Member -MemberType NoteProperty -Name $propertyName -Value $propertyValue -Force
}
$memberList += $memberType
}
$directoryObjectList
return $memberList
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,14 @@ In delegated scenarios with work or school accounts, the signed-in user needs a
```powershell
Connect-Entra -Scopes 'User.Read', 'User.Read.All' # User.Read.All is application-only permission (non-interactive login)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this comment. Connect-Entra -Scopes is an interactive login.
Also, it doesn't make sense to specify 'User.Read' and 'User.Read.All' at the same time.

Get-EntraBetaUserSponsor -UserId '[email protected]' |
Select-Object Id, displayName, userPrincipalName, createdDateTime, accountEnabled, userType |
Format-Table -AutoSize
Select-Object Id, DisplayName, '@odata.type', CreatedDateTime | Format-Table -AutoSize
```

```Output
id displayName userPrincipalName createdDateTime accountEnabled userType
-- ----------- ----------------- --------------- -------------- --------
c0c97c58-1895-4910-b1bb-58f96db771df Adele Vance [email protected] 28/10/2024 09:50:43 True Member
79984fce-9e33-497a-a5a2-b3c85e3fedcb Alex Wilber [email protected] 28/10/2024 09:50:46 True Member
406e3c9f-7a2d-4ef0-b1d4-69ddbd2719bb Diego Siciliani [email protected] 28/10/2024 09:50:46 True Member
id displayName @odata.type createdDateTime
-- ----------- ----------- ---------------
cccccccc-2222-3333-4444-dddddddddddd Angel Brown #microsoft.graph.user 3/7/2024 3:10:31 AM
eeeeeeee-4444-5555-6666-ffffffffffff Helpdesk Group #microsoft.graph.group 8/7/2024 2:52:47 PM
```

This example shows how to list user sponsors.
Expand All @@ -76,36 +74,39 @@ This example shows how to list user sponsors.

```powershell
Connect-Entra -Scopes 'User.Read', 'User.Read.All' # User.Read.All is application-only permission (non-interactive login)
Get-EntraBetaUserSponsor -UserId '[email protected]' -Top 1 | Select-Object Id, DisplayName, '@odata.type'
Get-EntraBetaUserSponsor -UserId '[email protected]' -Top 1 |
Select-Object Id, DisplayName, '@odata.type', CreatedDateTime | Format-Table -AutoSize
```

```Output
Id displayName @odata.type
-- ----------- -----------
cccccccc-2222-3333-4444-dddddddddddd Contoso Group #microsoft.graph.group
id displayName @odata.type createdDateTime
-- ----------- ----------- ---------------
cccccccc-2222-3333-4444-dddddddddddd Angel Brown #microsoft.graph.user 3/7/2024 3:10:31 AM
```

This example retrieves the top sponsor for the specified user.
This example retrieves the top sponsor for the specified user. You can use `-Limit` as an alias for `-Top`.

- The `-UserId` parameter specifies the User ID or User Principal Name.

### Example 3: Retrieve the assigned sponsor for a specific user by their SponsorId

```powershell
Connect-Entra -Scopes 'User.Read', 'User.Read.All' # User.Read.All is application-only permission (non-interactive login)
Get-EntraBetaUserSponsor -UserId '[email protected]' -SponsorId c0c97c58-1895-4910-b1bb-58f96db771df | Select-Object Id, DisplayName, '@odata.type'
Get-EntraBetaUserSponsor -UserId '[email protected]' -SponsorId 'cccccccc-2222-3333-4444-dddddddddddd' |
Select-Object Id, DisplayName, '@odata.type', CreatedDateTime | Format-Table -AutoSize
```

```Output
Id displayName @odata.type
-- ----------- -----------
cccccccc-2222-3333-4444-dddddddddddd Contoso Group #microsoft.graph.group
id displayName @odata.type createdDateTime
-- ----------- ----------- ---------------
cccccccc-2222-3333-4444-dddddddddddd Angel Brown #microsoft.graph.user 3/7/2024 3:10:31 AM
eeeeeeee-4444-5555-6666-ffffffffffff Helpdesk Group #microsoft.graph.group 8/7/2024 2:52:47 PM
```

This example retrieves the assigned sponsor for the specified user.

- The `-UserId` parameter specifies the User ID or User Principal Name.
- The `-SponsorId` parameter specifies the specific user's sponsor ID to retrieve.
- The `-SponsorId` parameter specifies the specific user's sponsor ID to retrieve (user or group ID).

## Parameters

Expand Down Expand Up @@ -140,6 +141,21 @@ Accept pipeline input: True (ByPropertyName, ByValue)
Accept wildcard characters: False
```
### -SponsorId
Specifies the specific user's sponsor ID to retrieve (user or group ID).
```yaml
Type: System.String
Parameter Sets: (All)

Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName, ByValue)
Accept wildcard characters: False
```
### -Top
Specifies the maximum number of records to return.
Expand Down Expand Up @@ -182,4 +198,4 @@ This cmdlet supports the common parameters: `-Debug`, `-ErrorAction`, `-ErrorVar

## Notes

## Related Links
## Related Links
52 changes: 34 additions & 18 deletions module/docs/entra-powershell-v1.0/Users/Get-EntraUserSponsor.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,14 @@ In delegated scenarios with work or school accounts, the signed-in user needs a
```powershell
Connect-Entra -Scopes 'User.Read', 'User.Read.All' # User.Read.All is application-only permission (non-interactive login)
Get-EntraUserSponsor -UserId '[email protected]' |
Select-Object Id, displayName, userPrincipalName, createdDateTime, accountEnabled, userType |
Format-Table -AutoSize
Select-Object Id, DisplayName, '@odata.type', CreatedDateTime | Format-Table -AutoSize
```

```Output
id displayName userPrincipalName createdDateTime accountEnabled userType
-- ----------- ----------------- --------------- -------------- --------
c0c97c58-1895-4910-b1bb-58f96db771df Adele Vance [email protected] 28/10/2024 09:50:43 True Member
79984fce-9e33-497a-a5a2-b3c85e3fedcb Alex Wilber [email protected] 28/10/2024 09:50:46 True Member
406e3c9f-7a2d-4ef0-b1d4-69ddbd2719bb Diego Siciliani [email protected] 28/10/2024 09:50:46 True Member
id displayName @odata.type createdDateTime
-- ----------- ----------- ---------------
cccccccc-2222-3333-4444-dddddddddddd Angel Brown #microsoft.graph.user 3/7/2024 3:10:31 AM
eeeeeeee-4444-5555-6666-ffffffffffff Helpdesk Group #microsoft.graph.group 8/7/2024 2:52:47 PM
```

This example shows how to list user sponsors.
Expand All @@ -76,36 +74,39 @@ This example shows how to list user sponsors.

```powershell
Connect-Entra -Scopes 'User.Read', 'User.Read.All' # User.Read.All is application-only permission (non-interactive login)
Get-EntraUserSponsor -UserId '[email protected]' -Top 1 | Select-Object Id, DisplayName, '@odata.type'
Get-EntraUserSponsor -UserId '[email protected]' -Top 1 |
Select-Object Id, DisplayName, '@odata.type', CreatedDateTime | Format-Table -AutoSize
```

```Output
Id displayName @odata.type
-- ----------- -----------
cccccccc-2222-3333-4444-dddddddddddd Contoso Group #microsoft.graph.group
id displayName @odata.type createdDateTime
-- ----------- ----------- ---------------
cccccccc-2222-3333-4444-dddddddddddd Angel Brown #microsoft.graph.user 3/7/2024 3:10:31 AM
```

This example retrieves the top sponsor for the specified user.
This example retrieves the top sponsor for the specified user. You can use `-Limit` as an alias for `-Top`.

- The `-UserId` parameter specifies the User ID or User Principal Name.

### Example 3: Retrieve the assigned sponsor for a specific user by their SponsorId

```powershell
Connect-Entra -Scopes 'User.Read', 'User.Read.All' # User.Read.All is application-only permission (non-interactive login)
Get-EntraUserSponsor -UserId '[email protected]' -SponsorId cccccccc-2222-3333-4444-dddddddddddd | Select-Object Id, DisplayName, '@odata.type'
Get-EntraUserSponsor -UserId '[email protected]' -SponsorId 'cccccccc-2222-3333-4444-dddddddddddd' |
Select-Object Id, DisplayName, '@odata.type', CreatedDateTime | Format-Table -AutoSize
```

```Output
Id displayName @odata.type
-- ----------- -----------
cccccccc-2222-3333-4444-dddddddddddd Contoso Group #microsoft.graph.group
id displayName @odata.type createdDateTime
-- ----------- ----------- ---------------
cccccccc-2222-3333-4444-dddddddddddd Angel Brown #microsoft.graph.user 3/7/2024 3:10:31 AM
eeeeeeee-4444-5555-6666-ffffffffffff Helpdesk Group #microsoft.graph.group 8/7/2024 2:52:47 PM
```

This example retrieves the assigned sponsor for the specified user.

- The `-UserId` parameter specifies the User ID or User Principal Name.
- The `-SponsorId` parameter specifies the specific user's sponsor ID to retrieve.
- The `-SponsorId` parameter specifies the specific user's sponsor ID to retrieve (user or group ID).

## Parameters

Expand Down Expand Up @@ -140,6 +141,21 @@ Accept pipeline input: True (ByPropertyName, ByValue)
Accept wildcard characters: False
```
### -SponsorId
Specifies the specific user's sponsor ID to retrieve (user or group ID).
```yaml
Type: System.String
Parameter Sets: (All)

Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName, ByValue)
Accept wildcard characters: False
```
### -Top
Specifies the maximum number of records to return.
Expand Down Expand Up @@ -182,4 +198,4 @@ This cmdlet supports the common parameters: `-Debug`, `-ErrorAction`, `-ErrorVar

## Notes

## Related Links
## Related Links