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

%3CLINGO-SUB%20id%3D%22lingo-sub-2729789%22%20slang%3D%22en-US%22%3EHow%20to%20create%2010%20(multiple)%20virtual%20machines%20using%20power%20shell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2729789%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20All%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20anyone%20please%20let%20me%20know%20how%20to%20create%2Fconfigure%20multiple%20(10)%20virtual%20machines%20(Ubuntu%20Image)%3C%2FP%3E%3CP%3Eusing%20power%20shell.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20Advance%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2729789%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2733712%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20create%2010%20(multiple)%20virtual%20machines%20using%20power%20shell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2733712%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1149175%22%20target%3D%22_blank%22%3E%40Prasad_Raju%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThere%20can%20be%20two%20types%20of%20VMs%2C%20on-premises%2C%20using%20Microsoft%20Hyper%20V%20from%20PowerShell%2C%20and%20VMs%20on%20Azure%2C%20using%20Azure%20shell%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3ECreating%20Multiple%20Hyper-V%20VMs%20via%20PowerShell%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAssuming%20that%20you%20want%20to%20create%20multiple%20Hyper-V%20VMs%20via%20%3CA%20href%3D%22https%3A%2F%2Fbit.ly%2F38gJTYt%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EPowerShell%2C%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CUL%3E%3CLI%3EBelow%20script%20thinks%20that%20you%20have%20VMName%20list%20and%20Creates%20VM%20names%20according%20to%20each%20name%20which%20are%20separated%20coma%20and%20single%20quoted%20VM%20Names%20in%20a%20variable.%3C%2FLI%3E%3C%2FUL%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EPre-requisites%20%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CUL%3E%3CLI%3EAdd%20an%20ISO%20file%20for%20OS%20Installation%20and%20puts%20the%20HDD%20in%20to%20the%20directory%20you%20will%20mention%20in%20%24VMLOC%20variable%20and%20assigns%20existing%20Hyper-V%20Switch%20to%20all%20VM%E2%80%99s.%3C%2FLI%3E%3C%2FUL%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EScript%20starts---------%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%23Below%20command%20will%20load%20the%20Hyper-V%20module%20for%20PowerShell.%3CBR%20%2F%3EGet-Command%20-Module%20Hyper-V%3C%2FP%3E%3CP%3E%23%20This%20script%20creates%20a%20Multiple%20VM's%20Based%20on%20the%20Names%20you%20provided.%3C%2FP%3E%3CP%3E%23Enter%20the%20VM%20names%20as%20mentioned%20below.%3CBR%20%2F%3E%24VMName%20%3D%20'Server001'%2C'server002'%3C%2FP%3E%3CP%3E%23Enter%20the%20ISO%20File%20path%20which%20contains%20the%20Windows%20Installation%20files%3CBR%20%2F%3E%24ISOpath%20%3D%20%22D%3A%5Clibrary%5CWindows%20Server%202008%20R2%20SP1_x64fre_server_eval_en-us-DVD.iso%22%3C%2FP%3E%3CP%3E%23Path%20of%20the%20VM%20HDD%20file%20stored%3CBR%20%2F%3E%24VMLOC%20%3D%20%22d%3A%5Ctest%22%3C%2FP%3E%3CP%3E%23Name%20of%20virtual%20switch%20which%20will%20be%20used%20in%20the%20VMs%3CBR%20%2F%3E%24VMNet%20%3D%20%22vEthernet-ADDC-M2%22%3C%2FP%3E%3CP%3E%23Create%20the%20VM's%3CBR%20%2F%3EForeach(%24vm%20in%20%24VMName)%20%7B%20New-VM%20-Name%20%24VM%20-Generation%202%20-SwitchName%20%24VMNet%3CBR%20%2F%3ENew-VHD%20-Path%20%22%24VMLOC%5C%24VM%5C%24vm.vhdx%22%20-Dynamic%20-SizeBytes%2040GB%3CBR%20%2F%3EADD-VMHardDiskDrive%20-VMName%20%24vm%20-Path%20%22%24VMLOC%5C%24VM%5C%24vm.vhdx%22%3CBR%20%2F%3ESet-VM%20%24VM%20-MemoryStartupBytes%201GB%3CBR%20%2F%3EAdd-VMDvdDrive%20-VMName%20%24vm%20-Path%20%24ISOpath%3CBR%20%2F%3ESet-VMFirmware%20-VMName%20%24vm%20-FirstBootDevice%20((Get-VMFirmware%20-VMName%20%24vm).BootOrder%20%7C%3CBR%20%2F%3EWhere-Object%20Device%20-like%20*DvD*).Device%3C%2FP%3E%3CP%3E%7D%3CBR%20%2F%3E%23Starts%20all%20of%20the%20VMs%20and%20installation%20of%20OS%20will%20be%20started.%3CBR%20%2F%3EStart-VM%20-Name%20%24VMName%3C%2FP%3E%3CP%3E%3CSTRONG%3EScript%20Ends-------------%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EThe%20only%20problem%20I%20have%20faced%20with%20this%20script%20is%20by%20using%20Microsoft%20ISO%20files%20for%20OS%2C%20which%20is%20marked%20by%20an%20end%20user's%20input%20when%20installation%20starts%2C%20asking%20for%20%22Press%20any%20key%20to%20start%20installation.........%22%2C%20to%20bypass%20this%20issue%2C%20you%20can%20go%20through%20the%20below%20article%20if%20needed.%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fserverfault.com%2Fquestions%2F353826%2Fwindows-boot-iso-file-without-press-any-key%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fserverfault.com%2Fquestions%2F353826%2Fwindows-boot-iso-file-without-press-any-key%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2735572%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20create%2010%20(multiple)%20virtual%20machines%20using%20power%20shell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2735572%22%20slang%3D%22en-US%22%3Eyou%20could%20create%20a%20virtual%20machine%20scale-set%20(vmss)%20with%2010%20instances.%3CBR%20%2F%3Ethis%20is%20the%20Azure%20CLI%20method%2C%20powershell%20linked%20below%3CBR%20%2F%3E%3CBR%20%2F%3Eaz%20vmss%20create%20--resource-group%20vmss-test-1%20--name%20MyScaleSet%20--image%20UbuntuLTS%20--authentication-type%20password%20--admin-username%20azureuser%20--admin-password%20P%40ssw0rd!%20--instance-count%2010%3CBR%20%2F%3E%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Faz.compute%2Fnew-azvmss%3Fview%3Dazps-6.4.0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Faz.compute%2Fnew-azvmss%3Fview%3Dazps-6.4.0%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E
New 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