Can't create a Databricks from a powershell script - azure

My issue
Can't create a databricks from script. Always getting this error message:
Private and public subnets should be unique for the Virtual Network
'/subscriptions/XXXX/resourceGroups/rgtest64/providers/Microsoft.Network/virtualNetworks/vnet64'.
Public subnet name: 'privsub64', Private subnet name:
| 'privsub64'
How to reproduce
Just running this script:
$location="westeurope"
$rg = "rgtest64"
$vnet = "vnet64"
New-AzResourceGroup -name $rg -Location westeurope
$dlg = New-AzDelegation -Name dbrdl -ServiceName "Microsoft.Databricks/workspaces"
$rdpRule = New-AzNetworkSecurityRuleConfig -Name rdp-rule -Description "Allow RDP" -Access Allow `
-Protocol Tcp -Direction Inbound -Priority 100 -SourceAddressPrefix Internet `
-SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389
$networkSecurityGroup = New-AzNetworkSecurityGroup -ResourceGroupName $rg `
-Location $location -Name nsg-test -SecurityRules $rdpRule
$privSubnet = New-AzVirtualNetworkSubnetConfig -Name privsub64 -AddressPrefix "10.0.1.0/24" `
-NetworkSecurityGroup $networkSecurityGroup -Delegation $dlg
$pubSubnet = New-AzVirtualNetworkSubnetConfig -Name pubsub64 -AddressPrefix "10.0.2.0/24" `
-NetworkSecurityGroup $networkSecurityGroup -Delegation $dlg
$testVN = New-AzVirtualNetwork -Name $vnet -ResourceGroupName $rg `
-Location $location -AddressPrefix "10.0.0.0/16" -Subnet $privSubnet,$pubSubnet
New-AzDatabricksWorkspace -Name databricks-test-with-custom-vn -ResourceGroupName $rg -Location $location `
-VirtualNetworkId $testVN.Id -PrivateSubnetName $privSubnet.Name -PublicSubnetName `
$privSubnet.Name -Sku standard
PSVersion: 7.1.3
PSEdition: Core
What I tested
I tested the script on several subscriptions.
I also tried without creating the subnets, same result.
I tested from an ARM template, same result.
It is working if I don't reference any VNET, but for internal reasons I need to impose a specific VNET.
What I need
Understanding what happen, and what to do
Everything was working until a few days ago. I don't understand
thanks

According to the error, your public subnet,s name and your private subnet name are the same. Whew we deploy Databricks workspace into vnet, the two subnets should be different. Please update your script as
New-AzDatabricksWorkspace -Name databricks-test-with-custom-vn -ResourceGroupName $rg -Location $location `
-VirtualNetworkId $testVN.Id -PrivateSubnetName $privSubnet.Name -PublicSubnetName `
$pubSubnet.Name -Sku standard
.
The whole script is as below
$location="westeurope"
$rg = "rgtest64"
$vnet = "vnet64"
New-AzResourceGroup -name $rg -Location westeurope
$dlg = New-AzDelegation -Name dbrdl -ServiceName "Microsoft.Databricks/workspaces"
$rdpRule = New-AzNetworkSecurityRuleConfig -Name rdp-rule -Description "Allow RDP" -Access Allow `
-Protocol Tcp -Direction Inbound -Priority 100 -SourceAddressPrefix Internet `
-SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389
$networkSecurityGroup = New-AzNetworkSecurityGroup -ResourceGroupName $rg `
-Location $location -Name nsg-test -SecurityRules $rdpRule
$privSubnet = New-AzVirtualNetworkSubnetConfig -Name privsub64 -AddressPrefix "10.0.1.0/24" `
-NetworkSecurityGroup $networkSecurityGroup -Delegation $dlg
$pubSubnet = New-AzVirtualNetworkSubnetConfig -Name pubsub64 -AddressPrefix "10.0.2.0/24" `
-NetworkSecurityGroup $networkSecurityGroup -Delegation $dlg
$testVN = New-AzVirtualNetwork -Name $vnet -ResourceGroupName $rg `
-Location $location -AddressPrefix "10.0.0.0/16" -Subnet $privSubnet,$pubSubnet
New-AzDatabricksWorkspace -Name databricks-test-with-custom-vn -ResourceGroupName $rg -Location $location `
-VirtualNetworkId $testVN.Id -PrivateSubnetName $privSubnet.Name -PublicSubnetName `
$pubSubnet.Name -Sku standard

