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-EntraUserAdministrativeUnit #1240

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

[Parameter(ParameterSetName = "GetQuery", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Retrieve all user's administrative units.")]
[switch] $All,

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

[Alias('DirectoryObjectId')]
[Parameter(ParameterSetName = "GetById", Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Administrative Unit ID to retrieve.")]
[System.String] $AdministrativeUnitId,

[Alias('Limit')]
[Parameter(ParameterSetName = "GetQuery", 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["AdministrativeUnitId"]) {
$params["DirectoryObjectId"] = $PSBoundParameters["AdministrativeUnitId"]
}
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-MgUserMemberOfAsAdministrativeUnit @params -PageSize 999 -Headers $customHeaders
}
else {
$response = Get-MgUserMemberOfAsAdministrativeUnit @params -Headers $customHeaders
}

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

[Parameter(ParameterSetName = "GetQuery", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Retrieve all user's administrative units.")]
[switch] $All,

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

[Alias('DirectoryObjectId')]
[Parameter(ParameterSetName = "GetById", Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Administrative Unit ID to retrieve.")]
[System.String] $AdministrativeUnitId,

[Alias('Limit')]
[Parameter(ParameterSetName = "GetQuery", 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["AdministrativeUnitId"]) {
$params["DirectoryObjectId"] = $PSBoundParameters["AdministrativeUnitId"]
}
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-MgBetaUserMemberOfAsAdministrativeUnit @params -PageSize 999 -Headers $customHeaders
}
else {
$response = Get-MgBetaUserMemberOfAsAdministrativeUnit @params -Headers $customHeaders
}

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