Azure Powershell-部署模板验证失败-Jtoken

问题描述

我正在尝试一次使用Powershell和模板文件一次部署多个虚拟机,但我一直收到此错误

New-AzResourceGroupDeployment : 9:27:40 AM - Error: Code=InvalidTemplate; Message=Deployment template validation
Failed: 'Template parameter JToken type is not valid. Expected 'String,Uri'. Actual 'Object'. Please see
https://aka.ms/resource-manager-parameter-files for usage details.'.
At C:\Users\jackk\OneDrive\Desktop\AzureAutomation\main.ps1:193 char:9
+         New-AzResourceGroupDeployment @parameters
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [New-AzResourceGroupDeployment],Exception
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDep
   loymentCmdlet

New-AzResourceGroupDeployment : The deployment validation Failed
At C:\Users\jackk\OneDrive\Desktop\AzureAutomation\main.ps1:193 char:9
+         New-AzResourceGroupDeployment @parameters
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [New-AzResourceGroupDeployment],InvalidOperationException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDep
   loymentCmdlet

这是我的powershell脚本的相关部分:

$location = "eastus2"
$networkInterfaceName = ""
$networkSecurityGroupName = ""
#$networkSecurityGroupRules = @()
$subnetName = $subnetSelection
$virtualNetworkId = "/subscriptions/1234-1234-123-123-123/resourceGroups/MainGroup/providers/Microsoft.Network/virtualNetworks/Main_Network"
$virtualMachineName = ""
$virtualMachineComputerName = ""
$virtualMachineRG = $resourceGroupSelection
$osdiskType = "StandardSSD_lrs"
$virtualMachinesize = "Standard_B2s"
$adminUsername = "ADMIN"
$adminPassword = ConvertTo-securestring "Password1234" -AsPlainText -Force
$diagnosticsstorageAccountName = $StorageAccountName
$diagnosticsstorageAccountId = $StorageAccountID
$imageName = $ImageSelection    

foreach ($i in 1..5) {
    
    $virtualMachineName = "test-$i".ToString()
    $virtualMachineComputerName = "test-$i".ToString()
    $rand = Get-Random -Maximum 1000
    $networkInterfaceName = "test-$i$rand".ToString()
    $networkSecurityGroupName = "test-$i-nsg".ToString()

    $paramObject = @{
        'location' = $location
        'networkInterfaceName' = $networkInterfaceName
        'networkSecurityGroupName' = $networkSecurityGroupName
        'subnetName' = $subnetSelection
        'virtualNetworkId' = $virtualNetworkId
        'virtualMachineName' = $virtualMachineName
        'virtualMachineComputerName' = $virtualMachineComputerName
        'virtualMachineRG' = $resourceGroupSelection
        'osdiskType' = $osdiskType
        'virtualMachinesize' = $virtualMachinesize
        'adminUsername' = $adminUsername
        'adminPassword' = $adminPassword
        'diagnosticsstorageAccountName' = $diagnosticsstorageAccountName
        'diagnosticsstorageAccountId' = $diagnosticsstorageAccountId
        'imageName' = $imageName
    }

    $parameters = @{
        'ResourceGroupName' = $resourceGroupSelection
        'TemplateFile' = $PathTemplate
        'TemplateParameterObject' = $paramObject
        'Verbose' = $True
    }

    New-AzResourceGroupDeployment @parameters

}

假定此代码段中未定义的任何变量都是在其他地方定义的带有有效信息的字符串。

