如何使用Powershell使用HDD OS磁盘创建新的VM

时间:2020-06-03 08:11:51

标签: azure azure-virtual-machine azure-powershell

我想选择HDD os磁盘来部署新的VM。我尝试使用“ New-AzDisk”,“ Set-AzVMOSDisk”和“ New-AzVM” powershell命令,但出现错误: New-AzVM : Cannot attach an existing OS disk if the VM is created from a platform, user or a shared gallery image. ErrorCode: InvalidParameter ErrorMessage: Cannot attach an existing OS disk if the VM is created from a platform, user or a shared gallery image. ErrorTarget: osDisk StatusCode: 400 ReasonPhrase: Bad Request OperationID : e7cac0e6-2ed8-4a61-a4ef-0bd04590b935 Au caractère Ligne:1 : 1 + New-AzVM -ResourceGroupName $rg_name -Location $location -VM $vm_adfs ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError : (:) [New-AzVM], ComputeCloudException + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.NewAzureVMCommand

我的代码如下

    $diskconfig = New-AzDiskConfig -Location $location -DiskSizeGB 127 -AccountType Standard_LRS -OsType Windows -CreateOption Empty
    $disk = New-AzDisk -ResourceGroupName $rg_name -DiskName $vm_name -Disk $diskconfig  
    $vm = New-AzVMConfig -VMName $vm_name -VMSize "Standard_DS1_V2"
    $vm = Set-AzVMOSDisk -VM $vm_adfs -ManagedDiskId $disk_adfs.Id -CreateOption Attach -Windows
    $vm = Set-AzVMOperatingSystem -VM $vm -Windows -ComputerName $vm_name -Credential $vm_cred -ProvisionVMAgent -EnableAutoUpdate
    $vm = Add-AzVMNetworkInterface -VM $vm -Id $nic.Id
    $vm = Set-AzVMSourceImage -VM $vm_adfs -PublisherName 'MicrosoftWindowsServer' -Offer 'WindowsServer' -Skus '2016-Datacenter' -Version latest
    New-AzVM -ResourceGroupName $rg_name -Location $location -VM $vm -Verbose

1 个答案:

答案 0 :(得分:0)

似乎您想从VM映像创建具有HDD os磁盘类型的VM,但是您的代码尝试附加现有的os磁盘。我认为是导致错误的原因。因此,您的解决方案是更改命令:

$vm = Set-AzVMOSDisk -VM $vm_adfs -ManagedDiskId $disk_adfs.Id -CreateOption Attach -Windows

进入:

$vm = Set-AzVMOSDisk -VM $vm_adfs -StorageAccountType Standard_LRS -CreateOption Attach -Windows

或者,如果要将现有的OS磁盘连接到新的VM,则只需删除命令:

$vm = Set-AzVMSourceImage -VM $vm_adfs -PublisherName 'MicrosoftWindowsServer' -Offer 'WindowsServer' -Skus '2016-Datacenter' -Version latest

在将另一个现有OS磁盘附加到虚拟机映像时,无法从该虚拟机映像创建新的虚拟机。当您同时使用这两种方法时,这两种方法会发生冲突。