Azure创建Linux VM报错:Required parameter 'osProfile.computerName'缺失
Azure PowerShell创建Linux VM时osProfile.computerName缺失错误排查
执行以下PowerShell脚本创建Azure Linux虚拟机时,明明已指定ComputerName,却返回osProfile.computerName缺失的错误,已重装Azure PS模块,求解决方案:
执行的脚本
$LocationName = "switzerlandnorth" $ResourceGroupName = "my_group" $SecurePassword = ConvertTo-SecureString "secret" -AsPlainText -Force $Credential = New-Object System.Management.Automation.PSCredential ("user1", $SecurePassword); $VirtualMachine = New-AzVMConfig -VMName "testvm" -VMSize "Standard_B2s" $ComputerName = "testvm-dec2024" $VirtualMachine = Add-AzVMNetworkInterface -VM $VirtualMachine -Id "/subscriptions/xxxxxx/resourceGroups/Production/providers/Microsoft.Network/networkInterfaces/yyyyy" $VirtualMachine = Add-AzVMSshPublicKey -VM $VirtualMachine -Path "/home/xxxx/.ssh/authorized_keys" -KeyData "xxxxxx" $VirtualMachine = Set-AzVMOperatingSystem -VM $VirtualMachine -Linux -ComputerName "test" -Credential $Credential $VirtualMachine = Set-AzVMSourceImage -VM $VirtualMachine -PublisherName "canonical" -Offer "0001-com-ubuntu-minimal-jammy" -sku "minimal-22_04-lts-gen2" -Version "latest" New-AzVM -VM $VirtualMachine -ResourceGroupName $ResourceGroupName -Location $LocationName -Verbose
报错信息
Line | 20 | New-AzVM -VM $VirtualMachine -ResourceGroupName $ResourceGroupName -L … | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Required parameter 'osProfile.computerName' is missing (null). ErrorCode: InvalidParameter ErrorMessage: Required parameter 'osProfile.computerName' is missing (null). ErrorTarget: | osProfile.computerName StatusCode: 400 ReasonPhrase: OperationID : xxxxxx
问题原因及解决方案
核心问题:SSH公钥与Credential参数冲突
对于使用SSH公钥认证的Linux虚拟机,同时指定-Credential参数会导致OS Profile配置异常,ComputerName无法正确写入配置对象。此外脚本存在两处细节错误:
Add-AzVMSshPublicKey的-Path中用户名是xxxx,但Credential指定的是user1,路径不匹配Set-AzVMOperatingSystem中硬编码了ComputerName为test,但之前定义的变量$ComputerName未被使用
修正后的脚本
$LocationName = "switzerlandnorth" $ResourceGroupName = "my_group" # 使用SSH公钥认证时无需密码Credential,移除相关定义 # $SecurePassword = ConvertTo-SecureString "secret" -AsPlainText -Force # $Credential = New-Object System.Management.Automation.PSCredential ("user1", $SecurePassword); $VirtualMachine = New-AzVMConfig -VMName "testvm" -VMSize "Standard_B2s" $ComputerName = "testvm-dec2024" $VirtualMachine = Add-AzVMNetworkInterface -VM $VirtualMachine -Id "/subscriptions/xxxxxx/resourceGroups/Production/providers/Microsoft.Network/networkInterfaces/yyyyy" # 修正SSH路径为对应用户名user1 $VirtualMachine = Add-AzVMSshPublicKey -VM $VirtualMachine -Path "/home/user1/.ssh/authorized_keys" -KeyData "xxxxxx" # 使用定义的$ComputerName变量,移除-Credential参数,添加-DisablePasswordAuthentication禁用密码登录 $VirtualMachine = Set-AzVMOperatingSystem -VM $VirtualMachine -Linux -ComputerName $ComputerName -DisablePasswordAuthentication $VirtualMachine = Set-AzVMSourceImage -VM $VirtualMachine -PublisherName "canonical" -Offer "0001-com-ubuntu-minimal-jammy" -sku "minimal-22_04-lts-gen2" -Version "latest" New-AzVM -VM $VirtualMachine -ResourceGroupName $ResourceGroupName -Location $LocationName -Verbose
额外检查项
- 确认指定的网卡资源存在,且当前账号有访问该跨资源组网卡的权限
- 确保Azure PS模块版本为最新(已重装可忽略)
内容的提问来源于stack exchange,提问作者Mike
相关产品推荐
相关产品推荐

