Мой сценарий: я должен передать строку JSON в качестве параметров для моего Azure пользовательского расширения сценария и выполнить сценарий для обновления настроек моей виртуальной машины. И я получил ошибку вроде ...ExecuteUpdateConfig.ps1 : Invalid array passed in, ']' expected...
Что я пробовал:
Я использую $ variable.GetType (), чтобы убедиться, что все имеет тип string.
Я пытался выполнить свой сценарий, не используя Azure Расширение пользовательского сценария для проверки самого сценария. И это на самом деле работает. Я передаю все строки, откуда берется «Массив»?
Например:
. ("D:\Utilities\ExecuteUpdateConfig.ps1") -DsnConfigsAsString $dsnconfigs -WebConfigsAsString $webconfigs
Я думаю, что проблема может заключаться в том, как я выполняю свой сценарий и передать параметры. Я попробовал два способа в местном:
a. Точечные работы:
. ("D:\Utilities\ExecuteUpdateConfig.ps1") -DsnConfigsAsString $dsnconfigs -WebConfigsAsString $webconfigs
b. этот синтаксис не работает и выдаст указанную ошибку:
powershell.exe -ExecutionPolicy Unrestricted -Command "D:\Utilities\\ExecuteUpdateConfig.ps1 -DsnConfigsAsString $dsnconfigs -WebConfigsAsString $webconfigs"
Как я сгенерирую свою строку JSON и передам ее в скрипт в расширении собственного скрипта:
$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"
В своем шаблоне ARM я затем объединяю свои параметры и команду PowerShell в CommandToExecute, я определил их как «строку» в разделе моих параметров в шаблоне ARM:
{
"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"
}
}
}
И в моем ExecuteUpdateConfig. скрипт ps1:
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"
}
Полная ошибка:
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