Skip to content

Commit bbf0c4e

Browse files
Get-EntraSubscription (#1395)
* Initial commit * Initial commit - directory subscription * Add alias * Pipeline param fix * Added unit test * Add test for id filtering * Fix test error * Remove redundant code * Fix test running error. * Add docs * Update related links section * Add beta command logic, docs and unit tests * Nit: Fix beta naming * Removing extra alias
1 parent 7644d0f commit bbf0c4e

File tree

8 files changed

+920
-5
lines changed

8 files changed

+920
-5
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# ------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All Rights Reserved.
3+
# Licensed under the MIT License. See License in the project root for license information.
4+
# ------------------------------------------------------------------------------
5+
function Get-EntraSubscription {
6+
[CmdletBinding(DefaultParameterSetName = 'GetQuery')]
7+
param (
8+
[Parameter(ParameterSetName = "GetById", Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Specifies the unique object ID of the subscription to retrieve.")]
9+
[Alias('SubscriptionId')]
10+
[System.String] $CommerceSubscriptionId,
11+
12+
[Parameter(ParameterSetName = "GetQuery", ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $false, HelpMessage = "Specifies the number of objects to return.")]
13+
[Alias("Limit")]
14+
[System.Int32] $Top,
15+
16+
[Parameter(ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $false, HelpMessage = "Specifies whether to return all objects.")]
17+
[switch] $All,
18+
19+
[Parameter(ParameterSetName = "GetQuery", ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $false, HelpMessage = "Filter the results based on the specified criteria.")]
20+
[System.String] $Filter,
21+
22+
[Parameter(Mandatory = $false, ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $false, HelpMessage = "Specifies the properties to include in the response.")]
23+
[Alias("Select")]
24+
[System.String[]] $Property
25+
)
26+
27+
PROCESS {
28+
$params = @{}
29+
$topCount = 0
30+
$customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand
31+
$baseUri = "https://graph.microsoft.com/v1.0/directory/subscriptions"
32+
$properties = "`$select=*"
33+
34+
if ($PSBoundParameters.ContainsKey("Property")) {
35+
$selectProperties = $Property -join ','
36+
$properties = "`$select=$selectProperties"
37+
}
38+
39+
if ($PSBoundParameters.ContainsKey("CommerceSubscriptionId")) {
40+
$commerceSubscriptionId = $PSBoundParameters["CommerceSubscriptionId"]
41+
$params["Uri"] = "${baseUri}/${commerceSubscriptionId}?$properties"
42+
}
43+
else {
44+
$params["Uri"] = "${baseUri}?$properties"
45+
46+
if ($PSBoundParameters.ContainsKey("Top")) {
47+
$topCount = $Top
48+
$params["Uri"] += if ($topCount -gt 999) { "&`$top=999" } else { "&`$top=$topCount" }
49+
}
50+
51+
if ($PSBoundParameters.ContainsKey("Filter")) {
52+
$params["Uri"] += "&`$filter=$Filter"
53+
}
54+
}
55+
56+
Write-Debug("============================ TRANSFORMATIONS ============================")
57+
$params.Keys | ForEach-Object { "$_ : $($params[$_])" } | Write-Debug
58+
Write-Debug("=========================================================================`n")
59+
60+
$response = Invoke-GraphRequest -Headers $customHeaders -Uri $params["Uri"] -Method GET
61+
62+
try {
63+
if ($PSBoundParameters.ContainsKey("CommerceSubscriptionId")) {
64+
$data = @($response)
65+
}
66+
else {
67+
$data = $response.value
68+
$all = $All.IsPresent
69+
$increment = $topCount - ($data.Count)
70+
71+
while ($response.PSObject.Properties["`@odata.nextLink"] -and (($all -and $increment -lt 0) -or $increment -gt 0)) {
72+
$params["Uri"] = $response.'@odata.nextLink'
73+
if ($increment -gt 0) {
74+
$topValue = [Math]::Min($increment, 999)
75+
$params["Uri"] = $params["Uri"].Replace('`$top=999', "`$top=$topValue")
76+
$increment -= $topValue
77+
}
78+
$response = Invoke-GraphRequest @params
79+
$data += $response.value
80+
}
81+
}
82+
}
83+
catch {
84+
Write-Error "An error occurred while retrieving data: $_"
85+
}
86+
87+
if ($Property -and $Property.Count -gt 0) {
88+
$data | Select-Object -Property $Property
89+
}
90+
else {
91+
$data | Select-Object -Property *
92+
}
93+
}
94+
}
95+
Set-Alias -Name Get-EntraDirectorySubscription -Value Get-EntraSubscription -Description "Retrieves the organization's commercial subscriptions." -Scope Global -Force
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# ------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All Rights Reserved.
3+
# Licensed under the MIT License. See License in the project root for license information.
4+
# ------------------------------------------------------------------------------
5+
function Get-EntraBetaSubscription {
6+
[CmdletBinding(DefaultParameterSetName = 'GetQuery')]
7+
param (
8+
[Parameter(ParameterSetName = "GetById", Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Specifies the unique object ID of the subscription to retrieve.")]
9+
[Alias('SubscriptionId')]
10+
[System.String] $CommerceSubscriptionId,
11+
12+
[Parameter(ParameterSetName = "GetQuery", ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $false, HelpMessage = "Specifies the number of objects to return.")]
13+
[Alias("Limit")]
14+
[System.Int32] $Top,
15+
16+
[Parameter(ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $false, HelpMessage = "Specifies whether to return all objects.")]
17+
[switch] $All,
18+
19+
[Parameter(ParameterSetName = "GetQuery", ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $false, HelpMessage = "Filter the results based on the specified criteria.")]
20+
[System.String] $Filter,
21+
22+
[Parameter(Mandatory = $false, ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $false, HelpMessage = "Specifies the properties to include in the response.")]
23+
[Alias("Select")]
24+
[System.String[]] $Property
25+
)
26+
27+
PROCESS {
28+
$params = @{}
29+
$topCount = 0
30+
$customHeaders = New-EntraBetaCustomHeaders -Command $MyInvocation.MyCommand
31+
$baseUri = "https://graph.microsoft.com/beta/directory/subscriptions"
32+
$properties = "`$select=*"
33+
34+
if ($PSBoundParameters.ContainsKey("Property")) {
35+
$selectProperties = $Property -join ','
36+
$properties = "`$select=$selectProperties"
37+
}
38+
39+
if ($PSBoundParameters.ContainsKey("CommerceSubscriptionId")) {
40+
$commerceSubscriptionId = $PSBoundParameters["CommerceSubscriptionId"]
41+
$params["Uri"] = "${baseUri}/${commerceSubscriptionId}?$properties"
42+
}
43+
else {
44+
$params["Uri"] = "${baseUri}?$properties"
45+
46+
if ($PSBoundParameters.ContainsKey("Top")) {
47+
$topCount = $Top
48+
$params["Uri"] += if ($topCount -gt 999) { "&`$top=999" } else { "&`$top=$topCount" }
49+
}
50+
51+
if ($PSBoundParameters.ContainsKey("Filter")) {
52+
$params["Uri"] += "&`$filter=$Filter"
53+
}
54+
}
55+
56+
Write-Debug("============================ TRANSFORMATIONS ============================")
57+
$params.Keys | ForEach-Object { "$_ : $($params[$_])" } | Write-Debug
58+
Write-Debug("=========================================================================`n")
59+
60+
$response = Invoke-GraphRequest -Headers $customHeaders -Uri $params["Uri"] -Method GET
61+
62+
try {
63+
if ($PSBoundParameters.ContainsKey("CommerceSubscriptionId")) {
64+
$data = @($response)
65+
}
66+
else {
67+
$data = $response.value
68+
$all = $All.IsPresent
69+
$increment = $topCount - ($data.Count)
70+
71+
while ($response.PSObject.Properties["`@odata.nextLink"] -and (($all -and $increment -lt 0) -or $increment -gt 0)) {
72+
$params["Uri"] = $response.'@odata.nextLink'
73+
if ($increment -gt 0) {
74+
$topValue = [Math]::Min($increment, 999)
75+
$params["Uri"] = $params["Uri"].Replace('`$top=999', "`$top=$topValue")
76+
$increment -= $topValue
77+
}
78+
$response = Invoke-GraphRequest @params
79+
$data += $response.value
80+
}
81+
}
82+
}
83+
catch {
84+
Write-Error "An error occurred while retrieving data: $_"
85+
}
86+
87+
if ($Property -and $Property.Count -gt 0) {
88+
$data | Select-Object -Property $Property
89+
}
90+
else {
91+
$data | Select-Object -Property *
92+
}
93+
}
94+
}
95+
Set-Alias -Name Get-EntraBetaDirectorySubscription -Value Get-EntraBetaSubscription -Description "Retrieves the organization's commercial subscriptions." -Scope Global -Force

module/docs/entra-powershell-beta/DirectoryManagement/Get-EntraBetaSubscribedSku.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
title: Get-EntraBetaSubscribedSku
33
description: This article provides details on the Get-EntraBetaSubscribedSku command.
44

5-
65
ms.topic: reference
76
ms.date: 08/13/2024
87
ms.author: eunicewaweru
@@ -48,8 +47,8 @@ The `Get-EntraBetaSubscribedSku` cmdlet gets subscribed SKUs to Microsoft servic
4847

4948
In delegated scenarios with work or school accounts, when acting on another user, the signed-in user must have a supported Microsoft Entra role or a custom role with the necessary permissions. The following least privileged roles support this operation:
5049

51-
- Dynamics 365 Business Central Administrator (read-only access to standard properties)
52-
- Global Reader
50+
- Dynamics 365 Business Central Administrator (read-only access to standard properties)
51+
- Global Reader
5352
- Directory Readers
5453

5554
## Examples
@@ -235,3 +234,5 @@ This cmdlet supports the common parameters: `-Debug`, `-ErrorAction`, `-ErrorVar
235234
## Notes
236235

237236
## Related Links
237+
238+
[Get-EntraBetaSubscription](Get-EntraBetaSubscription.md)

0 commit comments

Comments
 (0)