Create a VM with Azure Cloud Shell (PowerShell) with a non-Premium SSD OS disk

Frank van Graafeiland 166 Reputation points
2020-11-10T15:42:50.647+00:00

Hi Guys and gals,

I've sucessfuly created a VM in Azure Cloud Shell using PowerShell. The issue I'm having is that I can't seem to figure out how to create a VM with a standard marketplace Win2019 Datacenter image and a non-Premium SSD OS disk. Via the Azure Portal you can easily specify the image at the Basics tab and the type of OS disk at the Disks tab. However, I can't seem to find how this can be done in PowerShell, without creating the VM with a Premium OS disk first and then deallocating it, just to change the OS disk to a non-Premium performance kind. Any help regarding my issue is very much appreciated!

Here's the unfinished script I use to deploy a VM, which unfortunately gives it a Premium SSD OS disk by default:

$rGName = “ResourceGroupName-RG”
$lc = “West Europe”
$rdpAllowedIp = “PublicIP”
$vmSubnetPrefix = “10.0.0.0/24”
$vmSubnetName = “VM-Subnet”
$vnetPrefix = “10.0.0.0/16”
$vnetName = “VNetName”
$nsgName = “$vmSubnetName-NSG”
$publicIpSku = “Standard”
$publicIpAllocationMethod = “Static”
$vmAdmin = "LocalAdmin"
$vmAdminPassword = Read-Host “Admin password” -AsSecureString
$vmName = "MyVM"
$vmSize = "Standard_D2S_v3"
$vmTimeZone = “W. Europe Standard Time”

This command creates a new resource group.

$rG= New-AzResourceGroup -Location $lc -Name $rGName

This command defines an RDP rule for the Network Security Group created in the next step.

$nsgAllowRdpRule = New-AzNetworkSecurityRuleConfig -Name “RDP-Allow-$rdpAllowedIp” -Description "Allows RDP from a specific IP address." -Access Allow -Protocol Tcp -Direction Inbound -Priority 300 -SourceAddressPrefix $rdpAllowedIp -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389

Here, the Network Security Group is created with a rule to allow RDP from the IP address specified by $rdpAllowedIp

$nsg = New-AzNetworkSecurityGroup -ResourceGroupName $rgName -Location $lc -Name "$nsgName" -SecurityRules $NSGAllowRDPRule

This command creates a subnet configuration which will be deployed when creating a virtual network.

$subnet = New-AzVirtualNetworkSubnetConfig -Name $vmSubnetName -AddressPrefix $vmSubnetPrefix -NetworkSecurityGroupId $NSG.Id

A new virtual network and a subnet will be created. The subnet is created in accordance with the subnet configuration set above.

$vnet = New-AzVirtualNetwork -AddressPrefix $vnetPrefix -Location $lc -Name $vnetName -ResourceGroupName $rGName -Subnet $subnet

A public IP address will be created via which you can connect to the virtual machine. If you’ve set the $publicIpSku as Standard, the AllocationMethod parameter must be set to Static.

$PublicIp = New-AzPublicIpAddress -Name “$vmName-IP” -ResourceGroupName $rGName -Location $lc -Sku $publicIpSku -AllocationMethod $publicIpAllocationMethod

$NIC = New-AzNetworkInterface -Name “$vmName-NIC” -ResourceGroupName $rgName -Location $lc -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $PublicIp.Id

$Credential = New-Object System.Management.Automation.PSCredential ($vmAdmin, $vmAdminPassword);

$VirtualMachine = New-AzVMConfig -VMName $vmName -VMSize $vmSize

$VirtualMachine = Set-AzVMOperatingSystem -VM $VirtualMachine -Windows -ComputerName $vmName -Credential $Credential -ProvisionVMAgent -EnableAutoUpdate -TimeZone $vmTimeZone

$VirtualMachine = Add-AzVMNetworkInterface -VM $VirtualMachine -Id $NIC.Id

$VirtualMachine = Set-AzVMSourceImage -VM $VirtualMachine -PublisherName -Offer 'WindowsServer' -Skus '2019-Datacenter' -Version latest

New-AzVM -ResourceGroupName $rgName -Location $lc -VM $VirtualMachine -Verbose

I've tried experimenting with the following cmdlets, but then I got the message: "New-AzVM: This operation is not supported for a relative URI."
Set-AzVMOSDisk -VM $VirtualMachine -Name "$vmName-OSDisk" -VhdUri "os.vhd" -Caching ReadWrite

$VirtualMachine = Set-AzVMOperatingSystem -VM $VirtualMachine -Windows -ComputerName "$vmName" -Credential $Credential -ProvisionVMAgent -EnableAutoUpdate

$VirtualMachine = Set-AzVMSourceImage -VM $VirtualMachine -PublisherName 'MicrosoftWindowsServer' -Offer 'WindowsServer' -Skus '2019-Datacenter' -Version latest

Thanks in advance.

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,780 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 107.9K Reputation points MVP
    2020-11-10T16:37:36.903+00:00

    With the cmdlet "Set-AzVMOSDisk" you can use the parameter called "-StorageAccountType".
    There you you can specify the type of disk:
    Premium_LRS = Premium SSD
    StandardSSD_LRS = Standard SSD
    Standard_LRS = Standard HHD


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    2 people found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 107.9K Reputation points MVP
    2020-11-12T19:56:27.583+00:00

    I am using the Set-AzVMOSDisk cmdlet this way:

    Set-AzVMOSDisk -Name vmnameOSdisk1245 -DiskSizeInGB 30 -StorageAccountType Premium_LRS -CreateOption "fromImage" 
    Set-AzVMSourceImage -PublisherName "MicrosoftWindowsServer" -Offer "$WindowsServer" -Skus "2016-Datacenter-smalldisk" -Version "latest"
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    1 person found this answer helpful.
    0 comments No comments

  2. Frank van Graafeiland 166 Reputation points
    2020-11-13T18:51:02.177+00:00

    Thanks again, Andreas

    I will try this in the near future.

    0 comments No comments

  3. Frank van Graafeiland 166 Reputation points
    2020-11-14T09:49:18+00:00

    Works like a charm. Thanks again!


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.