Showing posts with label ARM. Show all posts
Showing posts with label ARM. Show all posts

Thursday, August 11, 2016

ARM PowerShell to create VM with existing Static Public IP

Static public IP is same as reserved IP in Azure classic. Once you create and assi

1 Create Static Public IP
        #Add Azure Account
         Add-AzureRMAccount
        #Set Subscription Context
        Get-AzureRmSubscription -SubscriptionName 'Visual Studio Professional with MSDN'| Set-AzureRmContext
        #RG Name
        $rgName = "DemoRG"
        #Location
        $locname = "West Europe"
        #Public IP Name
        $pipname = "MyPubIP"
       #DNS Name
       $domainname ="scriptdemo"
       #IP Allocation Method
       $ipallocation ="Static"
       #Create Azure Resource Group
       New-AzureRmResourceGroup -Name $rgName -Location $locname
       #Create Public IP
      New-AzureRmPublicIpAddress -Name $pipname -ResourceGroupName $rgName -DomainNameLabel $domainname -Location $locName -AllocationMethod $ipallocation
       #List Public IP
   Get-AzureRMPublicIPAddress

2. Create VM with existing Static Public IP
   #Storage Account Name
    $stName = "testst"
   #Deployment location
    $locName = "West Europe"
    #Resource Group Name
    $rgName = "DemoRG"
    #Public IP Name
    $pipname ="MyPubIP"
    #NIC Name
    $testnic ="TestNIC"
    #VM Name
    $vmname = "web"
    #Private IP
    $ipv = "10.0.0.5"
    #VNET
    $vnetName = "DemoVNET"
   #Subscription Credentials
    $cred = Get-Credential -Message "Type the name and password of the local administrator account."
   #Get VNET
    $vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $rgName -Name $vnetName
    #Create Storage
    $storageAcc =new-AzureRmStorageAccount -ResourceGroupName $rgName -AccountName $stName -Type "Standard_RAGRS" -Location $locName
    #Get Subnet
    $subnet = $vnet.Subnets[0].Id  
     #Create NIC
    $nic = New-AzureRmNetworkInterface -Name $testnic -ResourceGroupName $rgName -Location $locName -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -PrivateIpAddress $ipv
    #Create VM
    $vm = New-AzureRmVMConfig -VMName $vmname -VMSize "Standard_A1"
    $vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName  $vmname  -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
    $vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2012-R2-Datacenter -Version "latest"
    $vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
    $osDiskUri = $storageAcc.PrimaryEndpoints.Blob.ToString() + "vhds/WindowsVMosDisk.vhd"
    $vm = Set-AzureRmVMOSDisk -VM $vm -Name "windowsvmosdisk" -VhdUri $osDiskUri -CreateOption fromImage