Hi Ramakrishna Abhijeet P,
Thank you for posting in the Q&A Forums.
The following is a sample script showing how to use ForEach-Object -Parallel to parallelize your validation tasks. This script assumes that you have a directory structure that contains JSON files, each containing a DisplayName, ObjectID, and role definition name.
# Ensure you have the necessary modules
Import-Module Az
# Define the base directory containing subscription folders
$baseDir = "C:\Path\To\Your\Repo"
# Get all subscription folders
$subscriptionFolders = Get-ChildItem -Path $baseDir -Directory
# Function to validate a resource group
function Validate-ResourceGroup {
param (
[string]$resourceGroupPath
)
# Read the JSON content from the file
$jsonContent = Get-Content -Path $resourceGroupPath -Raw | ConvertFrom-Json
$displayName = $jsonContent.DisplayName
$objectId = $jsonContent.ObjectID
$roleDefinitionName = $jsonContent.RoleDefinitionName
# Validate DisplayName, ObjectID, and RoleDefinitionName
$isValidDisplayName = $true # Replace with actual validation logic
$isValidObjectId = $true # Replace with actual validation logic
$isValidRoleDefinition = $true # Replace with actual validation logic
# Output the validation result
[PSCustomObject]@{
DisplayName = $displayName
ObjectID = $objectId
RoleDefinitionName = $roleDefinitionName
IsValidDisplayName = $isValidDisplayName
IsValidObjectId = $isValidObjectId
IsValidRoleDefinition = $isValidRoleDefinition
}
}
# Process each subscription folder in parallel
$subscriptionFolders | ForEach-Object -Parallel {
param ($baseDir)
$subscriptionName = $_.Name
$resourceGroupFiles = Get-ChildItem -Path "$baseDir\$subscriptionName" -Filter *.json
# Process each resource group file in the subscription folder
$resourceGroupFiles | ForEach-Object {
$result = Validate-ResourceGroup -resourceGroupPath $_.FullName
Write-Output $result
}
} -ArgumentList $baseDir -ThrottleLimit 4 | Export-Csv -Path "ValidationResults.csv" -NoTypeInformation
The above is the content of the code, you can according to the comments to make changes for your environment.
Best regards
NeuviJ
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.