Related

unable to create Azure scale sets with error Cannot parse the request error

################### Resource Group ###################
$rg = New-AzResourcegroup -Name "vlab3rg1" -Location "EastUS"
$rgname = $rg.ResourceGroupName
################# Network Security group ####################
$rule1 = New-AzNetworkSecurityRuleConfig -Name "Allow_RDP" -Protocol * -SourceAddressPrefix * -DestinationAddressPrefix * `
-SourcePortRange * -DestinationPortRange 3389 -Access Allow -Priority 100 -Direction Inbound
$nsg = New-AzNetworkSecurityGroup -Name vmlab3nsg -Location "EastUS" -ResourceGroupName $rgname -SecurityRules $rule1
################### Create VNET and Subnet ###################
$subnet = New-AzVirtualNetworkSubnetConfig -Name subnet1 -AddressPrefix "10.0.1.0/27" -NetworkSecurityGroup $nsg
$vnet = New-AzVirtualNetwork -Name vmlab3vnet -ResourceGroupName $rgname -Location "EastUS" -AddressPrefix "10.0.1.0/24" -Subnet $subnet
######### Create public ip ####################################
$publicip = New-AzPublicIpAddress -Name publiciplb -ResourceGroupName $rgname -Location "EastUS" -Sku Standard -AllocationMethod Static
############ Create Lad balancer #################
$fronendip = New-AzLoadBalancerFrontendIpConfig -Name "frondendip_lb" -PublicIpAddress $publicip
$backendPool = New-AzLoadBalancerBackendAddressPoolConfig -Name "myBackEndPool"
$natpool1 = New-AzLoadBalancerInboundNatPoolConfig -Name "nat_rdp" -Protocol TCP -FrontendPortRangeStart 3389 -FrontendPortRangeEnd 50001 -BackendPort 50010 -FrontendIpConfigurationId $fronendip.Id
$lb = New-AzLoadBalancer -ResourceGroupName $rgname -Name lb_rdp -Location 'EastUS' -FrontendIpConfiguration $fronendip -BackendAddressPool $backendPool -InboundNatPool $natpool1 -sku Standard
############### Create vm #################
$ipConfig = New-AzVmssIpConfig `
-Name "myIPConfig" `
-LoadBalancerInboundNatPoolsId $lb.BackendAddressPools[0].Id `
-SubnetId $vnet.Subnets[0].Id `
$vmconfig = New-AzVmssConfig -Location "EastUS" -SkuCapacity 1 -SkuName "Standard_DS2" -UpgradePolicyMode Automatic
Set-AzVmssStorageProfile $vmconfig `
-OsDiskCreateOption "FromImage" `
-ImageReferencePublisher "MicrosoftWindowsServer" `
-ImageReferenceOffer "WindowsServer" `
-ImageReferenceSku "2016-Datacenter" `
-ImageReferenceVersion "latest" `
Set-AzVmssOsProfile $vmconfig `
-AdminUsername "admin123" `
-AdminPassword "W3lcome_123456" `
-ComputerNamePrefix "vmlab3w1"
Add-AzVmssNetworkInterfaceConfiguration `
-Name network_config `
-VirtualMachineScaleSet $vmconfig `
-Primary $true `
-IpConfiguration $ipConfig
New-AzVMss -ResourceGroupName $rgname -VMScaleSetName "VMSSKArt" -VirtualMachineScaleSet $vmconfig
Output:
New-AzVMss : Cannot parse the request.
ErrorCode: InvalidRequestFormat
ErrorMessage: Cannot parse the request.
ErrorTarget: StatusCode: 400
ReasonPhrase: Bad Request OperationID :
7f540760-4e1a-49aa-9ec9-eb33204bdcdd At line:1 char:1
+ New-AzVMss -ResourceGroupName $rgname -VMScaleSetName "VMSSKArt" -Vi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzVmss], ComputeCloudException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.Automation.NewAzureRmVmss
You have a mistake in your $ipConfig, it should be the following:
$ipConfig = New-AzVmssIpConfig `
-Name "myIPConfig" `
-LoadBalancerInboundNatPoolsId $lb.InboundNatPools[0].id `
-LoadBalancerBackendAddressPoolsId $lb.BackendAddressPools[0].Id `
-SubnetId $vnet.Subnets[0].Id
so actually 2 mistakes :)
ps. figured that out with the following:
New-AzVMss -ResourceGroupName $rgname -VMScaleSetName VMSSKArt -VirtualMachineScaleSet $vmconfig -Debug

