Skip to content

Commit

Permalink
Merge pull request #30 from bgelens/patch-1
Browse files Browse the repository at this point in the history
Update Get-AzDelegatedSubscription.ps1
  • Loading branch information
krnese committed Aug 28, 2019
2 parents 6f93432 + d741b41 commit 4417964
Showing 1 changed file with 15 additions and 30 deletions.
@@ -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 }
}

0 comments on commit 4417964

Please sign in to comment.