mwa
mwa

Reputation: 148

How to create a new VM with a HDD os disk using Powershell

I want to choose HDD os disk for a new VM to be deployed. I have tried using "New-AzDisk" "Set-AzVMOSDisk" and "New-AzVM" powershell commands but got an error : 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

my code is the following

    $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

Upvotes: 2

Views: 3946

Answers (1)

Charles Xu
Charles Xu

Reputation: 31462

It seems you want to create a VM with HDD os disk type from a VM image, but your code attempt to attach an existing os disk. I think it's the problem that caused the error. So the solution for you is that change the command:

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

into:

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

Or if you want to attach the existing OS disk to your new VM, then you just need to delete the command:

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

You cannot create a new VM from the VM image while attaching another existing OS disk to it. These two means will conflict when you use both of them at one time.

Upvotes: 2

Related Questions