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.