Deploy Multiple VMs in Azure using PowerShell
This script will enable the deployment of several virtual machines to Azure in an automated fashion. I already have certain parts of my infrastructure in Azure, such as the virtual network, resource group and a subnet. You will need to create these first before using this script.
The scripts deploys a set of Windows Server 2016 data center VMs, these are the steps:
- Imports the AzureRM module.
- Prompts for a login for Azure.
- A function has been created that defines the parameters for the deployment called "Provision-AzureVM", a single parameter has been defined called $VMName.
- The script then deploys a set of VM's based on the names contained in the array "$VMs" by calling the function as mentioned above.
To use the script you need to modify the variables in the function mark under # Variables.
You then need to define the names of virtual machines in the VMs array.
You could also add more required parameters at the beginning of the function to further customize and script a deployment. I have considered options such as OSImage and vNET/Subnet options.
# Author: Kirk Whetton # Email : kirkwhetton@gmail.com Import-Module AzureRM Login-AzureRmAccount function Provision-AzureVM{ param ($VMName) # Variables required for deployment $ResourceGroup = "Contoso" $Location = "WestEurope" $VNet = "Contoso-UK-vNET" $Subnet = "Infrastructure-Subnet" $VMSize = "Standard_DS2" $VMLocalAdminUser = "localadmin" $VMLocalAdminSecurePassword = ConvertTo-SecureString "Administrator123" -AsPlainText -Force $Credential = New-Object System.Management.Automation.PSCredential ($VMLocalAdminUser, $VMLocalAdminSecurePassword) $NSG = "Infrastructure-NSG" # Deployment New-AzureRMVM ` -ResourceGroupName $ResourceGroup ` -Name $VMName ` -Credential $Credential ` -Location $Location ` -VirtualNetworkName $vNet ` -SubnetName $Subnet ` -AllocationMethod Dynamic ` -SecurityGroupName $NSG ` -Size $VMSize ` -ImageName Win2016Datacenter } # Define the names of VMs for the deployment. $VMs = @( "VM1" "VM2" "VM3" "VM4" ) # Function is being called and VMName parameter is defined using the array above. foreach ($VM in $VMs){ Provision-AzureVM -VMName $VM }
Comments
Post a Comment