Skip to content

Instantly share code, notes, and snippets.

@ljtill
Last active August 1, 2022 13:17
Show Gist options
  • Save ljtill/b82d58f523eefc14739582d13f84ce5e to your computer and use it in GitHub Desktop.
Save ljtill/b82d58f523eefc14739582d13f84ce5e to your computer and use it in GitHub Desktop.
Provides the ability to thread the operations of retrieving Policy artefacts
Get-Job | Remove-Job
Import-Module -Name Az.Accounts
$profile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
$client = New-Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient($profile)
$token = $client.AcquireAccessToken("")
$accessToken = $token.AccessToken
$params = @{
Method = "Get"
Uri = ("https://management.azure.com/providers/Microsoft.Management/managementGroups?api-version=2020-05-01")
Headers = @{
"Authorization" = "Bearer $accessToken"
}
Body = $null
}
$response = Invoke-RestMethod @params
$response.value | ForEach-Object {
$managementGroup = $_
# Policy Assignments
Start-ThreadJob -Name "0-$($managementGroup.Name)" -ScriptBlock {
param ($managementGroup, $accessToken)
$params = @{
Method = "Get"
Uri = ("https://management.azure.com" + $managementGroup.Id + '/providers/Microsoft.Authorization/policyAssignments?$filter=atScope()&api-version=2019-09-01')
Headers = @{
"Authorization" = "Bearer $accessToken"
}
Body = $null
}
$response = Invoke-RestMethod @params
return $response
} -ArgumentList $managementGroup, $accessToken
# Policy Definition Sets
Start-ThreadJob -Name "1-$($managementGroup.Name)" -ScriptBlock {
param ($managementGroup, $accessToken)
$params = @{
Method = "Get"
Uri = ("https://management.azure.com" + $managementGroup.Id + "/providers/Microsoft.Authorization/policySetDefinitions?api-version=2019-09-01")
Headers = @{
"Authorization" = "Bearer $accessToken"
}
Body = $null
}
$response = Invoke-RestMethod @params
return $response
} -ArgumentList $managementGroup, $accessToken
# Policy Definitions
Start-ThreadJob -Name "2-$($managementGroup.Name)" -ScriptBlock {
param ($managementGroup, $accessToken)
$params = @{
Method = "Get"
Uri = ("https://management.azure.com" + $managementGroup.Id + "/providers/Microsoft.Authorization/policyDefinitions?api-version=2019-09-01")
Headers = @{
"Authorization" = "Bearer $accessToken"
}
Body = $null
}
$response = Invoke-RestMethod @params
return $response
} -ArgumentList $managementGroup, $accessToken
}
Get-Job | Wait-Job | Receive-Job
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment