#Get Exiting Vnet $vnet = Get-AzVirtualNetwork -ResourceGroupName Devmanh_RG -Name Devmanh_VNet #Get existing subnet $subnet=$vnet.Subnets[0] #create new Public IP $pip = New-AzPublicIpAddress -ResourceGroupName Devmanh_RG -Location southeastasia -Name Devmanh_AG_PIPAddress -AllocationMethod Dynamic #Get public IP for AG $pip = Get-AzPublicIpAddress -ResourceGroupName Devmanh_RG -Name Devmanh_AG_PIPAddress #Create Application Gateway IP configuration $gipconfig = New-AzApplicationGatewayIPConfiguration -Name Devmanh_AG_IPConfig -Subnet $subnet #Create new AG frontend IP config $fipconfig = New-AzApplicationGatewayFrontendIPConfig -Name Devmanh_AG_FrontendIPConfig -PublicIPAddress $pip #create new front end port $frontendport = New-AzApplicationGatewayFrontendPort -Name Devmanh_AG_FrontendPort -Port 80 #Create Default Backendpool $defaultPool = New-AzApplicationGatewayBackendAddressPool -Name appGatewayBackendPool #Create Default Httpsetting for backendpool $poolSettings = New-AzApplicationGatewayBackendHttpSettings -Name Devmanh_PoolSettings -Port 80 -Protocol Http -CookieBasedAffinity Enabled -RequestTimeout 120 #Create default Listener $defaultlistener = New-AzApplicationGatewayHttpListener -Name Devmanh_DefaultListener -Protocol Http -FrontendIPConfiguration $fipconfig -FrontendPort $frontendport #Create default rule for default Listener $frontendRule = New-AzApplicationGatewayRequestRoutingRule -Name Devmanh_rule1 -RuleType Basic -HttpListener $defaultlistener -BackendAddressPool $defaultPool -BackendHttpSettings $poolSettings #Create SKU $sku = New-AzApplicationGatewaySku -Name Standard_Medium -Tier Standard -Capacity 2 #Create New Application Gateway $appgw = New-AzApplicationGateway -Name Devmanh_AppGateway -ResourceGroupName Devmanh_RG -Location southeastasia -BackendAddressPools $defaultPool ` -BackendHttpSettingsCollection $poolSettings ` -FrontendIpConfigurations $fipconfig ` -GatewayIpConfigurations $gipconfig ` -FrontendPorts $frontendport ` -HttpListeners $defaultlistener ` -RequestRoutingRules $frontendRule ` -Sku $sku #####Add image and video backend pools and port##### #Get existing application Gateway $appgw = Get-AzApplicationGateway -ResourceGroupName Devmanh_RG -Name Devmanh_AppGateway #Add two new Backend pool to exiting Application Gateway Add-AzApplicationGatewayBackendAddressPool -ApplicationGateway $appgw -Name Devmanh_imagesBackendPool Add-AzApplicationGatewayBackendAddressPool -ApplicationGateway $appgw -Name Devmanh_videoBackendPool #Adds a front-end port to an application gateway. Add-AzApplicationGatewayFrontendPort -ApplicationGateway $appgw -Name Devmanh_backEndport -Port 8080 Set-AzApplicationGateway -ApplicationGateway $appgw ##########Add backend listener##################### #Get Exiting AG $appgw = Get-AzApplicationGateway ` -ResourceGroupName myResourceGroupAG ` -Name Devmanh_AppGateway $backendPort = Get-AzApplicationGatewayFrontendPort -ApplicationGateway $appgw -Name Devmanh_backEndport #Get Exiting frontend IP $fipconfig = Get-AzApplicationGatewayFrontendIPConfig -ApplicationGateway $appgw #Add New Listener for backend Add-AzApplicationGatewayHttpListener ` -ApplicationGateway $appgw ` -Name Devmanh_BackendListener ` -Protocol Http ` -FrontendIPConfiguration $fipconfig ` -FrontendPort $backendPort #Set Application Gateway with new listener Set-AzApplicationGateway -ApplicationGateway $appgw #################Add URL path map############# #Get application gateway if required $appgw = Get-AzApplicationGateway -ResourceGroupName Devmanh_RG -Name Devmanh_AppGateway #Get existing HttpSetting $poolSettings = Get-AzApplicationGatewayBackendHttpSettings -ApplicationGateway $appgw -Name Devmanh_PoolSettings #Get existing Backend Pool for Image request $imagePool = Get-AzApplicationGatewayBackendAddressPool -ApplicationGateway $appgw -Name Devmanh_imagesBackendPool #Get existing Backend Pool for Vedio request $videoPool = Get-AzApplicationGatewayBackendAddressPool -ApplicationGateway $appgw -Name Devmanh_videoBackendPool #Get existing Backend Pool for default request $defaultPool = Get-AzApplicationGatewayBackendAddressPool -ApplicationGateway $appgw -Name appGatewayBackendPool #New rule for Image $imagePathRule = New-AzApplicationGatewayPathRuleConfig -Name Devmanh_imagePathRule -Paths "/images/*" -BackendAddressPool $imagePool -BackendHttpSettings $poolSettings #New rule for Vedio $videoPathRule = New-AzApplicationGatewayPathRuleConfig -Name Devmanh_videoPathRule -Paths "/video/*" -BackendAddressPool $videoPool -BackendHttpSettings $poolSettings #Configure URL path for the AG Add-AzApplicationGatewayUrlPathMapConfig -ApplicationGateway $appgw -Name Devmanh_urlpathmap -PathRules $imagePathRule, $videoPathRule -DefaultBackendAddressPool $defaultPool -DefaultBackendHttpSettings $poolSettings #Set Application Gaetway Set-AzApplicationGateway -ApplicationGateway $appgw #####################Add routing rule################### #Get application gateway if required $appgw = Get-AzApplicationGateway -ResourceGroupName Devmanh_RG -Name Devmanh_AppGateway #Get newly created listener if required $backendlistener = Get-AzApplicationGatewayHttpListener -ApplicationGateway $appgw -Name Devmanh_BackendListener #Get Url path mapping if required $urlPathMap = Get-AzApplicationGatewayUrlPathMapConfig -ApplicationGateway $appgw -Name Devmanh_urlpathmap #Add a new path based routing Rule Add-AzApplicationGatewayRequestRoutingRule ` -ApplicationGateway $appgw ` -Name Devmanh_rule2 ` -RuleType PathBasedRouting ` -HttpListener $backendlistener ` -UrlPathMap $urlPathMap #Set Application Gateway Set-AzApplicationGateway -ApplicationGateway $appgw ########################Create virtual machine scale sets######################## #Get Exiting Vnet $vnet = Get-AzVirtualNetwork -ResourceGroupName Devmanh_RG -Name Devmanh_VNet #Get application gateway if required $appgw = Get-AzApplicationGateway -ResourceGroupName Devmanh_RG -Name Devmanh_AppGateway #Get existing Backend Pool for Image request $imagesPool = Get-AzApplicationGatewayBackendAddressPool -ApplicationGateway $appgw -Name Devmanh_imagesBackendPool #Get existing Backend Pool for Vedio request $videoPool = Get-AzApplicationGatewayBackendAddressPool -ApplicationGateway $appgw -Name Devmanh_videoBackendPool #Get existing Backend Pool for default request $backendPool = Get-AzApplicationGatewayBackendAddressPool -ApplicationGateway $appgw -Name appGatewayBackendPool for ($i=1; $i -le 3; $i++) { if ($i -eq 1) { $poolId = $backendPool.Id } if ($i -eq 2) { $poolId = $imagesPool.Id } if ($i -eq 3) { $poolId = $videoPool.Id } #Create new IP config for VMSS $ipConfig = New-AzVmssIpConfig -Name Devmanh_VmssIPConfig$i -SubnetId $vnet.Subnets[1].Id -ApplicationGatewayBackendAddressPoolsId $poolId #Configure VM Scale Set $vmssConfig = New-AzVmssConfig -Location westeurope -SkuCapacity 2 -SkuName Standard_B2s -UpgradePolicyMode Automatic #SET storage profile for VM Scale Set Set-AzVmssStorageProfile $vmssConfig -ImageReferencePublisher MicrosoftWindowsServer -ImageReferenceOffer WindowsServer -ImageReferenceSku 2016-Datacenter -ImageReferenceVersion latest -OsDiskCreateOption FromImage #SET storage profile for VM Scale Set Set-AzVmssOsProfile $vmssConfig -AdminUsername manasadmin -AdminPassword "password#1" -ComputerNamePrefix vmss$i #Add VMSS Network interrface config Add-AzVmssNetworkInterfaceConfiguration -VirtualMachineScaleSet $vmssConfig -Name Devmanh_VmssNetConfig$i -Primary $true -IPConfiguration $ipConfig #Create new VMSS New-AzVmss -ResourceGroupName Devmanh_RG -Name Devmanh_vmss$i -VirtualMachineScaleSet $vmssConfig } ####################Install IIS####################### $publicSettings = @{ "fileUris" = (,"https://raw.githubusercontent.com/Azure/azure-docs-powershell-samples/master/application-gateway/iis/appgatewayurl.ps1"); "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File appgatewayurl.ps1" } for ($i=1; $i -le 3; $i++) { $vmss = Get-AzVmss -ResourceGroupName Devmanh_RG -VMScaleSetName Devmanh_vmss$i Add-AzVmssExtension -VirtualMachineScaleSet $vmss ` -Name "customScript" ` -Publisher "Microsoft.Compute" ` -Type "CustomScriptExtension" ` -TypeHandlerVersion 1.8 ` -Setting $publicSettings Update-AzVmss -ResourceGroupName Devmanh_RG -Name Devmanh_vmss$i -VirtualMachineScaleSet $vmss } ################TEST THE AG################### Get-AzPublicIPAddress -ResourceGroupName Devmanh_RG -Name Devmanh_AG_PIPAddress 13.93.0.144 http://13.93.0.144:8080/video/test.htm http://13.93.0.144:8080/images/test.htm