How to Create Virtual Machines VMs Using PowerCLI Automation – VirtuallyThatGuy

This is a quick script using CSV to create VMs from template

###Create VMs Scripts

####create a .ps1 called "create-VMs.ps1" and add below scripts. then Create a CSV "NewVMs.csv" in the scripts folder in C drive

 

$vms​​ =​​ Import-CSV​​ "C:\Scripts\NewVMs.csv"

​​ 

foreach​​ ($vm​​ in​​ $vms){

#Assign Variables

$Template​​ =​​ Get-Template​​ -Name​​ $vm.Template

$Cluster​​ =​​ $vm.Cluster

$Datastore​​ =​​ Get-Datastore​​ -Name​​ $vm.Datastore

$Custom​​ =​​ Get-OSCustomizationSpec​​ -Name​​ $vm.Customization

$vCPU​​ =​​ $vm.vCPU

$Memory​​ =​​ $vm.Memory

$Network​​ =​​ $vm.Network

$Location​​ =​​ $vm.Location

$VMName​​ =​​ $vm.Name

 

#Where the VM gets built

New-VM​​ -Name​​ $VMName​​ -Template​​ $Template​​ -ResourcePool​​ (Get-Cluster​​ $Cluster​​ |​​ Get-ResourcePool)​​ -Location​​ $Location​​ -StorageFormat​​ Thin​​ -Datastore​​ $Datastore​​ -OSCustomizationSpec​​ $Custom

Start-Sleep​​ -Seconds​​ 10

​​ 

#Where the vCPU, memory, and network gets set

$NewVM​​ =​​ Get-VM​​ -Name​​ $VMName

$NewVM​​ |​​ Set-VM​​ -MemoryGB​​ $Memory​​ -NumCpu​​ $vCPU​​ -Confirm:$false

$NewVM​​ |​​ Get-NetworkAdapter​​ |​​ Set-NetworkAdapter​​ -NetworkName​​ $Network​​ -Confirm:$false

}

 

 

​​ 

 

###Create VMs Scripts
####create a .ps1 called "create-VMs.ps1" and add below scripts. then Create a CSV "NewVMs.csv" in the scripts folder in C drive

$vms = Import-CSV "C:\Scripts\NewVMs.csv"
 
foreach ($vm in $vms){
#Assign Variables
$Template = Get-Template -Name $vm.Template
$Cluster = $vm.Cluster
$Datastore = Get-Datastore -Name $vm.Datastore
$Custom = Get-OSCustomizationSpec -Name $vm.Customization
$vCPU = $vm.vCPU
$Memory = $vm.Memory
$Network = $vm.Network
$Location = $vm.Location
$VMName = $vm.Name

#Where the VM gets built
New-VM -Name $VMName -Template $Template -ResourcePool (Get-Cluster $Cluster | Get-ResourcePool) -Location $Location -StorageFormat Thin -Datastore $Datastore -OSCustomizationSpec $Custom
Start-Sleep -Seconds 10
 
#Where the vCPU, memory, and network gets set
$NewVM = Get-VM -Name $VMName
$NewVM | Set-VM -MemoryGB $Memory -NumCpu $vCPU -Confirm:$false
$NewVM | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $Network -Confirm:$false
}


You May Also Like

About the Author: VirtuallyThatGuy

Leave a Reply

Your email address will not be published. Required fields are marked *