Automate The Creation of Virtual Machines in VMWare

preview_player
Показать описание
In This Video, you will learn how to automate Virtual Machine build in VMware. In this example we are only creating single VM with Customization spec and IP address assignment but in future release we may see more parameter and more number of machines build at the same time.

You can use below script and let me know if you are facing any challenges.

=====================Script Here==================
$VMname = read-host "AUTOVM01"
$IP = "100.0.0.51"
$SNM = "255.0.0.0"
$GW = "100.0.0.1"
$DNS1 = "100.0.0.1"
$DNS2 = "100.0.0.2"
$CPU = "4"
$MemoryGB = "6"

#Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false

$GC = Get-Credential -Message "Credential are required to Access Gues Windows Machine" -User Administrator

New-VM -Name $VMname -Template Win2016 -OSCustomizationSpec Windows -Datastore 'STRG-CLS01’ -DiskStorageFormat Thin -ResourcePool CLS01 -Location Mumbai
Set-VM -VM $VMname -NumCpu $CPU -MemoryGB $MemoryGB -Confirm:$false | Out-Null
Start-vm -VM $VMname

#Only show time in your script
function Get-TimeStamp {
return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)
}

#We are waiting here to complete Customization on VM
while($true){
Start-Sleep -Seconds 60
write-output "$(Get-TimeStamp) Waiting for Customization to be completed"
$CustomizeSuccess = Get-VIEvent -Entity $VMname | Where-Object {$_.FullFormattedMessage -Match "Customization of VM $VMname succeeded"}
$CustomizeFail= Get-VIEvent -Entity $VMname | Where-Object {$_.FullFormattedMessage -match "Customization of VM $VMname Failed"}

If ($CustomizeSuccess){
Write-host "OS Customization has completed on $VMname"
#Here we are setting IP address on the VM by using guest credential
Write-Host "Setting IP address for $VMname..."
$IPConf = Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType bat -ScriptText $IPADD
If ($IPConf) {Write-host "IP address configured in System"} else {Write-host "IP Address assignmenet failed."
Pause
Exit}
$DNS1Conf = Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType bat -ScriptText $DNSE1
If ($DNS1Conf) {Write-host "Primary DNS Assigned"} else {Write-host "Primary DNS assignment failed."
Pause
Exit}
$DNS2Conf = Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType bat -ScriptText $DNSE2
If ($DNS2Conf) {Write-host "Secondary DNS configured in System"} else {Write-host "Secondary DNS assignment failed."
Pause
Exit}
Write-Host "System Build is done, Please verify the VM."
break
}
If ($CustomizeFail){

Write-host "OS Customization failed on $VMname , Press any Key to Exit the Script"
Pause
Exit
}
}
===================== Script End Here ======================

PowerCLI Exclude List of VM's From Script Deleting Snapshots in VMware

PowerCLI Power On a Suspended VMware Virtual Machine

PowerShell Installing software remotely on Multiple Computers

PowerShell Send Email if Windows Service is in Stopped State

PowerShell Send email if windows service is not running

Get Local Group Members From Remote computers

Powershell Script To Change Service Account or Update Password

Powershell How To Change Remote Computer (Workgroup) Local Account Password

Delete Virtual Machine Snapshots Older Than Specific Days

How To Create File Selection Dialog Box in PowerShell

Add User/Group To The Local Administrators/RDP Group On Multiple Computers

Get Windows Updates list from Remote Computer

PowerShell Remotely Uninstall Software from Multiple Computers

PowerShell Search Specific KB Status on Remote Computers

PowerShell Script to Connect Multiple Server's Remote Session

PowerCLI Script to power ON dedicated VMs listed in a text file

PowerCLI Script to power Off dedicated VMs listed in a text file

Delete Snapshot's from Multiple Virtual Machines
Рекомендации по теме
Комментарии
Автор

Sir, Please prepare one Rvtool script for fetch multiple Vcenter report and we are using 8.0 ESXi version. Kindly help me on this please.

syedfarooq
Автор

I am getting below error message:

The term 'Invoke-VMScript' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.

amandastful
Автор

Hello i always get a no bootable device as a problem I don’t understand why

YnellMwa
Автор

I'm getting error at the time of IP setting Please help

Setting IP address for 2K19AUTOVM...
Invoke-VMScript : 18-04-2023 to authenticate
with the guest operating system using the supplied credentials.
At Creation.ps1:40 char:20
+ ... $IPConf = Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptT ...
+
+ CategoryInfo : NotSpecified: (:) [Invoke-VMScript], InvalidGues
tLogin
+ FullyQualifiedErrorId :

bkbalakrishna
Автор

This script is very good and I have built multiple machines. Need to go over couple small changes but it works. I have question as I have multiple Single node ESXi servers and they do not have clusters. How can a script to build on single esxi as your example is pointing to only cluster based nodes. Please give some tips to make is working. Thanks

minhajahmed
Автор

Great script.
How about adding the part to add the VM to Windows domain?

samuelyapir