Skip to content

Commit b497e53

Browse files
authored
Merge pull request #782 from sergiopadure/main
Added support for latest version of Azure Powershell
2 parents 78bc884 + 7b8450b commit b497e53

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

tools/scripts/New-ExtractorConfiguration.ps1

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ It requires at least one of SubscriptionId or SubscriptionName, along with ApimI
1010
.PARAMETER Environment
1111
The environment to use for the extractor configuration file. The default value is "AzureUSGovernment". Valid values are "AzureUSGovernment" and "AzureCloud"
1212
13+
.PARAMETER TenantId
14+
The tenant ID to use for the extractor configuration file. This is the ID of the tenant in the Azure portal. Required because otherwise the latest version of Azure Powershell starts trying to connect to all the tenants available to the user then bugs out.
15+
1316
.PARAMETER SubscriptionId
1417
The subscription ID to use for the extractor configuration file. This is the ID of the subscription in the Azure portal. This parameter is required if the SubscriptionName parameter is not specified.
1518
16-
.PARAMETER SubscriptionName
17-
The subscription name to use for the extractor configuration file. This is the name of the subscription in the Azure portal. This parameter is required if the SubscriptionId parameter is not specified.
18-
1919
.PARAMETER ApimInstanceName
2020
The name of the APIM instance to use for the extractor configuration file. This is the name of the APIM instance in the Azure portal.
2121
@@ -26,7 +26,7 @@ The name of the resource group that contains the APIM instance to use for the ex
2626
The name of the stage to use for the extractor configuration file. This is the name of the stage in the Azure portal.
2727
2828
.EXAMPLE
29-
. .\New-ExtractorConfiguration.ps1 -ApimInstanceName 'apim-instance' -ResourceGroupName 'rg-of-apiminstance' -Stage 'dev' -SubscriptionName 'subscription-of-apim'
29+
. .\New-ExtractorConfiguration.ps1 -TenantId 'tenant-id' -ApimInstanceName 'apim-instance' -ResourceGroupName 'rg-of-apiminstance' -Stage 'dev' -SubscriptionId 'subscriptionid-of-apim'
3030
3131
.NOTES
3232
Ensure you have the necessary Azure permissions to execute the script.
@@ -36,15 +36,15 @@ Ensure you have the necessary Azure permissions to execute the script.
3636
param(
3737
[string]
3838
[ValidateSet("AzureUSGovernment","AzureCloud")]
39-
$Environment = "AzureUsGovernment",
39+
$Environment = "AzureCloud",
4040

4141
[string]
42-
[Parameter(Mandatory, ParameterSetName = "SubscriptionId")]
43-
$SubscriptionId = '',
42+
[Parameter(Mandatory)]
43+
$TenantId = '',
4444

4545
[string]
46-
[Parameter(Mandatory, ParameterSetName = "SubscriptionName")]
47-
$SubscriptionName = '',
46+
[Parameter(Mandatory)]
47+
$SubscriptionId = '',
4848

4949
[string]
5050
[Parameter(Mandatory)]
@@ -63,14 +63,7 @@ param(
6363
$context = Get-AzContext
6464
if ($null -eq $Context)
6565
{
66-
Login-AzAccount -Environment $Environment
67-
}
68-
if ($SubscriptionId.Length -gt 0) {
69-
Set-AzContext -SubscriptionId $SubscriptionId
70-
}
71-
else
72-
{
73-
Set-AzContext -SubscriptionName $SubscriptionName
66+
Connect-AzAccount -Tenant $TenantId -Environment $Environment -Subscription $SubscriptionId
7467
}
7568

7669
# Encapsulate potential error throwing operations within a try-catch block
@@ -82,15 +75,21 @@ try {
8275
$products = Get-AzApiManagementProduct -Context $apim
8376
$backends = Get-AzApiManagementBackend -Context $apim
8477

85-
$uri = $Environment -eq "AzureUsGovernment" ? "management.usgovcloudapi.net" : "management.azure.com"
78+
$uri = switch ($Environment) {
79+
"AzureUsGovernment" { "https://management.usgovcloudapi.net" }
80+
"AzureCloud" { "https://management.azure.com" }
81+
}
82+
83+
$token = (ConvertFrom-SecureString (Get-AzAccessToken -ResourceUrl $uri).Token -AsPlainText)
8684
$headers = @{
87-
"Authorization" = "Bearer $((Get-AzAccessToken -ResourceUrl "https://$uri").Token)"
85+
"Authorization" = "Bearer $token"
8886
}
89-
90-
$tags = (Invoke-RestMethod -Method GET -Uri "https://$uri/subscriptions/$((Get-AzContext).Subscription.Id)/resourceGroups/$ResourceGroupName/providers/Microsoft.ApiManagement/service/$ApimInstanceName/tags?api-version=2022-08-01" -Headers $headers).value.name
91-
87+
$TagsUri = "$uri/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.ApiManagement/service/$ApimInstanceName/tags?api-version=2022-08-01"
88+
$TagsUri
89+
$tags = (Invoke-RestMethod -Method GET -Uri $TagsUri -Headers $headers).value.name
90+
9291
} catch {
93-
Write-Error "Error: $_"
92+
Write-Error "$_"
9493
exit 1
9594
}
9695
#endregion Azure Queries
@@ -153,3 +152,4 @@ Out-File -FilePath "autogen.configuration.extractor.$Stage.yaml" -InputObject $f
153152

154153
# Output completion message
155154
Write-Output "Done listing APIs, tags, loggers, namedValues, products, and backends"
155+
Write-Output "Done listing APIs, tags, loggers, namedValues, products, and backends"

0 commit comments

Comments
 (0)