Azure / Azure/azure-powershell
Test-AzResourceGroupDeployment does not return validation output or error properly
- Dominant language
- C#
- Stars
- 4.8k
- Forks
- 4.3k
- Avg merge
- 2d 17h
- Merged PRs (30d)
- 51
Description
### Description
Test-AzResourceGroupDeployment does not return a powershell object of any kind and it's result is not capturable into a powershell variable for some reason.
$TestResult = Test-AzResourceGroupDeployment
Will result in $TestResult containing nothing when the validation test was successful even when set to verbose and a verbose output is displayed:
"VERBOSE: 12:44:15 PM - Template is valid."
$TestResult = Test-AzResourceGroupDeployment
Will result in $TestResult containing a single return character when the validation test was unsuccessful but verbose, no matter what the error or how many.
Even attempting to direct the error output into a variable via -ErrorVariable
Test-AzResourceGroupDeployment -ErrorVariable $TemplateValidationErrorVariable
results in $TemplateValidationErrorVariable being empty.
### Issue script & Debug output
```PowerShell
$ArmTemplateValidationParameters = @{
ResourceGroupName = $TargetResourceGroupName
TemplateFile = "$FullTemplatePath"
#Standard Parameters
Mode = 'Incremental'
Verbose = $true
ErrorAction = 'Stop'
WarningAction = 'Stop'
ErrorVariable = $TemplateValidationError
}
Try {$ValidationResult = Test-AzResourceGroupDeployment @ArmTemplateValidationParameters
} Catch {
Write-Error "Template is Invalid!"
$TemplateValidationSuccess = $false
}
```
### Environment data
```PowerShell
Name Value
---- -----
PSVersion 7.5.0
PSEdition Core
GitCommitId 7.5.0
OS Microsoft Windows 10.0.22631
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
```
### Module versions
```PowerShell
ModuleType Version PreRelease Name ExportedCommands
---------- ------- ---------- ---- ----------------
Script 4.0.2 Az.Accounts {Add-AzEnvironment, Clear-AzConfig, Clear-AzContext, Clear-AzDefault…}
Script 9.1.0 Az.Compute {Add-AzImageDataDisk, Add-AzVhd, Add-AzVMAdditionalUnattendContent, Add-AzVMDataDisk…}
Script 7.9.0 Az.Resources {Export-AzResourceGroup, Export-AzTemplateSpec, Get-AzDenyAssignment, Get-AzDeployment…}
```
### Error output
```PowerShell
HistoryId: 574
Message : Invalid character after parsing property name. Expected ':' but got: C. Path 'resources[0].type', line 10, position 9.
StackTrace : at Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.ResourceManagerCmdletBase.HandleException(ExceptionDispatchInfo
capturedException)
at Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.ResourceManagerCmdletBase.ExecuteCmdlet()
at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord()
Exception : Newtonsoft.Json.JsonReaderException
InvocationInfo : {Test-AzResourceGroupDeployment}
Line : $ValidationResult = $(Test-AzResourceGroupDeployment @ArmTemplateValidationParameters)
Position : At line:23 char:27
+ … nResult = $(Test-AzResourceGroupDeployment @ArmTemplateValidationPara …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HistoryId : 574
Message : Template is Invalid!
StackTrace :
Exception : Microsoft.PowerShell.Commands.WriteErrorException
InvocationInfo : {If ($null -ne $TemplateContent){
#The template variable isn't empty, so validate the template file
$TemplateImportSuccess = $true
$ArmTemplateValidationParameters = @{
ResourceGroupName = $TargetResourceGroupName
TemplateFile = "$FullExportedTemplatePath"
#Standard Parameters
Mode = 'Incremental'
Verbose = $true
ErrorAction = 'Stop'
WarningAction = 'Stop'
ErrorVariable = $TemplateValidationError
}
Try {$ValidationResult = Test-AzResourceGroupDeployment @ArmTemplateValidationParameters} Catch {
Write-Error "Template is Invalid!"
$TemplateValidationSuccess = $false
$TemplateImportSuccess = $false
}
#Use the validation result to determine the template validity
$ValidationResult = $(Test-AzResourceGroupDeployment @ArmTemplateValidationParameters)
IF ($(Test-AzResourceGroupDeployment @ArmTemplateValidationParameters) -match $TemplateValidationResultFailureRegexPattern) {
Write-Error "Template is Invalid Regex!"
$TemplateImportSuccess = $false
}
}}
Line :
Position :
HistoryId : 574
Message : Invalid character after parsing property name. Expected ':' but got: C. Path 'resources[0].type', line 10, position 9.
StackTrace : at Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.ResourceManagerCmdletBase.HandleException(ExceptionDispatchInfo
capturedException)
at Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.ResourceManagerCmdletBase.ExecuteCmdlet()
at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord()
Exception : Newtonsoft.Json.JsonReaderException
InvocationInfo : {Test-AzResourceGroupDeployment}
Line : Try {$ValidationResult = Test-AzResourceGroupDeployment @ArmTemplateValidationParameters
Position : At line:15 char:30
+ … ionResult = Test-AzResourceGroupDeployment @ArmTemplateValidationPara …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HistoryId : 574
```
Contributor guide
Research direction
Start with the Test-AzResourceGroupDeployment cmdlet and reproduce the successful and failing cases using the provided PowerShell parameters, including Verbose, ErrorAction, and ErrorVariable. Check how its validation output and errors are emitted, then verify that callers can capture the success result and validation errors without relying on displayed verbose text.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure, powershell
- Domain
- cli, cloud
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100