Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from bgelens/patch-1
Update Get-AzDelegatedSubscription.ps1
- Loading branch information
Showing
1 changed file
with
15 additions
and
30 deletions.
There are no files selected for viewing
45 changes: 15 additions & 30 deletions
45
...ted-Resource-Management/tools/get-azdelegatedsubscription/Get-AzDelegatedSubscription.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,28 @@ | ||
function Get-AzDelegatedSubscription { | ||
[cmdletbinding()] | ||
param () | ||
begin { | ||
# Getting Azure connection context for the signed in user | ||
$currentContext = Get-AzContext | ||
$token = $currentContext.TokenCache.ReadItems() | ? {$_.tenantid -eq $currentContext.Tenant.Id} | ||
} | ||
process { | ||
# Getting Home Tenant Id | ||
$getTenant = @{ | ||
Uri = "https://management.azure.com/tenants?api-version=2019-07-01" | ||
Headers = @{ | ||
Authorization = "Bearer $($token.AccessToken)" | ||
"Content-Type" = "application/json" | ||
} | ||
Method = "GET" | ||
} | ||
$tenant = Invoke-WebRequest @getTenant | ||
$tenantContent = ConvertFrom-Json -InputObject $tenant.content | ||
$tenantId = $tenantContent.value.id.Split('/')[2] | ||
|
||
# Getting Azure connection context for the signed in user | ||
$currentContext = Get-AzContext | ||
|
||
# fetching new token | ||
$azureRmProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile | ||
$profileClient = [Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient]::new($azureRmProfile) | ||
$token = $profileClient.AcquireAccessToken($currentContext.Subscription.TenantId) | ||
|
||
# Listing All Subscriptions to grab their tenantId's | ||
$listSubscriptions = @{ | ||
Uri = "https://management.azure.com/subscriptions?api-version=2019-07-01" | ||
Uri = 'https://management.azure.com/subscriptions?api-version=2019-07-01' | ||
Headers = @{ | ||
Authorization = "Bearer $($token.AccessToken)" | ||
"Content-Type" = "application/json" | ||
'Content-Type' = 'application/json' | ||
} | ||
Method = "GET" | ||
} | ||
$list = Invoke-WebRequest @listSubscriptions | ||
$output = ConvertFrom-Json -InputObject $list.Content | ||
Method = 'GET' | ||
} | ||
$list = Invoke-RestMethod @listSubscriptions | ||
|
||
end { | ||
# Filtering the output to only show the delegated subscriptions | ||
Write-Host "Preparing list of delegated subscriptions..." | ||
$delegatedSubsValue = $output.value | ||
$delegatedSubs = $delegatedSubsValue | where-object {$_.tenantId -notmatch $tenantid} | ||
$delegatedSubs | ||
} | ||
} | ||
$delegatedSubsValue = $list.value | ||
$delegatedSubsValue | Where-Object -FilterScript { $_.tenantId -ne $token.TenantId } | ||
} |