VMware Communities
Natarajvd
Contributor
Contributor

Need script for multiple vm deployment

I am planning to deploy multiple  windows vms from existing vm template and the script should look for all input from csv files .

1. vm should deploy from template

2. modify the configuration of the vm based on CSV file

3.genarlize sid 

4.rename the vm as mentioned in csv file

5.set ip address and dns  for single nic  as mentioned in csv

6. join the server to domain 

 

I wrote the below code its working as expected till vm deploy and configuration modification i am looking further codes to work as above requirement kindly help 

# Connect to vCenter Server
Connect-VIServer -Server "vcsa6.mylab.local" -User "Administrator@vsphere.local" -Password "Welcome@xxx"

# Import the CSV file containing the VM specifications and domain information
$csv = Import-Csv "C:\Users\raj\Desktop\Vmdeployment automation\input.csv"

# Loop through each row in the CSV file and create a VM
foreach ($row in $csv)
{

$vmName = $row.VMname
$esxiHost = $row.esxihost
$cpu = $row.cpu
$memory = $row.memory
$datastore = $row.datastore
$vmFolder = $row.vmfolder
$ipv4Address = $row.ipaddress
$subnetMask = $row.subnetmask
$gateway = $row.gateway
$pdns = $row.pdns
$sdns = $row.sdns
$template = $row.template
$domainName = $row.domainname
$domainUser = $row.domainuser
$domainPassword = $row.domainpassword
$adminPassword = $row.adminpassword


# Create the new VM from the template
$vm = New-VM -Name $vmName -Template $template -VMHost $esxiHost -Datastore $datastore -Location $vmFolder -Confirm:$false -RunAsync

$vm = Wait-Task $vm

# Power on the new VM
Start-VM -VM $vm -Confirm:$false -RunAsync

# Wait for the VM to power on
do {
Start-Sleep -Seconds 5
} until ((Get-VM -Name $vmName).PowerState -eq "PoweredOn")

Write-Host "$vmName is now powered on."

# Set the number of CPUs and memory for the new VM
Set-VM -VM $vmName -NumCpu $cpu -MemoryGB $memory -Confirm:$false



}

 

 

0 Kudos
0 Replies