|
| 1 | +--- |
| 2 | +title: Configure routing preference for a VM - Azure PowerShell |
| 3 | +description: Learn how to create a VM with a public IP address with routing preference choice using the Azure PowerShell. |
| 4 | +services: virtual-network |
| 5 | +documentationcenter: na |
| 6 | +author: KumudD |
| 7 | +manager: mtillman |
| 8 | +ms.service: virtual-network |
| 9 | +ms.devlang: na |
| 10 | +ms.topic: conceptual |
| 11 | +ms.tgt_pltfrm: na |
| 12 | +ms.workload: infrastructure-services |
| 13 | +ms.date: 05/18/2020 |
| 14 | +ms.author: mnayak |
| 15 | + |
| 16 | +--- |
| 17 | +# Configure routing preference for a VM using Azure PowerShell |
| 18 | + |
| 19 | +This article shows you how to configure routing preference for a virtual machine. Internet bound traffic from the VM will be routed via the ISP network when you choose **Internet** as your routing preference option . The default routing is via the Microsoft global network. |
| 20 | + |
| 21 | +This article shows you how to create a virtual machine with a public IP that is set to route traffic via the ISP network using Azure PowerShell. |
| 22 | + |
| 23 | +> [!IMPORTANT] |
| 24 | +> Routing preference is currently in public preview. |
| 25 | +> This preview version is provided without a service level agreement, and it's not recommended for production workloads. Certain features might not be supported or might have constrained capabilities. |
| 26 | +> For more information, see [Supplemental Terms of Use for Microsoft Azure Previews](https://azure.microsoft.com/support/legal/preview-supplemental-terms/). |
| 27 | +
|
| 28 | +## Register the feature for your subscription |
| 29 | +The Routing Preference feature is currently in preview. Register the feature for your subscription as follows: |
| 30 | +```azurepowershell |
| 31 | +Register-AzProviderFeature -FeatureName AllowRoutingPreferenceFeature ProviderNamespace Microsoft.Network |
| 32 | +``` |
| 33 | + |
| 34 | +## Create a resource group |
| 35 | +1. If using the Cloud Shell, skip to step 2. Open a command session and sign into Azure with `Connect-AzAccount`. |
| 36 | +2. Create a resource group with the [New-AzResourceGroup](/powershell/module/az.resources/new-azresourcegroup) command. The following example creates a resource group in the East US Azure region: |
| 37 | + |
| 38 | + ```azurepowershell |
| 39 | + $rg = New-AzResourceGroup -Name MyResourceGroup -Location EastUS |
| 40 | + ``` |
| 41 | +
|
| 42 | +## Create a public IP address |
| 43 | +
|
| 44 | +To access your virtual machines from the Internet, you need a public IP addresses. Create public IP addresses with [New-AzPublicIpAddress](/powershell/module/az.network/new-azpublicipaddress). The following example creates a IPv4 public IP address named *MyPublicIP* routing preference type *Internet* in the *MyResourceGroup* resource group in *East US* region: |
| 45 | +
|
| 46 | +```azurepowershell-interactive |
| 47 | +$iptagtype="RoutingPreference" |
| 48 | +$tagName = "Internet" |
| 49 | +$ipTag = New-AzPublicIpTag -IpTagType $iptagtype -Tag $tagName |
| 50 | +# attach the tag |
| 51 | +$publicIp = New-AzPublicIpAddress ` |
| 52 | +-Name "MyPublicIP" ` |
| 53 | +-ResourceGroupName $rg.ResourceGroupName ` |
| 54 | +-Location $rg.Location ` |
| 55 | +-IpTag $ipTag ` |
| 56 | +-AllocationMethod Static ` |
| 57 | +-Sku Standard ` |
| 58 | +-IpAddressVersion IPv4 |
| 59 | +``` |
| 60 | + |
| 61 | +## Create network resources |
| 62 | + |
| 63 | +Before you deploy a VM, you must create supporting network resources - network security group, virtual network, and virtual NIC. |
| 64 | + |
| 65 | +### Create a network security group |
| 66 | + |
| 67 | +Create a network security group with [New-AzNetworkSecurityGroup](/powershell/module/az.network/new-aznetworksecuritygroup). The following example creates a NSG named *myNSG* |
| 68 | + |
| 69 | +```azurepowershell |
| 70 | +$nsg = New-AzNetworkSecurityGroup ` |
| 71 | +-ResourceGroupName $rg.ResourceGroupName ` |
| 72 | +-Location $rg.Location ` |
| 73 | +-Name "myNSG" |
| 74 | +``` |
| 75 | + |
| 76 | +### Create a virtual network |
| 77 | + |
| 78 | +Create a virtual network with [New-AzVirtualNetwork](/powershell/module/az.network/new-azvirtualnetwork). The following example creates a virtual network named *myVNET* with *mySubNet*: |
| 79 | + |
| 80 | +### Create a subnet |
| 81 | + |
| 82 | +```azurepowershell |
| 83 | +$subnet = New-AzVirtualNetworkSubnetConfig ` |
| 84 | +-Name "mySubnet" ` |
| 85 | +-AddressPrefix "10.0.0.0/24" |
| 86 | +``` |
| 87 | + |
| 88 | +```azurepowershell |
| 89 | +# Create a virtual network |
| 90 | +$vnet = New-AzVirtualNetwork ` |
| 91 | +-ResourceGroupName $rg.ResourceGroupName ` |
| 92 | +-Location $rg.Location ` |
| 93 | +-Name "myVNET" ` |
| 94 | +-AddressPrefix "10.0.0.0/16" ` |
| 95 | +-Subnet $subnet |
| 96 | +``` |
| 97 | + |
| 98 | +### Create a NIC |
| 99 | + |
| 100 | +Create virtual NICs with [New-AzNetworkInterface](/powershell/module/az.network/new-aznetworkinterface. The following example creates a virtual NIC. |
| 101 | + |
| 102 | +```azurepowershell |
| 103 | +# Create an IP Config |
| 104 | +$ipconfig=New-AzNetworkInterfaceIpConfig ` |
| 105 | +-Name myIpConfig ` |
| 106 | +-Subnet $vnet.subnets[0] ` |
| 107 | +-PrivateIpAddressVersion IPv4 ` |
| 108 | +-PublicIpAddress $publicIp |
| 109 | +
|
| 110 | +# Create a NIC |
| 111 | +$nic = New-AzNetworkInterface ` |
| 112 | +-Name "mynic" ` |
| 113 | +-ResourceGroupName $rg.ResourceGroupName ` |
| 114 | +-Location $rg.Location ` |
| 115 | +-NetworkSecurityGroupId $nsg.Id ` |
| 116 | +-IpConfiguration $ipconfig |
| 117 | +``` |
| 118 | + |
| 119 | +## Create a virtual machine |
| 120 | + |
| 121 | +Set an administrator username and password for the VMs with [Get-Credential](https://msdn.microsoft.com/powershell/reference/5.1/microsoft.powershell.security/Get-Credential): |
| 122 | + |
| 123 | +```azurepowershell |
| 124 | + $cred = get-credential -Message "Routing Preference SAMPLE: Please enter the Administrator credential to log into the VM." |
| 125 | +``` |
| 126 | + |
| 127 | +Now you can create the VM with [New-AzVM](/powershell/module/az.compute/new-azvm). The following example creates two VMs and the required virtual network components if they do not already exist. |
| 128 | + |
| 129 | +```azurepowershell |
| 130 | + $vmsize = "Standard_A2" |
| 131 | + $ImagePublisher = "MicrosoftWindowsServer" |
| 132 | + $imageOffer = "WindowsServer" |
| 133 | + $imageSKU = "2019-Datacenter" |
| 134 | +
|
| 135 | + $vmName= "myVM" |
| 136 | + $vmconfig = New-AzVMConfig -VMName $vmName -VMSize $vmsize | Set-AzVMOperatingSystem -Windows -ComputerName $vmName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate | Set-AzVMSourceImage -PublisherName $ImagePublisher -Offer $imageOffer -Skus $imageSKU -Version "latest" | Set-AzVMOSDisk -Name "$vmName.vhd" -CreateOption "FromImage" | Add-AzVMNetworkInterface -Id $nic.Id |
| 137 | + $VM1 = New-AzVM -ResourceGroupName $rg.ResourceGroupName -Location $rg.Location -VM $vmconfig |
| 138 | +``` |
| 139 | + |
| 140 | +## Allow network traffic to the VM |
| 141 | + |
| 142 | +Before you can connect to the public IP address from the internet, ensure that you have the necessary ports open in any network security group that you might have associated to the network interface, the subnet the network interface is in, or both. You can view the effective security rules for a network interface and its subnet using the [Portal](diagnose-network-traffic-filter-problem.md#diagnose-using-azure-portal), [CLI](diagnose-network-traffic-filter-problem.md#diagnose-using-azure-cli), or [PowerShell](diagnose-network-traffic-filter-problem.md#diagnose-using-powershell). |
| 143 | + |
| 144 | +## Clean up resources |
| 145 | + |
| 146 | +When no longer needed, you can use the [Remove-AzResourceGroup](/powershell/module/az.resources/remove-azresourcegroup) command to remove the resource group, VM, and all related resources. |
| 147 | + |
| 148 | + ```azurepowershell |
| 149 | + Remove-AzResourceGroup -Name MyResourceGroup |
| 150 | +``` |
| 151 | + |
| 152 | +## Next steps |
| 153 | + |
| 154 | +* Learn more about [routing preference in public IP addresses](routing-preference-overview.md). |
| 155 | +* Learn more about [public IP addresses](virtual-network-ip-addresses-overview-arm.md#public-ip-addresses) in Azure. |
| 156 | +* Learn more about [public IP address settings](virtual-network-public-ip-address.md#create-a-public-ip-address). |
0 commit comments