My scenario:
I have to pass in a JSON string as parameters to my Azure custom script extension and execute a script to update my VM configs. And I got error like ...ExecuteUpdateConfig.ps1 : Invalid array passed in, ']' expected...
What I have tried:
I use $variable.GetType() to make sure everything is of type string.
I tried to execute my script without using Azure Custom Script extension to test the script itself. And it actually works. I am passing in all strings, where does the "Array" coming from ?
E.g.
. ("D:\Utilities\ExecuteUpdateConfig.ps1") -DsnConfigsAsString $dsnconfigs -WebConfigsAsString $webconfigs
I think the problem could be how I execute my script and pass in the parameters. I tried two ways in the local:
a. dot sourcing works:
. ("D:\Utilities\ExecuteUpdateConfig.ps1") -DsnConfigsAsString $dsnconfigs -WebConfigsAsString $webconfigs
b. this syntax does not work and will throw the error mentioned:
powershell.exe -ExecutionPolicy Unrestricted -Command "D:\Utilities\\ExecuteUpdateConfig.ps1 -DsnConfigsAsString $dsnconfigs -WebConfigsAsString $webconfigs"
How I generate my JSON string and pass my string to script in custom script extension :
$vm = @{ }
$vm | Add-Member WebConfigs @()
$vm | Add-Member WebConfigs @()
$vm | Add-Member WebConfigs @()
$vm.WebConfigs += ([PSCustomObject]@{
Path = "C:\WindowsAzure\Packages\CommonAgentConfig.config"
Node = "configSections"
ChildNode = "section"
AttributeKey = "name"
AttributeValue = "microsoft.windowsAzure.guestAgent.configuration"
UpdatedValue = "SamLeong"
EncryptedValue = $false
})
##convert to json string
$webconfigs = ConvertTo-Json @($vm.WebConfigs )
$dsnconfigs = ConvertTo-Json @($vm.DsnConfigs)
$registryconfigs = ConvertTo-Json @()
##Generate a VM with custom script extension using ARM template
##dsnConfigs, webconfigs and registryConfigs are my JSON string
##password,fileuri, resourcegorupname not related to the problems are not shown here
New-AzResourceGroupDeployment `
-ResourceGroupName $resourceGroupName `
-TemplateFile ".\customscriptext.json" `
-adminUsername "tester" `
-adminPassword $adminPassword `
-vmName "test16" `
-dsnConfigs $dsnconfigs -webConfigs $webconfigs -FileUrl $fileUrl `
-dnsLabelPrefix "samtest16"
In my ARM template, I then concat my params and PowerShell command into CommandToExecute, I defined them as "string" in my parameters section in ARM template:
{
"apiVersion": "2018-06-01",
"type": "Microsoft.Compute/virtualMachines/extensions",
"location": "[resourceGroup().location]",
"name": "[concat(parameters('vmName'),'/installcustomscript')]",
"dependsOn": [
"[concat('Microsoft.Compute/virtualMachines/', parameters('vmName'))]"
],
"tags": {
"displayName": "config-app"
},
"properties": {
"publisher": "Microsoft.Compute",
"type": "CustomScriptExtension",
"typeHandlerVersion": "1.10",
"autoUpgradeMinorVersion": true,
"settings": {
"fileUris": "[parameters('FileUrl')]"
},
"protectedSettings": {
"commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File ExecuteUpdateConfig.ps1', ' -DsnConfigsAsString ', parameters('dsnConfigs'), ' -WebConfigsAsString ', parameters('webConfigs'))]",
"storageAccountName": "xxxxxxxxxxxxxxxxxxxxxxxxx",
"storageAccountKey": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
}
}
And in my ExecuteUpdateConfig.ps1 script:
param([string] $DsnConfigsAsString, [string] $WebConfigsAsString )
try {
$DsnConfigs = $DsnConfigsAsString | Out-String | ConvertFrom-Json
$WebConfigs = $WebConfigsAsString | Out-String | ConvertFrom-Json
#other execution logics follow...
foreach ($dsn in $DsnConfigs) { ##read the dsn object and update }
foreach ($webConfig in $WebConfigs) { ##read the webconfig object and update }
}catch{
Write-Error "$($_.Exception.Message)"
throw "Error in updateAllSettings"
}
Full error:
Microsoft.Compute/virtualMachines/extensions 'samtest17/installcustomscript' failed
with message '{
"status": "Failed",
"error": {
"code": "ResourceDeploymentFailure",
"message": "The resource operation completed with terminal provisioning state
'Failed'.",
"details": [
{
"code": "VMExtensionProvisioningError",
"message": "VM has reported a failure when processing extension
'installcustomscript'. Error message: \"Command execution finished, but failed
because it returned a non-zero exit code of: '1'. The command had an error output of:
'C:\\Packages\\Plugins\\Microsoft.Compute.CustomScriptExtension\\1.10.5\\Downloads\\0\
\\r\nExecuteUpdateConfig.ps1 : Invalid array passed in, ']' expected. (3): [\r\n +
CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorExcep \r\n
tion\r\n + FullyQ...' For more information, check the instance view by executing
Get-AzVmssVm or Get-AzVm (https://aka.ms/GetAzVm). These commands can be executed
using CloudShell (https://aka.ms/CloudShell)\"\r\n\r\nMore information on
troubleshooting is available at https://aka.ms/VMExtensionCSEWindowsTroubleshoot "
}
]
}
}'
At D:\Scripts\Testing\TestCustomScript.ps1:58 char:1
+ New-AzResourceGroupDeployment `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-AzResourceGroupDeployment], Exce
ption
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implem
entation.NewAzureResourceGroupDeploymentCmdlet
New-AzResourceGroupDeployment : 10:42:42 AM - VM has reported a failure when
processing extension 'installcustomscript'. Error message: "Command execution
finished, but failed because it returned a non-zero exit code of: '1'. The command
had an error output of:
'C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.10.5\Downloads\0\
ExecuteUpdateConfig.ps1 : Invalid array passed in, ']' expected. (3): [
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorExcep
tion
+ FullyQ...' For more information, check the instance view by executing
Get-AzVmssVm or Get-AzVm (https://aka.ms/GetAzVm). These commands can be executed
using CloudShell (https://aka.ms/CloudShell)"
More information on troubleshooting is available at
https://aka.ms/VMExtensionCSEWindowsTroubleshoot
At D:\Scripts\Testing\TestCustomScript.ps1:58 char:1
+ New-AzResourceGroupDeployment `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-AzResourceGroupDeployment], Exce
ption
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implem
entation.NewAzureResourceGroupDeploymentCmdlet
New-AzResourceGroupDeployment : 10:42:42 AM - Template output evaluation skipped: at
least one resource deployment operation failed. Please list deployment operations for
details. Please see https://aka.ms/DeployOperations for usage details.
At D:\Scripts\Testing\TestCustomScript.ps1:58 char:1
+ New-AzResourceGroupDeployment `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-AzResourceGroupDeployment], Exce
ption
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implem
entation.NewAzureResourceGroupDeploymentCmdlet
New-AzResourceGroupDeployment : 10:42:42 AM - Template output evaluation skipped: at
least one resource deployment operation failed. Please list deployment operations for
details. Please see https://aka.ms/DeployOperations for usage details.
At D:\Scripts\Testing\TestCustomScript.ps1:58 char:1
+ New-AzResourceGroupDeployment `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-AzResourceGroupDeployment], Exce
ption
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implem
entation.NewAzureResourceGroupDeploymentCmdlet