'Powershell Loop though specific json files from multiple folders and use that json file to push into Loop
$tenantID = (Get-AzureADTenantDetail).ObjectId
$mg = "/providers/Microsoft.Management/managementGroups/" + $tenantID
foreach($subfolder in Get-ChildItem -Path C:\Users\xxxx\Desktop\AzurePolicies -Depth 2 -Recurse){
Write-Host "Subfolder is " $subfolder
foreach($file in Get-ChildItem -Path $subfolder -File *.json -Recurse){
foreach($policy in $file){
$displayname = (Get-Content $file -Raw | ConvertFrom-Json).properties.displayName
Write-Host "Display name is" $displayname
$policyname = (Get-Content $policy -Raw | ConvertFrom-Json).properties.name
Write-Host "Policy name is" $policyname
foreach ($item in $policy) {
function DefinePolicies {
New-AzPolicyDefinition -Name $policyname -DisplayName $displayname -Policy $policy -ManagementGroupName $tenantID
Start-Sleep -Seconds 2
}
DefinePolicies
function AssignPolicies {
$policy = Get-AzPolicyDefinition -Name $policyname -ManagementGroupName $tenantID
New-AzPolicyAssignment -Name $policyname -PolicyDefinition $policy -Scope $mg
Start-Sleep -Seconds 2
}
AssignPolicies
}
}
}
}
My Files are organized as Azurepolicies/Keyvaults/Somepolicy/azurepolicy.json
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