Azure Funtion: Powershell command New-AzureRmVM never returns

I've got a powershell script that I use to build a VM.
# Variables for common values
$resourceGroup = "AAA-Production3"
$location = "West US 2"
$vmName = "AAA-Prod-SVR1"
$SubnetName = "AAA-PROD-SUBNET01"
$NamevNET = "AAA-PROD-VNET"
$Namepublicdns = "AAA-PROD-ADF01-IP01"
$NameNetworkSecurityGroupRuleRDP = 'Default-allow-rdp'
$NameNetworkSecurityGroup = 'AAA-SVR1-NSG'
$NameVNic = "AAA-PROD-VNIC01"
$VMSize = 'Standard_D1_v2'
# Create user object
$cred = Get-Credential -Message "Enter a username and password for the virtual machine."
New-AzureRmResourceGroup -Name $resourceGroup -Location $location
$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix 10.50.1.0/24
$vnet = New-AzureRmVirtualNetwork -ResourceGroupName $resourceGroup -Location $location `
-Name $NamevNET -AddressPrefix 10.50.1.0/24 -Subnet $subnetConfig
# Create a public IP address and specify a DNS name
$pip = New-AzureRmPublicIpAddress -ResourceGroupName $resourceGroup -Location $location `
-Name "$Namepublicdns$(Get-Random)" -AllocationMethod Static -IdleTimeoutInMinutes 4
# Create an inbound network security group rule for port 3389
$nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig -Name $NameNetworkSecurityGroupRuleRDP -Protocol Tcp `
-Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
-DestinationPortRange 3389 -Access Allow
# Create a network security group
$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName $resourceGroup -Location $location `
-Name $NameNetworkSecurityGroup -SecurityRules $nsgRuleRDP
# Create a virtual network card and associate with public IP address and NSG
$nic = New-AzureRmNetworkInterface -Name $NameVNic -ResourceGroupName $resourceGroup -Location $location `
-SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id
# Create a virtual machine configuration
$vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize $VMSize | `
Set-AzureRmVMOperatingSystem -Windows -ComputerName $vmName -Credential $cred | `
Set-AzureRmVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2016-Datacenter -Version latest | `
Add-AzureRmVMNetworkInterface -Id $nic.Id
# Create a virtual machine
New-AzureRmVM -ResourceGroupName $resourceGroup -Location $location -VM $vmConfig
Everything runs fine, but when it gets to the end, the New-AzureRmVM script never returns or exits. If I disconnect my machine from the net, I'll get a message about a long running process, but other than that, the shell appears to be locked up.
Any ideas how to get the command to exit so I can reuse the shell?

How to create and attach a Unmanged disk in Azure VM through powershell scripts? [duplicate]

