Skip to content

Instantly share code, notes, and snippets.

@rizky
Last active January 25, 2017 04:28
Show Gist options
  • Save rizky/be6964d85ea7ba5463eaa9684fde0a7a to your computer and use it in GitHub Desktop.
Save rizky/be6964d85ea7ba5463eaa9684fde0a7a to your computer and use it in GitHub Desktop.
Create VM Azure with Multiple IP Adresses
# Register for the preview by sending an email to Multiple IPs with your subscription ID and intended use.
# Do not attempt to complete the remaining steps:
# - Until you receive an e-mail notifying you that you've been accepted into the preview
# - Without following the instructions in the email you receive
# Login to Your Azure account
$path= "AzureProfile.json"
Select-AzureRmProfile -path $path
$subscription-name = "BizPark"
Get-AzureRmSubscription -SubscriptionName $subscription-name | Select-AzureRmSubscription
# 1. Create a resource group
# Set the location for the resources. This command sets the location to centralus
$location = "northeurope"
# Create a resource group. This command creates the resource group named myResourceGroup in the location that you set.
$myResourceGroup = "myResource"
New-AzureRmResourceGroup -Name $myResourceGroup -Location $location
# 2: Create a storage accountsudo -i
# Test the storage account name for uniqueness. This command tests the name myStorageAccount.
$myStorageAccountName = "mystorage"
# create the storage account.
$myStorageAccount = New-AzureRmStorageAccount -ResourceGroupName $myResourceGroup -Name $myStorageAccountName -SkuName "Standard_LRS" -Kind "Storage" -Location $location
# 3: Create a virtual network
# Create a subnet for the virtual network. This command creates a subnet named mySubnet with an address prefix of 10.0.0.0/24.
$mySubnet = New-AzureRmVirtualNetworkSubnetConfig -Name "mySubnet" -AddressPrefix 10.0.0.0/24
# creates a virtual network named myVnet using the subnet that you created and an address prefix of 10.0.0.0/16.
$myVnet = New-AzureRmVirtualNetwork -Name "myVnet" -ResourceGroupName $myResourceGroup -Location $location -AddressPrefix 10.0.0.0/16 -Subnet $mySubnet
# 4:Create a VM with multiple IP addresses
# Create a variable to store the subnet object created in Step 4 (Create a VNet) of the Create a Windows VM article by typing the following command:
$SubnetName = $mySubnet.Name
$Subnet = $myVnet.Subnets | Where-Object { $_.Name -eq $SubnetName }
# Define the IP configurations you want to assign to the NIC. You can add, remove, or change the configurations as necessary. The following configurations are described in the scenario:
# IPConfig-1
# Enter the commands that follow to create:
# A public IP address resource with a static public IP address
# An IP configuration with the public IP address resource and a dynamic private IP address
$myPublicIp1 = New-AzureRmPublicIpAddress -Name "myPublicIp1" -ResourceGroupName $myResourceGroup -Location $location -AllocationMethod Static
$IpConfigName1 = "IPConfig-1"
$IpConfig1 = New-AzureRmNetworkInterfaceIpConfig -Name $IpConfigName1 -Subnet $Subnet -PublicIpAddress $myPublicIp1 -Primary
# IPConfig-2
# Change the value of the $IPAddress variable that follows to an available, valid address on the subnet you created.
# To check whether the address 10.0.0.5 is available on the subnet, enter the command
# Test-AzureRmPrivateIPAddressAvailability -IPAddress 10.0.0.5 -VirtualNetwork $myVnet.
# If the address is available, the output returns True. If it's not available, the output returns False and a list of addresses that are available.
# Enter the following commands to create a new public IP address resource and a new IP configuration with a static public IP address and a static private IP address:
$IpConfigName2 = "IPConfig-2"
$IPAddress = 10.0.0.5
$myPublicIp2 = New-AzureRmPublicIpAddress -Name "myPublicIp2" -ResourceGroupName $myResourceGroup -Location $location -AllocationMethod Static
$IpConfig2 = New-AzureRmNetworkInterfaceIpConfig -Name $IpConfigName2 -Subnet $Subnet -PrivateIpAddress $IPAddress -PublicIpAddress $myPublicIp2
# IPConfig-3
# Enter the following commands to create an IP configuration with a dynamic private IP address and no public IP address:
# $IpConfigName3 = "IpConfig-3"
# $IpConfig3 = New-AzureRmNetworkInterfaceIpConfig -Name $IPConfigName3 -Subnet $Subnet
# Create the NIC using the IP configurations defined in the previous step by entering the following command:
# $myNIC = New-AzureRmNetworkInterface -Name myNIC -ResourceGroupName $myResourceGroup -Location $location -IpConfiguration $IpConfig1,$IpConfig2,$IpConfig3
$myNIC = New-AzureRmNetworkInterface -Name myNIC -ResourceGroupName $myResourceGroup -Location $location -IpConfiguration $IpConfig1,$IpConfig2
# 5: Create a virtual machine
# Run this command to set the administrator account name and password for the virtual machine.
# $cred = Get-Credential -Message "Type the name and password of the local administrator account."
$SecurePassword = ConvertTo-SecureString "password" -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ("username", $SecurePassword);
# Create the configuration object for the virtual machine. This command creates a configuration object named myVmConfig that defines the name of the VM and the size of the VM.
$myVm = New-AzureRmVMConfig -VMName "myVM" -VMSize "Standard_DS1_v2"
# Configure operating system settings for the VM. This command sets the computer name, operating system type, and account credentials for the VM.
$myVM = Set-AzureRmVMOperatingSystem -VM $myVM -Linux -ComputerName "myComputer" -Credential $Credential
# Define the image to use to provision the VM. This command defines the Windows Server image to use for the VM.
$myVM = Set-AzureRmVMSourceImage -VM $myVM -PublisherName "Canonical" -Offer "UbuntuServer" -Skus "14.04.4-LTS" -Version "latest"
# Add the network interface that you created to the configuration.
$myVM = Add-AzureRmVMNetworkInterface -VM $myVM -Id $myNIC.Id
# Define the name and location of the VM hard disk. The virtual hard disk file is stored in a container. This command creates the disk in a container named vhds/myOsDisk1.vhd in the storage account that you created.
$blobPath = "vhds/myOsDisk1.vhd"
$osDiskUri = $myStorageAccount.PrimaryEndpoints.Blob.ToString() + $blobPath
# Add the operating system disk information to the VM configuration. Replace The value of $diskName with a name for the operating system disk. Create the variable and add the disk information to the configuration.
$myVM = Set-AzureRmVMOSDisk -VM $myVM -Name "myOsDisk1" -VhdUri $osDiskUri -CreateOption fromImage
# Finally, create the virtual machine.
New-AzureRmVM -ResourceGroupName $myResourceGroup -Location $location -VM $myVM
# Enter the following command to view the private IP addresses and public IP address resources assigned to the NIC:
$myNIC.IpConfigurations | Format-Table Name, PrivateIPAddress, PublicIPAddress, Primary
# 6: Add IP addresses to a VM
# Create VM: https://github.com/Microsoft/azure-docs/blob/master/articles/virtual-machines/virtual-machines-windows-ps-create.md
# Multiple IPs: https://docs.microsoft.com/en-us/azure/virtual-network/virtual-network-multiple-ip-addresses-portal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment