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

[Modularize] Get-EntraUserRole #1274

Open
wants to merge 1 commit into
base: modularize
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
119 changes: 119 additions & 0 deletions module/Entra/Microsoft.Entra/Users/Get-EntraUserRole.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# ------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All Rights Reserved.
# Licensed under the MIT License. See License in the project root for license information.
# ------------------------------------------------------------------------------
function Get-EntraUserRole {
[CmdletBinding(DefaultParameterSetName = 'GetQuery')]
param (
[Parameter(ParameterSetName = "GetQuery", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Filter to apply to the query.")]
[System.String] $Filter,

[Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Retrieve all user roles.")]
[switch] $All,

[Parameter(ParameterSetName = "GetVague", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Search for user roles.")]
[System.String] $SearchString,

[Alias('ObjectId')]
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "User object ID to retrieve.")]
[System.String] $UserId,

[Alias('DirectoryRoleObjectId')]
[Parameter(Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Directory Role ID to retrieve.")]
[System.String] $DirectoryRoleId,

[Alias('Limit')]
[Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Maximum number of results to return.")]
[System.Nullable`1[System.Int32]] $Top,

[Alias('Select')]
[Parameter(Mandatory = $false, ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = "Properties to include in the results.")]
[System.String[]] $Property
)

PROCESS {
$params = @{}
$customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand
$keysChanged = @{ SearchString = "Filter" }

if ($null -ne $PSBoundParameters["ErrorAction"]) {
$params["ErrorAction"] = $PSBoundParameters["ErrorAction"]
}
if ($null -ne $PSBoundParameters["DirectoryRoleId"]) {
$params["DirectoryObjectId"] = $PSBoundParameters["DirectoryRoleId"]
}
if ($null -ne $PSBoundParameters["UserId"]) {
$params["UserId"] = $PSBoundParameters["UserId"]
}
if ($PSBoundParameters.ContainsKey("Verbose")) {
$params["Verbose"] = $PSBoundParameters["Verbose"]
}
if ($null -ne $PSBoundParameters["OutVariable"]) {
$params["OutVariable"] = $PSBoundParameters["OutVariable"]
}
if ($null -ne $PSBoundParameters["InformationAction"]) {
$params["InformationAction"] = $PSBoundParameters["InformationAction"]
}
if ($null -ne $PSBoundParameters["WarningVariable"]) {
$params["WarningVariable"] = $PSBoundParameters["WarningVariable"]
}
if ($PSBoundParameters.ContainsKey("Debug")) {
$params["Debug"] = $PSBoundParameters["Debug"]
}
if ($null -ne $PSBoundParameters["PipelineVariable"]) {
$params["PipelineVariable"] = $PSBoundParameters["PipelineVariable"]
}
if ($null -ne $PSBoundParameters["SearchString"]) {
$TmpValue = $PSBoundParameters["SearchString"]
$Value = "displayName eq '$TmpValue' or startsWith(displayName,'$TmpValue')"
$params["Filter"] = $Value
}
if ($null -ne $PSBoundParameters["ErrorVariable"]) {
$params["ErrorVariable"] = $PSBoundParameters["ErrorVariable"]
}
if ($null -ne $PSBoundParameters["Top"]) {
$params["Top"] = $PSBoundParameters["Top"]
}
if ($null -ne $PSBoundParameters["OutBuffer"]) {
$params["OutBuffer"] = $PSBoundParameters["OutBuffer"]
}
if ($null -ne $PSBoundParameters["All"]) {
if ($PSBoundParameters["All"]) {
$params["All"] = $PSBoundParameters["All"]
}
}
if ($null -ne $PSBoundParameters["WarningAction"]) {
$params["WarningAction"] = $PSBoundParameters["WarningAction"]
}
if ($null -ne $PSBoundParameters["ErrorVariable"]) {
$params["ErrorVariable"] = $PSBoundParameters["ErrorVariable"]
}
if ($null -ne $PSBoundParameters["InformationVariable"]) {
$params["InformationVariable"] = $PSBoundParameters["InformationVariable"]
}
if ($null -ne $PSBoundParameters["Property"]) {
$params["Property"] = $PSBoundParameters["Property"]
}

# Debug logging for transformations
Write-Debug "============================ TRANSFORMATIONS ============================"
$params.Keys | ForEach-Object { "$_ : $($params[$_])" } | Write-Debug
Write-Debug "=========================================================================`n"

try {
# Make the API call with -PageSize 999 if -All is used
if ($PSBoundParameters.ContainsKey("All") -and $All) {
$response = Get-MgUserMemberOfAsDirectoryRole @params -PageSize 999 -Headers $customHeaders
}
else {
$response = Get-MgUserMemberOfAsDirectoryRole @params -Headers $customHeaders
}

return $response
}
catch {
# Handle any errors that occur during the API call
Write-Error "An error occurred while retrieving the user roles: $_"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# ------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All Rights Reserved.
# Licensed under the MIT License. See License in the project root for license information.
# ------------------------------------------------------------------------------
function Get-EntraBetaUserRole {
[CmdletBinding(DefaultParameterSetName = 'GetQuery')]
param (
[Parameter(ParameterSetName = "GetQuery", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Filter to apply to the query.")]
[System.String] $Filter,

[Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Retrieve all user roles.")]
[switch] $All,

[Parameter(ParameterSetName = "GetVague", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Search for user roles.")]
[System.String] $SearchString,

[Alias('ObjectId')]
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "User object ID to retrieve.")]
[System.String] $UserId,

[Alias('DirectoryObjectId')]
[Parameter(Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Directory Role ID to retrieve.")]
[System.String] $DirectoryRoleId,

[Alias('Limit')]
[Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Maximum number of results to return.")]
[System.Nullable`1[System.Int32]] $Top,

[Alias('Select')]
[Parameter(Mandatory = $false, ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = "Properties to include in the results.")]
[System.String[]] $Property
)

PROCESS {
$params = @{}
$customHeaders = New-EntraBetaCustomHeaders -Command $MyInvocation.MyCommand
$keysChanged = @{ SearchString = "Filter" }

if ($null -ne $PSBoundParameters["ErrorAction"]) {
$params["ErrorAction"] = $PSBoundParameters["ErrorAction"]
}
if ($null -ne $PSBoundParameters["DirectoryRoleId"]) {
$params["DirectoryObjectId"] = $PSBoundParameters["DirectoryRoleId"]
}
if ($null -ne $PSBoundParameters["UserId"]) {
$params["UserId"] = $PSBoundParameters["UserId"]
}
if ($PSBoundParameters.ContainsKey("Verbose")) {
$params["Verbose"] = $PSBoundParameters["Verbose"]
}
if ($null -ne $PSBoundParameters["OutVariable"]) {
$params["OutVariable"] = $PSBoundParameters["OutVariable"]
}
if ($null -ne $PSBoundParameters["InformationAction"]) {
$params["InformationAction"] = $PSBoundParameters["InformationAction"]
}
if ($null -ne $PSBoundParameters["WarningVariable"]) {
$params["WarningVariable"] = $PSBoundParameters["WarningVariable"]
}
if ($PSBoundParameters.ContainsKey("Debug")) {
$params["Debug"] = $PSBoundParameters["Debug"]
}
if ($null -ne $PSBoundParameters["PipelineVariable"]) {
$params["PipelineVariable"] = $PSBoundParameters["PipelineVariable"]
}
if ($null -ne $PSBoundParameters["SearchString"]) {
$TmpValue = $PSBoundParameters["SearchString"]
$Value = "displayName eq '$TmpValue' or startsWith(displayName,'$TmpValue')"
$params["Filter"] = $Value
}
if ($null -ne $PSBoundParameters["ErrorVariable"]) {
$params["ErrorVariable"] = $PSBoundParameters["ErrorVariable"]
}
if ($null -ne $PSBoundParameters["Top"]) {
$params["Top"] = $PSBoundParameters["Top"]
}
if ($null -ne $PSBoundParameters["OutBuffer"]) {
$params["OutBuffer"] = $PSBoundParameters["OutBuffer"]
}
if ($null -ne $PSBoundParameters["All"]) {
if ($PSBoundParameters["All"]) {
$params["All"] = $PSBoundParameters["All"]
}
}
if ($null -ne $PSBoundParameters["WarningAction"]) {
$params["WarningAction"] = $PSBoundParameters["WarningAction"]
}
if ($null -ne $PSBoundParameters["ErrorVariable"]) {
$params["ErrorVariable"] = $PSBoundParameters["ErrorVariable"]
}
if ($null -ne $PSBoundParameters["InformationVariable"]) {
$params["InformationVariable"] = $PSBoundParameters["InformationVariable"]
}
if ($null -ne $PSBoundParameters["Property"]) {
$params["Property"] = $PSBoundParameters["Property"]
}

# Debug logging for transformations
Write-Debug "============================ TRANSFORMATIONS ============================"
$params.Keys | ForEach-Object { "$_ : $($params[$_])" } | Write-Debug
Write-Debug "=========================================================================`n"

try {
# Make the API call with -PageSize 999 if -All is used
if ($PSBoundParameters.ContainsKey("All") -and $All) {
$response = Get-MgBetaUserMemberOfAsDirectoryRole @params -PageSize 999 -Headers $customHeaders
}
else {
$response = Get-MgBetaUserMemberOfAsDirectoryRole @params -Headers $customHeaders
}

return $response
}
catch {
# Handle any errors that occur during the API call
Write-Error "An error occurred while retrieving the user roles: $_"
}
}
}
Loading