Sep 07 2021 11:20 AM
Hi All,
Can anyone please let me know how to create/configure multiple (10) virtual machines (Ubuntu Image)
using power shell.
Thanks in Advance
Sep 08 2021 11:51 AM
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,
Pre-requisites
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
Sep 09 2021 02:25 AM