I'm trying to create an Azure VM with an unmanaged disk via PowerShell since managed disks aren't supported in Azure Government yet.
None of the documentation I could find for PowerShell VM creation references managed or unmanaged disks and the default seems to be managed disks. My VM creation fails with the following error:
New-AzureRmVM : Managed Disks are not supported in this region.
ErrorCode: BadRequest
Here's the script I'm using:
$location = "USGovTexas"
New-AzureRmResourceGroup -Name myResourceGroup -Location $location
# Create a subnet configuration
$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name mySubnet -AddressPrefix 192.168.1.0/24
# Create a virtual network
$vnet = New-AzureRmVirtualNetwork -ResourceGroupName myResourceGroup -Location $location `
-Name MYvNET -AddressPrefix 192.168.0.0/16 -Subnet $subnetConfig
# Create a public IP address and specify a DNS name
$pip = New-AzureRmPublicIpAddress -ResourceGroupName myResourceGroup -Location $location `
-AllocationMethod Static -IdleTimeoutInMinutes 4 -Name "mypublicdns$(Get-Random)"
# Create an inbound network security group rule for port 3389
$nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleRDP -Protocol Tcp `
-Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
-DestinationPortRange 3389 -Access Allow
# Create an inbound network security group rule for port 80
$nsgRuleWeb = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleWWW -Protocol Tcp `
-Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
-DestinationPortRange 80 -Access Allow
# Create a network security group
$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName myResourceGroup -Location $location `
-Name myNetworkSecurityGroup -SecurityRules $nsgRuleRDP,$nsgRuleWeb
# Create a virtual network card and associate with public IP address and NSG
$nic = New-AzureRmNetworkInterface -Name myNic -ResourceGroupName myResourceGroup -Location $location `
-SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id
# Define a credential object
$cred = Get-Credential
# Create a virtual machine configuration
$vmConfig = New-AzureRmVMConfig -VMName myVM -VMSize Standard_DS2_v2 | `
Set-AzureRmVMOperatingSystem -Windows -ComputerName myVM -Credential $cred | `
Set-AzureRmVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer `
-Skus 2016-Datacenter -Version latest | Add-AzureRmVMNetworkInterface -Id $nic.Id
# Create the virtual machine
New-AzureRmVM -ResourceGroupName myResourceGroup -Location $location -VM $vmConfig
We can use this script to create Azure windows VM with unmanaged disk:
$location = "USGovTexas"
$rgname = "myResourceGroup"
New-AzureRmResourceGroup -Name myResourceGroup -Location $location
# Create a subnet configuration
$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name mySubnet -AddressPrefix 192.168.1.0/24
# Create a virtual network
$vnet = New-AzureRmVirtualNetwork -ResourceGroupName myResourceGroup -Location $location `
-Name MYvNET -AddressPrefix 192.168.0.0/16 -Subnet $subnetConfig
# Create a public IP address and specify a DNS name
$pip = New-AzureRmPublicIpAddress -ResourceGroupName myResourceGroup -Location $location `
-AllocationMethod Static -IdleTimeoutInMinutes 4 -Name "mypublicdns$(Get-Random)"
# Create an inbound network security group rule for port 3389
$nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleRDP -Protocol Tcp `
-Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
-DestinationPortRange 3389 -Access Allow
# Create an inbound network security group rule for port 80
$nsgRuleWeb = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleWWW -Protocol Tcp `
-Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
-DestinationPortRange 80 -Access Allow
# Create a network security group
$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName myResourceGroup -Location $location `
-Name myNetworkSecurityGroup -SecurityRules $nsgRuleRDP,$nsgRuleWeb
# Create a virtual network card and associate with public IP address and NSG
$nic = New-AzureRmNetworkInterface -Name myNic -ResourceGroupName myResourceGroup -Location $location `
-SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id
# Define a credential object
$cred = Get-Credential
#VM config
$vmsize = "Standard_DS2"
$vmName="myVM"
$vm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
$pubName = ”MicrosoftWindowsServer”
$offerName = ”WindowsServer”
$skuName = ”2016-Datacenter”
$vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $vmName -Credential $cred
$vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName $pubName -Offer $offerName -Skus $skuName -Version "latest"
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $NIC.Id
# Create a new storage account
New-AzureRmStorageAccount -ResourceGroupName "MyResourceGroup" -AccountName "MyStorageAccount" -Location $location -SkuName "Standard_LRS"
# Disk setup
$diskName = ”jason-disk”
$storageaccount = "jasontest321"
$STA = Get-AzureRmStorageAccount -ResourceGroupName $rgName -Name $storageAccount
$OSDiskUri = $STA.PrimaryEndpoints.Blob.ToString() + "vhds/" + $diskName? + ".vhd"
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $OSDiskUri -CreateOption fromImage
# Create the virtual machine
New-AzureRmVM -ResourceGroupName myResourceGroup -Location $location -VM $vm

Deploy VM on Azure using created (generalized/sysprep'ed) snapshot stored on Blob

I need someone more experienced with VM's on Azure. I have prepared image with Win2016 Server and few additional configurations. I have done sysprep and created image and snapshot after that. When I create a new machine using this image, machine fails and don't event start. So I have found another solution: snapshot taken from this machine and transferred to blob. A new machine after below code execution starts but i am not able to connect RDP...
error: VM deployed with error: New-AzureRmVM : Long running operation failed with status 'Failed'
$resourceGroupName="<resource-group>"
$subscriptionId = "<subs>"
$tenantId = "<tenant>"
$location = "West Europe"
# sign in
Write-Host "Logging in...";
Login-AzureRmAccount -TenantID $tenantID;
# select subscription
Write-Host "Selecting subscription '$subscriptionId'";
Select-AzureRmSubscription -SubscriptionID $subscriptionId;
# Create a Resource Group
New-AzureRmResourceGroup -Name $resourceGroupName -Location $location
#Prepare the VM parameters
$rgName = $resourceGroupName
# Get the Azure location and storage account names
$locName=(Get-AzureRmResourceGroup -Name $rgName).Location
$exSubnet=New-AzureRMVirtualNetworkSubnetConfig -Name EX2016Subnet -AddressPrefix 10.0.0.0/24
New-AzureRMVirtualNetwork -Name EX2016Vnet -ResourceGroupName $rgName -Location $locName -AddressPrefix 10.0.0.0/16 -Subnet $exSubnet -DNSServer 10.0.0.5
$rule1 = New-AzureRMNetworkSecurityRuleConfig -Name "RDPTraffic" -Description "Allow RDP to all VMs on the subnet" -Access Allow -Protocol Tcp -Direction Inbound -Priority 100 -SourceAddressPrefix Internet -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389
$rule2 = New-AzureRMNetworkSecurityRuleConfig -Name "ExchangeSecureWebTraffic" -Description "Allow HTTPS to the Exchange server" -Access Allow -Protocol Tcp -Direction Inbound -Priority 101 -SourceAddressPrefix Internet -SourcePortRange * -DestinationAddressPrefix "10.0.0.5/32" -DestinationPortRange 443
$rule3 = New-AzureRMNetworkSecurityRuleConfig -Name "ExchangeSecurePortExternal" -Description "Allow access port 25 to the Exchange server" -Access Allow -Protocol Tcp -Direction Inbound -Priority 102 -SourceAddressPrefix Internet -SourcePortRange * -DestinationAddressPrefix "10.0.0.5/32" -DestinationPortRange 25
New-AzureRMNetworkSecurityGroup -Name EX2016Subnet -ResourceGroupName $rgName -Location $locName -SecurityRules $rule1, $rule2,$rule3
$vnet=Get-AzureRMVirtualNetwork -ResourceGroupName $rgName -Name EX2016Vnet
$nsg=Get-AzureRMNetworkSecurityGroup -Name EX2016Subnet -ResourceGroupName $rgName
Set-AzureRMVirtualNetworkSubnetConfig -VirtualNetwork $vnet -Name EX2016Subnet -AddressPrefix "10.0.0.0/24" -NetworkSecurityGroup $nsg
# Specify the virtual machine name and size
$vmName="exVM"
$vmSize="Standard_D3_v2"
$vnet=Get-AzureRMVirtualNetwork -Name "EX2016Vnet" -ResourceGroupName $rgName
$vm=New-AzureRMVMConfig -VMName $vmName -VMSize $vmSize
# Create the NIC for the virtual machine
$nicName=$vmName + "-NIC"
$pipName=$vmName + "-PublicIP"
$pip=New-AzureRMPublicIpAddress -Name $pipName -ResourceGroupName $rgName -DomainNameLabel $vmDNSName -Location $locName -AllocationMethod Dynamic
$nic=New-AzureRMNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $locName -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -PrivateIpAddress "10.0.0.5"
# Specify the image and local administrator account, and then add the NIC
$cred=Get-Credential -Message "Type the name and password of the local administrator account for exVM."
$osDiskName = "VM01-OSDisk"
$osDiskUri = "https://xxxxxx.blob.core.windows.net/images/Image.vhd"
$storageAccountType = "StandardLRS"
$vm=Add-AzureRMVMNetworkInterface -VM $vm -Id $nic.Id
$osDisk = New-AzureRmDisk -DiskName $osDiskName -Disk (New-AzureRmDiskConfig -AccountType $storageAccountType -Location $location -CreateOption Import -SourceUri $osDiskUri) -ResourceGroupName $rgName
$vm = Set-AzureRmVMOSDisk -VM $vm -ManagedDiskId $osDisk.Id -StorageAccountType $storageAccountType -DiskSizeInGB 128 -CreateOption Attach -Windows
$vm = Set-AzureRmVMBootDiagnostics -VM $vm -disable
#Create the new VM
New-AzureRmVM -ResourceGroupName $rgName -Location $location -VM $vm
You could use the blob to create a snapshot and use the snapshot to create a new VM, you could check this example.
$snapshot = Get-AzureRmSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName
$diskConfig = New-AzureRmDiskConfig -AccountType $storageType -Location $snapshot.Location -SourceResourceId $snapshot.Id -CreateOption Copy
$disk = New-AzureRmDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $osDiskName
#Initialize virtual machine configuration
$VirtualMachine = New-AzureRmVMConfig -VMName $virtualMachineName -VMSize $virtualMachineSize
#Use the Managed Disk Resource Id to attach it to the virtual machine. Please change the OS type to linux if OS disk has linux OS
$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -ManagedDiskId $disk.Id -CreateOption Attach -Linux
#Create a public IP for the VM
$publicIp = New-AzureRmPublicIpAddress -Name ($VirtualMachineName.ToLower()+'_ip') -ResourceGroupName $resourceGroupName -Location $snapshot.Location -AllocationMethod Dynamic
#Get the virtual network where virtual machine will be hosted
$vnet = Get-AzureRmVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $resourceGroupName
# Create NIC in the first subnet of the virtual network
$nic = New-AzureRmNetworkInterface -Name ($VirtualMachineName.ToLower()+'_nic') -ResourceGroupName $resourceGroupName -Location $snapshot.Location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $publicIp.Id
$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $nic.Id
#Create the virtual machine with Managed Disk
New-AzureRmVM -VM $VirtualMachine -ResourceGroupName $resourceGroupName -Location $snapshot.Location
You also could check this link to create snapshot from a blob.

Create an Azure VM with an unmanaged disk

I'm trying to create an Azure VM with an unmanaged disk via PowerShell since managed disks aren't supported in Azure Government yet.
None of the documentation I could find for PowerShell VM creation references managed or unmanaged disks and the default seems to be managed disks. My VM creation fails with the following error:
New-AzureRmVM : Managed Disks are not supported in this region.
ErrorCode: BadRequest
Here's the script I'm using:
$location = "USGovTexas"
New-AzureRmResourceGroup -Name myResourceGroup -Location $location
# Create a subnet configuration
$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name mySubnet -AddressPrefix 192.168.1.0/24
# Create a virtual network
$vnet = New-AzureRmVirtualNetwork -ResourceGroupName myResourceGroup -Location $location `
-Name MYvNET -AddressPrefix 192.168.0.0/16 -Subnet $subnetConfig
# Create a public IP address and specify a DNS name
$pip = New-AzureRmPublicIpAddress -ResourceGroupName myResourceGroup -Location $location `
-AllocationMethod Static -IdleTimeoutInMinutes 4 -Name "mypublicdns$(Get-Random)"
# Create an inbound network security group rule for port 3389
$nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleRDP -Protocol Tcp `
-Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
-DestinationPortRange 3389 -Access Allow
# Create an inbound network security group rule for port 80
$nsgRuleWeb = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleWWW -Protocol Tcp `
-Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
-DestinationPortRange 80 -Access Allow
# Create a network security group
$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName myResourceGroup -Location $location `
-Name myNetworkSecurityGroup -SecurityRules $nsgRuleRDP,$nsgRuleWeb
# Create a virtual network card and associate with public IP address and NSG
$nic = New-AzureRmNetworkInterface -Name myNic -ResourceGroupName myResourceGroup -Location $location `
-SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id
# Define a credential object
$cred = Get-Credential
# Create a virtual machine configuration
$vmConfig = New-AzureRmVMConfig -VMName myVM -VMSize Standard_DS2_v2 | `
Set-AzureRmVMOperatingSystem -Windows -ComputerName myVM -Credential $cred | `
Set-AzureRmVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer `
-Skus 2016-Datacenter -Version latest | Add-AzureRmVMNetworkInterface -Id $nic.Id
# Create the virtual machine
New-AzureRmVM -ResourceGroupName myResourceGroup -Location $location -VM $vmConfig
We can use this script to create Azure windows VM with unmanaged disk:
$location = "USGovTexas"
$rgname = "myResourceGroup"
New-AzureRmResourceGroup -Name myResourceGroup -Location $location
# Create a subnet configuration
$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name mySubnet -AddressPrefix 192.168.1.0/24
# Create a virtual network
$vnet = New-AzureRmVirtualNetwork -ResourceGroupName myResourceGroup -Location $location `
-Name MYvNET -AddressPrefix 192.168.0.0/16 -Subnet $subnetConfig
# Create a public IP address and specify a DNS name
$pip = New-AzureRmPublicIpAddress -ResourceGroupName myResourceGroup -Location $location `
-AllocationMethod Static -IdleTimeoutInMinutes 4 -Name "mypublicdns$(Get-Random)"
# Create an inbound network security group rule for port 3389
$nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleRDP -Protocol Tcp `
-Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
-DestinationPortRange 3389 -Access Allow
# Create an inbound network security group rule for port 80
$nsgRuleWeb = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleWWW -Protocol Tcp `
-Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
-DestinationPortRange 80 -Access Allow
# Create a network security group
$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName myResourceGroup -Location $location `
-Name myNetworkSecurityGroup -SecurityRules $nsgRuleRDP,$nsgRuleWeb
# Create a virtual network card and associate with public IP address and NSG
$nic = New-AzureRmNetworkInterface -Name myNic -ResourceGroupName myResourceGroup -Location $location `
-SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id
# Define a credential object
$cred = Get-Credential
#VM config
$vmsize = "Standard_DS2"
$vmName="myVM"
$vm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
$pubName = ”MicrosoftWindowsServer”
$offerName = ”WindowsServer”
$skuName = ”2016-Datacenter”
$vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $vmName -Credential $cred
$vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName $pubName -Offer $offerName -Skus $skuName -Version "latest"
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $NIC.Id
# Create a new storage account
New-AzureRmStorageAccount -ResourceGroupName "MyResourceGroup" -AccountName "MyStorageAccount" -Location $location -SkuName "Standard_LRS"
# Disk setup
$diskName = ”jason-disk”
$storageaccount = "jasontest321"
$STA = Get-AzureRmStorageAccount -ResourceGroupName $rgName -Name $storageAccount
$OSDiskUri = $STA.PrimaryEndpoints.Blob.ToString() + "vhds/" + $diskName? + ".vhd"
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $OSDiskUri -CreateOption fromImage
# Create the virtual machine
New-AzureRmVM -ResourceGroupName myResourceGroup -Location $location -VM $vm

Resources