VMware Cloud Community
vSohill
Expert
Expert
Jump to solution

PowerCLI Script – Deploy VMs from template and Configure VM hardware

Hi,

I am looking for PowerCli script to deploy VMs from template, the aim  from that script is deploy a VM  different VM hardware configuration from the template such number of vCPU, RAM, ....etc.

Thank you

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The vCPU and RAM are already done with the Set-VM cmdlet.

For the disk you will have to use the Set-HardDisk cmdlet.

$templateName = 'template'

$esxName = 'esx'

$dsName = 'ds'

$template = Get-Template -Name $templateName

$ds = Get-Datastore -Name $dsName

$esx = Get-VMHost -Name $esxName

$vm = New-VM -Template $template -Name stressTest -VMHost $esx -Datastore $ds -DiskStorageFormat Thin |

Set-VM -NumCpu 2 -MemoryGB 4 -Confirm:$false

Get-HardDisk -VM $vm | Set-HardDisk -CapacityGB 100

Start-VM -VM $vm -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
11 Replies
LucD
Leadership
Leadership
Jump to solution

Basically you can pipe the New-VM and Set-VM cmdlets together to achieve something like that.

$templateName = 'template'

$esxName = 'esx'

$dsName = 'ds'

$template = Get-Template -Name $templateName

$ds = Get-Datastore -Name $dsName

$esx = Get-VMHost -Name $esxName

New-VM -Template $template -Name MyVM -VMHost $esx -Datastore $ds -DiskStorageFormat Thin |

Set-VM -NumCpu 2 -MemoryGB 4 -Confirm:$false |

Start-VM -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vSohill
Expert
Expert
Jump to solution

Hi  LucD,

I would like to set number of vCPU, RAM and disk size, Can I do that ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The vCPU and RAM are already done with the Set-VM cmdlet.

For the disk you will have to use the Set-HardDisk cmdlet.

$templateName = 'template'

$esxName = 'esx'

$dsName = 'ds'

$template = Get-Template -Name $templateName

$ds = Get-Datastore -Name $dsName

$esx = Get-VMHost -Name $esxName

$vm = New-VM -Template $template -Name stressTest -VMHost $esx -Datastore $ds -DiskStorageFormat Thin |

Set-VM -NumCpu 2 -MemoryGB 4 -Confirm:$false

Get-HardDisk -VM $vm | Set-HardDisk -CapacityGB 100

Start-VM -VM $vm -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vSohill
Expert
Expert
Jump to solution

Hi

I tried to point to a cluster instaed of host , but did not work with me. Can I use change the host to a cluster in the script ?

templateName = 'Temp-01'

$Cluster = "test01"

$ds = "Datastore_02"

$template = Get-Template -Name $templateName

$ds = Get-Datastore -Name $ds

$ESXi= Get-Cluster $Cluster

$vm = New-VM -Template $template -Name stressTest $ESXi=Get-Cluster $Cluster  -Datastore $ds -DiskStorageFormat Thin |

Set-VM -NumCpu 2 -MemoryGB 4 -Confirm:$false

Get-HardDisk -VM $vm | Set-HardDisk -CapacityGB 100

Start-VM -VM $vm -Confirm:$false

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try with the ResourcePool parameter instead of the VMHost parameter.

$templateName = 'template'

$esxName = 'esx'

$clusterName = 'cluster'

$dsName = 'ds'

$template = Get-Template -Name $templateName

$ds = Get-Datastore -Name $dsName

$cluster = Get-Cluster -Name $clusterName

$esx = Get-VMHost -Name $esxName

$vm = New-VM -Template $template -Name NewVM -ResourcePool $cluster -Datastore $ds -DiskStorageFormat Thin |

Set-VM -NumCpu 2 -MemoryGB 4 -Confirm:$false

Get-HardDisk -VM $vm | Set-HardDisk -CapacityGB 100

Start-VM -VM $vm -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

rizwann7
Contributor
Contributor
Jump to solution

I need help, I've copied the above the script and change the required things but the script is not working, could you please some help. please see the below script, I don't have vm host name but i have IP address. 

$templateName = 'template'

$esxName = 'esx'

$clusterName = 'cluster'

$dsName = 'ds'

 

$template = Get-Template -Name $IDS-DEP-VM14-Server-2008-BaseImage

$ds = Get-Datastore -Name $DHI DS #002 - FC

$cluster = Get-Cluster -Name $DHI CLU #02 - VDI

$esx = Get-VMHost -Name $10.5.10.231

 

$vm = New-VM -Template $template -Name Rizwannewvm -ResourcePool $cluster -Datastore $ds -DiskStorageFormat Thin |

Set-VM -NumCpu 2 -MemoryGB 4 -Confirm:$false

 

Get-HardDisk -VM $vm | Set-HardDisk -CapacityGB 100

 

Start-VM -VM $vm -Confirm:$false

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The $ in front of the IP address on the Get-VMHost line is causing the error.

Same goes for the Get-Datastore line.

Set the names in the variables.

$templateName = 'IDS-DEP-VM14-Server-2008-BaseImage'

$esxName = '10.5.10.231'

$clusterName = 'DHI CLU #02 - VDI'

$dsName = 'DHI DS #002 - FC'

$template = Get-Template -Name $templateName

$ds = Get-Datastore -Name $dsName

$cluster = Get-Cluster -Name $clusterName

$esx = Get-VMHost -Name $esxName

$vm = New-VM -Template $template -Name Rizwannewvm -ResourcePool $cluster -Datastore $ds -DiskStorageFormat Thin |

Set-VM -NumCpu 2 -MemoryGB 4 -Confirm:$false

Get-HardDisk -VM $vm | Set-HardDisk -CapacityGB 100

Start-VM -VM $vm -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
rizwann7
Contributor
Contributor
Jump to solution

It's working now  🙂 - Thank you so much dear - Ok now can you guide me a little bit more on it as I'm not expert in writing scripts, I want to change few things inside the VM, like VM name in my computer properties and IP address and DNS. Can you help me a little bit more, please? 
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Since your new questions are a bit outside the scope of this thread, I suggest you create one or more threads with your new questions.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
rizwann7
Contributor
Contributor
Jump to solution

Hello Dear,

As you given me directions so I've created another discussion, could you please help me with it.

How do i add OS customization like OS Name and IP configuration inside the VM

Thank you

Rizwan

How do i add OS customization like OS Name and IP configuration inside the VM

0 Kudos
chicojr
Enthusiast
Enthusiast
Jump to solution

Hopefully this thread isn't dead, Great script but I would like some insight to take it a couple steps further. In need of ability to do:

* Change Host name

* Add to the Domain

* upgrade Vmtools and vm hardware

* Ip server, gateway, subnetmask and two dns servers

* Were on Vsphere 6.7 so ability to enable CPU hot plug, and Memory hot plug. Also enable - Check and upgrade vmware tools before each power on.