Deploy a new virtual machine based on a template in VMware via Powershell

Step 1. Install VMwarePowerCli
Open Powershell as Administrator and run this command:

Install-Module VMware.PowerCLI -Scope CurrentUser

Step 2. Copy the script and fill in your information
Copy the script below and save this locally on your own device.
Fill in the information that is required for your environment, You will

#Fill in the information below.
#Keep in mind that for DNS Adresses you will need to write is as follow: ("","")

$VcenterUsername = 
$VcenterPassword = 
$VcenterURL = 
$VmNameOfNewVm =
$templatename =  
$networkname = 
$resourcepool = 
$datastore = 
$VMlocaluser = 
$VMlocalpassword = 
$VMDiskSize = 
$VMNumberOfCPU = 
$VMMemory = 

$DomainAdmin = 
$DomainPassword = 
$DomainName = 
$DomainDescriptionOnObject = 
$IPAddress = 
$GatewayAddress = 
$DNSAddresses = 

#This is where the script starts to run, you do not need to fill in anything below this part
set-powercliconfiguration -invalidcertificateaction ignore -Confirm:$false
Connect-VIServer –Server '$VcenterURL' -username $VcenterUsername -password $VcenterPassword
New-vm -name $VmNameOfNewVm -template "$templatename" -ResourcePool $resourcepool -datastore $Datastore
Get-VM -name $VmNameOfNewVm | Get-HardDisk| set-harddisk -capacityGB $VMDiskSize -confirm:$false
Get-VM -name $VmNameOfNewVm | set-VM -NumCPU $VMNumberOfCPU -confirm:$false
Get-VM -name $VmNameOfNewVm | set-VM -memoryGB $VMMemory -confirm:$false
Get-VM -name $VmNameOfNewVm | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName "$networkName" -Confirm:$false
Get-VM -name $VmNameOfNewVm | start-vm -Confirm:$false
Disconnect-VIServer –Server '$VcenterURL' -Confirm:$false

After the VM is created, run this:

set-powercliconfiguration -invalidcertificateaction ignore -Confirm:$false
Connect-VIServer –Server '$VcenterURL' -username $VcenterUsername -password $VcenterPassword
Start-sleep -second 60
$vmLocalPWord = ConvertTo-SecureString -String "$VMlocalpassword" -AsPlainText -Force
$vmLocalCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $vmLocalUser, $vmLocalPWord
# This Scriptblock is used to add new VMs to the newly created domain by first defining the domain creds on the machine and then using Add-Computer
$JoinNewDomain = '$DomainUser = "$DomainAdmin"
$DomainPWord = ConvertTo-SecureString -String "$DomainPassword" -AsPlainText -Force;
$DomainCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $DomainUser, $DomainPWord;
Add-Computer -DomainName $DomainName -Credential $DomainCredential;
rename-Computer -newname "$VmNameOfNewVm" -Domaincredential $DomainCredential;
$networkinterface = (Get-NetAdapter).name
New-NetIPAddress –InterfaceAlias “$networkinterface” -addressFamily IPv4 -IPAddress “$IPAddress” –PrefixLength 21 -DefaultGateway "$GatewayAddress"
Set-DnsClientServerAddress -InterfaceAlias "$networkinterface" -serveraddresses ("","")
Set-NetFirewallProfile -Profile Domain -Enabled False
Invoke-VMScript -ScriptText $JoinNewDomain -VM "$VmNameOfNewVm" -GuestCredential $vmLocalCredential
Start-sleep -second 30
restart-VMGuest -vm $VmNameOfNewVm
Disconnect-VIServer –Server '$VcenterURL' -Confirm:$false
$Username = "$VcenterUsername"
$SecurePwd = ConvertTo-SecureString -String "$VcenterPassword" -AsPlainText -Force;
$Creds = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $SecurePwd
Set-ADComputer "$VmNameOfNewVm" -Description "$DomainDescriptionOnObject" -Credential $Creds

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s