How to create 10 (multiple) virtual machines using power shell

Copper Contributor

Hi All,

 

Can anyone please let me know how to create/configure multiple (10) virtual machines (Ubuntu Image)

using power shell.

 

 

Thanks in Advance

2 Replies

@Prasad_Raju 

 

There can be two types of VMs, on-premises, using Microsoft Hyper V from PowerShell, and VMs on Azure, using Azure shell

 

Creating Multiple Hyper-V VMs via PowerShell

 

Assuming that you want to create multiple Hyper-V VMs via PowerShell,

 

  • Below script thinks that you have VMName list and Creates VM names according to each name which are separated coma and single quoted VM Names in a variable.

 

Pre-requisites

 

  • Add an ISO file for OS Installation and puts the HDD in to the directory you will mention in $VMLOC variable and assigns existing Hyper-V Switch to all VM’s.

 

Script starts---------

#Below command will load the Hyper-V module for PowerShell.
Get-Command -Module Hyper-V

# This script creates a Multiple VM's Based on the Names you provided.

#Enter the VM names as mentioned below.
$VMName = 'Server001','server002'

#Enter the ISO File path which contains the Windows Installation files
$ISOpath = "D:\library\Windows Server 2008 R2 SP1_x64fre_server_eval_en-us-DVD.iso"

#Path of the VM HDD file stored
$VMLOC = "d:\test"

#Name of virtual switch which will be used in the VMs
$VMNet = "vEthernet-ADDC-M2"

#Create the VM's
Foreach($vm in $VMName) { New-VM -Name $VM -Generation 2 -SwitchName $VMNet
New-VHD -Path "$VMLOC\$VM\$vm.vhdx" -Dynamic -SizeBytes 40GB
ADD-VMHardDiskDrive -VMName $vm -Path "$VMLOC\$VM\$vm.vhdx"
Set-VM $VM -MemoryStartupBytes 1GB
Add-VMDvdDrive -VMName $vm -Path $ISOpath
Set-VMFirmware -VMName $vm -FirstBootDevice ((Get-VMFirmware -VMName $vm).BootOrder |
Where-Object Device -like *DvD*).Device

}
#Starts all of the VMs and installation of OS will be started.
Start-VM -Name $VMName

Script Ends-------------

 

The only problem I have faced with this script is by using Microsoft ISO files for OS, which is marked by an end user's input when installation starts, asking for "Press any key to start installation.........", to bypass this issue, you can go through the below article if needed.
https://serverfault.com/questions/353826/windows-boot-iso-file-without-press-any-key

you could create a virtual machine scale-set (vmss) with 10 instances.
this is the Azure CLI method, powershell linked below

az vmss create --resource-group vmss-test-1 --name MyScaleSet --image UbuntuLTS --authentication-type password --admin-username azureuser --admin-password P@ssw0rd! --instance-count 10

https://docs.microsoft.com/en-us/powershell/module/az.compute/new-azvmss?view=azps-6.4.0