我正在使用的模板文件

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#","contentVersion": "1.0.0.0","parameters": {
        "location": {
            "type": "String"
        },"networkInterfaceName": {
            "type": "String"
        },"networkSecurityGroupName": {
            "type": "String"
        },"networkSecurityGroupRules": {
            "type": "Array","defaultValue": []
        },"subnetName": {
            "type": "String"
        },"virtualNetworkId": {
            "type": "String"
        },"virtualMachineName": {
            "type": "String"
        },"virtualMachineComputerName": {
            "type": "String"
        },"virtualMachineRG": {
            "type": "String"
        },"osdiskType": {
            "type": "String"
        },"virtualMachinesize": {
            "type": "String"
        },"adminUsername": {
            "type": "String"
        },"adminPassword": {
            "type": "securestring"
        },"diagnosticsstorageAccountName": {
            "type": "String"
        },"diagnosticsstorageAccountId": {
            "type": "String"
        },"imageName": {
            "type": "String"
        }
    },"variables": {
        "nsgid": "[resourceId(resourceGroup().name,'Microsoft.Network/networkSecurityGroups',parameters('networkSecurityGroupName'))]","vnetId": "[parameters('virtualNetworkId')]","subnetRef": "[concat(variables('vnetId'),'/subnets/',parameters('subnetName'))]"
    },"resources": [
        {
            "type": "Microsoft.Network/networkInterfaces","apiVersion": "2019-07-01","name": "[parameters('networkInterfaceName')]","location": "[parameters('location')]","dependsOn": [
                "[concat('Microsoft.Network/networkSecurityGroups/',parameters('networkSecurityGroupName'))]"
            ],"properties": {
                "ipConfigurations": [
                    {
                        "name": "ipconfig1","properties": {
                            "subnet": {
                                "id": "[variables('subnetRef')]"
                            },"privateIPAllocationMethod": "Dynamic"
                        }
                    }
                ],"networkSecurityGroup": {
                    "id": "[variables('nsgid')]"
                }
            }
        },{
            "type": "Microsoft.Network/networkSecurityGroups","apiVersion": "2019-02-01","name": "[parameters('networkSecurityGroupName')]","properties": {
                "securityRules": "[parameters('networkSecurityGroupRules')]"
            }
        },{
            "type": "Microsoft.Compute/virtualMachines","name": "[parameters('virtualMachineName')]","dependsOn": [
                "[concat('Microsoft.Network/networkInterfaces/',parameters('networkInterfaceName'))]"
            ],"properties": {
                "hardwareProfile": {
                    "vmSize": "[parameters('virtualMachinesize')]"
                },"storageProfile": {
                    "osdisk": {
                        "createOption": "fromImage","manageddisk": {
                            "storageAccountType": "[parameters('osdiskType')]"
                        }
                    },"imageReference": {
                        "id": "[parameters('imageName')]"
                    }
                },"networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces',parameters('networkInterfaceName'))]"
                        }
                    ]
                },"osProfile": {
                    "computerName": "[parameters('virtualMachineComputerName')]","adminUsername": "[parameters('adminUsername')]","adminPassword": "[parameters('adminPassword')]","windowsConfiguration": {
                        "enableAutomaticUpdates": true,"provisionVmAgent": true
                    }
                },"diagnosticsProfile": {
                    "bootDiagnostics": {
                        "enabled": true,"storageUri": "[concat('https://',parameters('diagnosticsstorageAccountName'),'.blob.core.windows.net/')]"
                    }
                }
            }
        }
    ],"outputs": {
        "adminUsername": {
            "type": "String","value": "[parameters('adminUsername')]"
        }
    }
}

我已尽力想尽一切办法,并且我认为已将问题缩小到networkSecurityGroupRules。我从Azure中的有效部署中获取了此模板文件networkSecurityGroupRules随附的参数文件的值设置为[]。所以我为其创建了一个认值,但是它不起作用。我也尝试过在Powershell中将其声明为空数组,并将其与其他paramObjects一起传递,但遇到相同的错误。我也尝试了ArrayList,但没有成功。我还用在$paramObjects中传递给它的确切值填充了parameter.json文件,它可以完美地工作。如果我将defaultValue更改为null,则在部署时它将要求我填写networkSecurityGroupRules [0],networkSecurityGroupRules [1]等。我真的不知道问题出在哪里,因此将无济于事。非常感谢,谢谢。

解决方法

发现了问题。 “ paramObject”中的所有内容都必须用引号引起来。例如,'location' = $location必须为'location' = "$location"