- Прежде всего получите все соответствующие шаблоны, например, через
$armTemplateFiles = Get-ChildItem -Path $PSScriptRoot -Include *.JobTemplate.json -Recurse;
- Теперь выполните итерацию по каждому файлу шаблона и создайте задание для каждого (эти задания затем выполняются параллельно)
Код:
foreach ($armTemplateFile in $armTemplateFiles) {
$logic = {
Param(
[object]
[Parameter(Mandatory=$true)]
$ctx,
[object]
[Parameter(Mandatory=$true)]
$armTemplateFile,
[string]
[Parameter(Mandatory=$true)]
$resourceGroupName
)
function Format-ValidationOutput {
param ($ValidationOutput, [int] $Depth = 0)
Set-StrictMode -Off
return @($ValidationOutput | Where-Object { $_ -ne $null } | ForEach-Object { @(' ' * $Depth + ': ' + $_.Message) + @(Format-ValidationOutput @($_.Details) ($Depth + 1)) })
}
# Get related parameters file
$paramTemplateFile = Get-ChildItem -Path $armTemplateFile.FullName.Replace("JobTemplate.json", "JobTemplate.parameters.json")
# Test Deployment
$ErrorMessages = Format-ValidationOutput (Test-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName `
-TemplateFile $armTemplateFile.FullName `
-TemplateParameterFile $paramTemplateFile.FullName `
-DefaultProfile $ctx)
if ($ErrorMessages) {
Write-Host '', 'Validation returned the following errors:', @($ErrorMessages), '', 'Template is invalid.'
}
else { # Deploy
New-AzureRmResourceGroupDeployment -Name (($armTemplateFile.Name).Split(".")[0] + ((Get-Date).ToUniversalTime()).ToString('MMddHHmm')) `
-ResourceGroupName $resourceGroupName `
-TemplateFile $armTemplateFile.FullName `
-TemplateParameterFile $paramTemplateFile.FullName `
-Force `
-ErrorVariable ErrorMessages `
-DefaultProfile $ctx
if ($ErrorMessages) {
Write-Host '', 'Template deployment returned the following errors:', @(@($ErrorMessages) | ForEach-Object { $_.Exception.Message.TrimEnd("`r`n") })
}
}
}
Start-Job $logic -ArgumentList (Get-AzureRmContext), $armTemplateFile, $ResourceGroupName
}
While (Get-Job -State "Running")
{
Start-Sleep 10
Write-Host "Jobs still running..."
}
Get-Job | Receive-Job