diff --git a/module/Entra/AdditionalFunctions/Get-EntraUserAdministrativeUnit.ps1 b/module/Entra/AdditionalFunctions/Get-EntraUserAdministrativeUnit.ps1 new file mode 100644 index 0000000000..31fe928646 --- /dev/null +++ b/module/Entra/AdditionalFunctions/Get-EntraUserAdministrativeUnit.ps1 @@ -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: $_" + } + } +} \ No newline at end of file diff --git a/module/EntraBeta/AdditionalFunctions/Get-EntraBetaUserAdministrativeUnit.ps1 b/module/EntraBeta/AdditionalFunctions/Get-EntraBetaUserAdministrativeUnit.ps1 new file mode 100644 index 0000000000..17b87cfc20 --- /dev/null +++ b/module/EntraBeta/AdditionalFunctions/Get-EntraBetaUserAdministrativeUnit.ps1 @@ -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: $_" + } + } +} \ No newline at end of file diff --git a/module/docs/entra-powershell-beta/Microsoft.Graph.Entra.Beta/Get-EntraBetaUserAdministrativeUnit.md b/module/docs/entra-powershell-beta/Microsoft.Graph.Entra.Beta/Get-EntraBetaUserAdministrativeUnit.md new file mode 100644 index 0000000000..11e9733aaa --- /dev/null +++ b/module/docs/entra-powershell-beta/Microsoft.Graph.Entra.Beta/Get-EntraBetaUserAdministrativeUnit.md @@ -0,0 +1,200 @@ +--- +title: Get-EntraBetaUserAdministrativeUnit +description: This article provides details on the Get-EntraBetaUserAdministrativeUnit command. + + +ms.topic: reference +ms.date: 12/02/2024 +ms.author: eunicewaweru +ms.reviewer: stevemutungi +manager: CelesteDG + +external help file: Microsoft.Graph.Entra.Beta-Help.xml +Module Name: Microsoft.Graph.Entra.Beta +online version: https://learn.microsoft.com/powershell/module/Microsoft.Graph.Entra.Beta/Get-EntraBetaUserAdministrativeUnit + +schema: 2.0.0 +--- + +# Get-EntraBetaUserAdministrativeUnit + +## Synopsis + +Retrieves the list of administrative units a user belongs to. + +## Syntax + +### GetQuery (Default) + +```powershell +Get-EntraBetaUserAdministrativeUnit + -UserId + [-All] + [-Top ] + [-Property ] + [] +``` + +### GetById + +```powershell +Get-EntraBetaUserAdministrativeUnit + -UserId + -AdministrativeUnitId + [-Property ] + [] +``` + +## Description + +The `Get-EntraBetaUserAdministrativeUnit` cmdlet retrieves a list of administrative units to which a user belongs. + +## Examples + +### Example 1: Get a list of administrative units to which a specific user belongs + +```powershell +Connect-Entra -Scopes 'AdministrativeUnit.Read.All' +Get-EntraBetaUserAdministrativeUnit -UserId 'SawyerM@contoso.com' +``` + +```Output +DeletedDateTime Id Description DisplayName Visibility +--------------- -- ----------- ----------- ---------- + dddddddd-3333-4444-5555-eeeeeeeeeeee Pacific Administrative Unit Pacific Admin Unit + aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb Engineering Administrative Unit Engineering Admin Unit +``` + +This cmdlet retrieves a list of administrative units to which a specific user belongs. + +### Example 2: Get a list of administrative units to which a specific user belongs using the All parameter + +```powershell +Connect-Entra -Scopes 'AdministrativeUnit.Read.All' +Get-EntraBetaUserAdministrativeUnit -UserId 'SawyerM@contoso.com' -All +``` + +```Output +DeletedDateTime Id Description DisplayName Visibility +--------------- -- ----------- ----------- ---------- + dddddddd-3333-4444-5555-eeeeeeeeeeee Pacific Administrative Unit Pacific Admin Unit + aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb Engineering Administrative Unit Engineering Admin Unit +``` + +This cmdlet retrieves a list of administrative units to which a specific user belongs using the All parameter. + +### Example 3: Get an administrative unit to which a specific user belongs + +```powershell +Connect-Entra -Scopes 'AdministrativeUnit.Read.All' +Get-EntraBetaUserAdministrativeUnit -UserId 'SawyerM@contoso.com' -Top 1 +``` + +```Output +DeletedDateTime Id Description DisplayName Visibility +--------------- -- ----------- ----------- ---------- + dddddddd-3333-4444-5555-eeeeeeeeeeee Pacific Administrative Unit Pacific Admin Unit + aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb Engineering Administrative Unit Engineering Admin Unit +``` + +This cmdlet retrieves an administrative unit to which a specific user belongs. + +### Example 4: Get a list of administrative units to which a specific user belongs using the Administrative Unit ID parameter + +```powershell +Connect-Entra -Scopes 'AdministrativeUnit.Read.All' +$administrativeUnit = Get-EntraBetaAdministrativeUnit -Filter "DisplayName eq 'Pacific Admin Unit'" +Get-EntraBetaUserAdministrativeUnit -UserId 'SawyerM@contoso.com' -AdministrativeUnitId $administrativeUnit.Id +``` + +```Output +DeletedDateTime Id Description DisplayName Visibility +--------------- -- ----------- ----------- ---------- + dddddddd-3333-4444-5555-eeeeeeeeeeee Pacific Administrative Unit Pacific Admin Unit +``` + +This cmdlet retrieves a list of administrative units to which a specific user belongs using the Administrative Unit ID parameter. + +- `-AdministrativeUnitId` parameter specifies the administrative unit ID. + +## Parameters + +### -All + +List all pages. + +```yaml +Type: System.Management.Automation.SwitchParameter +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: False +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -Top + +The maximum number of administrative units a user belongs to. + +```yaml +Type: System.Int32 +Parameter Sets: GetQuery +Aliases: Limit + +Required: False +Position: Named +Default value: None +Accept pipeline input: True (ByPropertyName, ByValue) +Accept wildcard characters: False +``` + +### -Property + +Specifies properties to be returned + +```yaml +Type: System.String[] +Parameter Sets: (All) +Aliases: Select + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -AdministrativeUnitId + +The unique ID of the administrative unit. + +```yaml +Type: System.String +Parameter Sets: GetQuery +Aliases: DirectoryObjectId + +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 + +### System.Object + +## Notes + +## Related Links + +[Get-EntraBetaUserMembership](Get-EntraBetaUserMembership.md) diff --git a/module/docs/entra-powershell-v1.0/Microsoft.Graph.Entra/Get-EntraUserAdministrativeUnit.md b/module/docs/entra-powershell-v1.0/Microsoft.Graph.Entra/Get-EntraUserAdministrativeUnit.md new file mode 100644 index 0000000000..e4de9f931c --- /dev/null +++ b/module/docs/entra-powershell-v1.0/Microsoft.Graph.Entra/Get-EntraUserAdministrativeUnit.md @@ -0,0 +1,200 @@ +--- +title: Get-EntraUserAdministrativeUnit +description: This article provides details on the Get-EntraUserAdministrativeUnit command. + + +ms.topic: reference +ms.date: 12/02/2024 +ms.author: eunicewaweru +ms.reviewer: stevemutungi +manager: CelesteDG + +external help file: Microsoft.Graph.Entra-Help.xml +Module Name: Microsoft.Graph.Entra +online version: https://learn.microsoft.com/powershell/module/Microsoft.Graph.Entra/Get-EntraUserAdministrativeUnit + +schema: 2.0.0 +--- + +# Get-EntraUserAdministrativeUnit + +## Synopsis + +Retrieves the list of administrative units a user belongs to. + +## Syntax + +### GetQuery (Default) + +```powershell +Get-EntraUserAdministrativeUnit + -UserId + [-All] + [-Top ] + [-Property ] + [] +``` + +### GetById + +```powershell +Get-EntraUserAdministrativeUnit + -UserId + -AdministrativeUnitId + [-Property ] + [] +``` + +## Description + +The `Get-EntraUserAdministrativeUnit` cmdlet retrieves a list of administrative units to which a user belongs. + +## Examples + +### Example 1: Get a list of administrative units to which a specific user belongs + +```powershell +Connect-Entra -Scopes 'AdministrativeUnit.Read.All' +Get-EntraUserAdministrativeUnit -UserId 'SawyerM@contoso.com' +``` + +```Output +DeletedDateTime Id Description DisplayName Visibility +--------------- -- ----------- ----------- ---------- + dddddddd-3333-4444-5555-eeeeeeeeeeee Pacific Administrative Unit Pacific Admin Unit + aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb Engineering Administrative Unit Engineering Admin Unit +``` + +This cmdlet retrieves a list of administrative units to which a specific user belongs. + +### Example 2: Get a list of administrative units to which a specific user belongs using the All parameter + +```powershell +Connect-Entra -Scopes 'AdministrativeUnit.Read.All' +Get-EntraUserAdministrativeUnit -UserId 'SawyerM@contoso.com' -All +``` + +```Output +DeletedDateTime Id Description DisplayName Visibility +--------------- -- ----------- ----------- ---------- + dddddddd-3333-4444-5555-eeeeeeeeeeee Pacific Administrative Unit Pacific Admin Unit + aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb Engineering Administrative Unit Engineering Admin Unit +``` + +This cmdlet retrieves a list of administrative units to which a specific user belongs using the All parameter. + +### Example 3: Get an administrative unit to which a specific user belongs + +```powershell +Connect-Entra -Scopes 'AdministrativeUnit.Read.All' +Get-EntraUserAdministrativeUnit -UserId 'SawyerM@contoso.com' -Top 1 +``` + +```Output +DeletedDateTime Id Description DisplayName Visibility +--------------- -- ----------- ----------- ---------- + dddddddd-3333-4444-5555-eeeeeeeeeeee Pacific Administrative Unit Pacific Admin Unit + aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb Engineering Administrative Unit Engineering Admin Unit +``` + +This cmdlet retrieves an administrative unit to which a specific user belongs. + +### Example 4: Get a list of administrative units to which a specific user belongs using the Administrative Unit ID parameter + +```powershell +Connect-Entra -Scopes 'AdministrativeUnit.Read.All' +$administrativeUnit = Get-EntraAdministrativeUnit -Filter "DisplayName eq 'Pacific Admin Unit'" +Get-EntraUserAdministrativeUnit -UserId 'SawyerM@contoso.com' -AdministrativeUnitId $administrativeUnit.Id +``` + +```Output +DeletedDateTime Id Description DisplayName Visibility +--------------- -- ----------- ----------- ---------- + dddddddd-3333-4444-5555-eeeeeeeeeeee Pacific Administrative Unit Pacific Admin Unit +``` + +This cmdlet retrieves a list of administrative units to which a specific user belongs using the Administrative Unit ID parameter. + +- `-AdministrativeUnitId` parameter specifies the administrative unit ID. + +## Parameters + +### -All + +List all pages. + +```yaml +Type: System.Management.Automation.SwitchParameter +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: False +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -Top + +The maximum number of administrative units a user belongs to. + +```yaml +Type: System.Int32 +Parameter Sets: GetQuery +Aliases: Limit + +Required: False +Position: Named +Default value: None +Accept pipeline input: True (ByPropertyName, ByValue) +Accept wildcard characters: False +``` + +### -Property + +Specifies properties to be returned + +```yaml +Type: System.String[] +Parameter Sets: (All) +Aliases: Select + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -AdministrativeUnitId + +The unique ID of the administrative unit. + +```yaml +Type: System.String +Parameter Sets: GetQuery +Aliases: DirectoryObjectId + +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 + +### System.Object + +## Notes + +## Related Links + +[Get-EntraUserMembership](Get-EntraUserMembership.md) diff --git a/test/module/Entra/Get-EntraUserAdministrativeUnit.Tests.ps1 b/test/module/Entra/Get-EntraUserAdministrativeUnit.Tests.ps1 new file mode 100644 index 0000000000..634a88257d --- /dev/null +++ b/test/module/Entra/Get-EntraUserAdministrativeUnit.Tests.ps1 @@ -0,0 +1,79 @@ +# ------------------------------------------------------------------------------ +# Copyright (c) Microsoft Corporation. All Rights Reserved. +# Licensed under the MIT License. See License in the project root for license information. +# ------------------------------------------------------------------------------ + +BeforeAll { + if ((Get-Module -Name Microsoft.Graph.Entra) -eq $null) { + Import-Module Microsoft.Graph.Entra + } + Import-Module (Join-Path $psscriptroot "..\Common-Functions.ps1") -Force + + $scriptblock = { + return @( + [PSCustomObject]@{ + "DisplayName" = "Pacific Admin Unit" + "Id" = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + "Description" = "Pacific Admin Unit" + "Members" = $null + "DeletedDateTime" = $null + "Visibility" = $null + } + ) + } + + Mock -CommandName Get-MgUserMemberOfAsAdministrativeUnit -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} + +Describe "Get-EntraUserAdministrativeUnit" { + Context "Test for Get-EntraUserAdministrativeUnit" { + It "Should return all user roles" { + $result = Get-EntraUserAdministrativeUnit -UserId 'SawyerM@contoso.com' + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgUserMemberOfAsAdministrativeUnit -ModuleName Microsoft.Graph.Entra -Times 1 + } + + It "Should return top user role" { + $result = Get-EntraUserAdministrativeUnit -UserId 'SawyerM@contoso.com' -Top 1 + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgUserMemberOfAsAdministrativeUnit -ModuleName Microsoft.Graph.Entra -Times 1 + } + + It "Property parameter should work" { + $result = Get-EntraUserAdministrativeUnit -UserId 'SawyerM@contoso.com' -Property "DisplayName" + $result | Should -Not -BeNullOrEmpty + $result.DisplayName | Should -Be "Pacific Admin Unit" + Should -Invoke -CommandName Get-MgUserMemberOfAsAdministrativeUnit -ModuleName Microsoft.Graph.Entra -Times 1 + } + + It "Should fail when Property is empty" { + { Get-EntraUserAdministrativeUnit -UserId 'SawyerM@contoso.com' -Property } | Should -Throw "Missing an argument for parameter 'Property'*" + } + + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraUserAdministrativeUnit" + $result = Get-EntraUserAdministrativeUnit -UserId 'SawyerM@contoso.com' + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraUserAdministrativeUnit" + Should -Invoke -CommandName Get-MgUserMemberOfAsAdministrativeUnit -ModuleName Microsoft.Graph.Entra -Times 1 -ParameterFilter { + $Headers.'User-Agent' | Should -Be $userAgentHeaderValue + $true + } + } + + It "Should execute successfully without throwing an error" { + # Disable confirmation prompts + $originalDebugPreference = $DebugPreference + $DebugPreference = 'Continue' + + try { + # Act & Assert: Ensure the function doesn't throw an exception + { Get-EntraUserAdministrativeUnit -UserId 'SawyerM@contoso.com' -Debug } | Should -Not -Throw + } + finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } + } +} \ No newline at end of file diff --git a/test/module/EntraBeta/Get-EntraBetaUserAdministrativeUnit.Tests.ps1 b/test/module/EntraBeta/Get-EntraBetaUserAdministrativeUnit.Tests.ps1 new file mode 100644 index 0000000000..2590bddc46 --- /dev/null +++ b/test/module/EntraBeta/Get-EntraBetaUserAdministrativeUnit.Tests.ps1 @@ -0,0 +1,79 @@ +# ------------------------------------------------------------------------------ +# Copyright (c) Microsoft Corporation. All Rights Reserved. +# Licensed under the MIT License. See License in the project root for license information. +# ------------------------------------------------------------------------------ + +BeforeAll { + if ((Get-Module -Name Microsoft.Graph.Entra.Beta) -eq $null) { + Import-Module Microsoft.Graph.Entra.Beta + } + Import-Module (Join-Path $psscriptroot "..\Common-Functions.ps1") -Force + + $scriptblock = { + return @( + [PSCustomObject]@{ + "DisplayName" = "Pacific Admin Unit" + "Id" = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + "Description" = "Pacific Admin Unit" + "Members" = $null + "DeletedDateTime" = $null + "Visibility" = $null + } + ) + } + + Mock -CommandName Get-MgBetaUserMemberOfAsAdministrativeUnit -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra.Beta +} + +Describe "Get-EntraBetaUserAdministrativeUnit" { + Context "Test for Get-EntraBetaUserAdministrativeUnit" { + It "Should return all user roles" { + $result = Get-EntraBetaUserAdministrativeUnit -UserId 'SawyerM@contoso.com' + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgBetaUserMemberOfAsAdministrativeUnit -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + + It "Should return top user role" { + $result = Get-EntraBetaUserAdministrativeUnit -UserId 'SawyerM@contoso.com' -Top 1 + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgBetaUserMemberOfAsAdministrativeUnit -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + + It "Property parameter should work" { + $result = Get-EntraBetaUserAdministrativeUnit -UserId 'SawyerM@contoso.com' -Property "DisplayName" + $result | Should -Not -BeNullOrEmpty + $result.DisplayName | Should -Be "Pacific Admin Unit" + Should -Invoke -CommandName Get-MgBetaUserMemberOfAsAdministrativeUnit -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + + It "Should fail when Property is empty" { + { Get-EntraBetaUserAdministrativeUnit -UserId 'SawyerM@contoso.com' -Property } | Should -Throw "Missing an argument for parameter 'Property'*" + } + + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraBetaUserAdministrativeUnit" + $result = Get-EntraBetaUserAdministrativeUnit -UserId 'SawyerM@contoso.com' + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraBetaUserAdministrativeUnit" + Should -Invoke -CommandName Get-MgBetaUserMemberOfAsAdministrativeUnit -ModuleName Microsoft.Graph.Entra.Beta -Times 1 -ParameterFilter { + $Headers.'User-Agent' | Should -Be $userAgentHeaderValue + $true + } + } + + It "Should execute successfully without throwing an error" { + # Disable confirmation prompts + $originalDebugPreference = $DebugPreference + $DebugPreference = 'Continue' + + try { + # Act & Assert: Ensure the function doesn't throw an exception + { Get-EntraBetaUserAdministrativeUnit -UserId 'SawyerM@contoso.com' -Debug } | Should -Not -Throw + } + finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } + } +} \ No newline at end of file