VMware Cloud Community
crosen
Contributor
Contributor

Set VM hostname while cloning template

Trying to clone a template multiple times and set the hostname in each VM automatically. The script works fine until I add the '-OSCustomizationSpec' piece.

How can I set the hostname of the VM?

Can I run multiple clones at a time from the same template?

Thanks.

$template = "acqpocad1temp"

$NewVMName ="poc6"

$datastore ="dsRTPVMware70000"

$host = "vhost1"

$folder = "001.70.888-ACQPOC"

$rp = "001.8.8.70HC.888-ACQPOC"

$hostname = "poc6"

Get-Template "$template" | New-VM -VMHost "$host" -Name "$NewVMName" -Location ( Get-Folder "$folder" ) -ResourcePool ( Get-ResourcePool "$rp" ) -Datastore ( Get-Datastore "$datastore" ) -OSCustomizationSpec ( get-oscustomizationspec -name "$hostname" )

Reply
0 Kudos
10 Replies
Vm10
Contributor
Contributor

Hey Crosen,

i was fighting with the same issue for a while, could never get new-vm to produce a working vm! Your script put me on the right track!

here's what works for me:

$template = "Win2k3-SP2-20G"

$NewVMName ="Clone2"

$datastore ="DS3"

$vchost = "<host ip>" (insert your hostname)

$folder = "Test Servers"

$respool = "001.8.8.70HC.888-ACQPOC"

$oscust = "Windows2003"

Get-Template "$template" | New-VM -VMHost "$vchost" -Name "$NewVMName" -Location ( Get-Folder "$folder" ) -Datastore ( Get-Datastore "$datastore" ) -OSCustomizationSpec ( get-oscustomizationspec -name "$oscust" )

greetings,

Mike

Reply
0 Kudos
Vm10
Contributor
Contributor

version2:

foreach ($i in 1..2) {Get-Template "$template" | New-VM -VMHost "$vchost" -Name "$NewVMName-$i" -Location ( Get-Folder "$folder" ) -Datastore ( Get-Datastore "$datastore" ) -OSCustomizationSpec ( get-oscustomizationspec -name "$oscust" ) | Start-VM }

this creates 2 vm's, clone1-1 and clone1-2

hope this gets you going.

Mike

Reply
0 Kudos
crosen
Contributor
Contributor

Still is not liking the 'get-oscustomizatiospec' part of the script for me. That is the part that I need to figure out so I don't have to log onto 1000 VMs and change their hostname. Thanks.

$hostname = "poc"

foreach ($i in 1..2) {Get-Template "$template" | New-VM -VMHost "vhost1" -Name "$NewVMName-$i" -Location ( Get-Folder "$folder" ) -ResourcePool ( Get-ResourcePool "$rp" ) -Datastore ( Get-Datastore "$datastore" ) -OSCustomizationSpec ( get-oscustomizationspec -name "$hostname-$i" ) | Start-VM }

Get-OSCustomizationSpec : 3/10/2009 09:22:20 Get-OSCustomizationSpec OSCustomizationSpec wit

found, using the specified filter(s).

At C:\VMware_Healthcheck\createvms2.ps1:16 char:262

+ Get-Template "$template" | New-VM -VMHost "rtpvhost73.tivlab.raleigh.ibm.com" -Name "$NewVMName" -Lo

er "$folder" ) -ResourcePool ( Get-ResourcePool "$rp" ) -Datastore ( Get-Datastore "$datastore" ) -OSC

( get-oscustomizationspec <<<< -name "$hostname" )

New-VM : Cannot bind parameter 'OSCustomizationSpec'. Cannot convert "" to "VMware.VimAutomation.Types

pec".

At C:\VMware_Healthcheck\createvms2.ps1:16 char:236

+ Get-Template "$template" | New-VM -VMHost "rtpvhost73.tivlab.raleigh.ibm.com" -Name "$NewVMName" -Lo

er "$folder" ) -ResourcePool ( Get-ResourcePool "$rp" ) -Datastore ( Get-Datastore "$datastore" ) -OSC

<<<< ( get-oscustomizationspec -name "$hostname" )

Reply
0 Kudos
JoeLyons
Enthusiast
Enthusiast

"Can I run multiple clones at a time from the same template?"

Remember to back EVERYTHING up before you change ANYTHING and consider awarding points if answers where helpful to you.
Reply
0 Kudos
crosen
Contributor
Contributor

How do I run the clone multiple times synchronously? Two scripts?

Reply
0 Kudos
prospero63
Contributor
Contributor

Here is what I did (YMMV). I built a customization spec that says "use VM name for computer name" and then I feed the VM name into the syntax for new-vm. When it's all said and done, I have unique VM and computer names for all the deployed VM's. The only drawback I have seen is that there is no "power on after deployment" option that I can find for the new-vm cmdlet so once all the VM's are deployed, I run another script (start-vm) that powers them all on, allowing sysprep to finish the job.

Reply
0 Kudos
crosen
Contributor
Contributor

That is exactly what I need. Can you share that script with me?

To start the VMs, I added this " | Start-VM" at the end of my command and it was working.

Thanks.

Reply
0 Kudos
prospero63
Contributor
Contributor

I'll give the start-vm thing a try. Mine is a simple one line script. I'm not using variables or anything like that:

new-vm -VMHost <ESX_host> -name <VM_Name> -Location (get-folder -name <Folder> -location <datacenter>) -template <template> -Datastore (get-datastore -VMHost <ESX_Host> -name <datastore>) -OSCustomizationSpec <spec_name> -RunAsync

I just replace the VM_Name for each new line and build a .ps1 file with 15-20 lines like above (and a the connect-viserver command at the top), then run the .ps1 file and check back on status in about an hour and a half.

Reply
0 Kudos
crosen
Contributor
Contributor

Still not working for me. Do you do a set-oscustomizationspec anywhere?

PS C:\VMware_Healthcheck> new-vm -VMHost "rtpvhost73.tivlab.raleigh.ibm.com" -name "poc1" -Location (get-folder -name "0

01.70.888-ACQPOC") -template "acqpocad1temp" -Datastore (get-datastore -VMHost "vhost1" -name

"dsRTPVMware70000") -OSCustomizationSpec "poc1" -RunAsync*

New-VM : 3/10/2009 12:59:02 New-VM Could not find OSCustomizationSpec with name 'poc1'.

At line:1 char:7

+ new-vm <<<< -VMHost "vhost1" -name "poc1" -Location (get-folder -name "001.70.888-ACQPOC"

) -template "acqpocad1temp" -Datastore (get-datastore -VMHost "vhost1" -name "dsRTPVMware700

00") -OSCustomizationSpec "poc1" -RunAsync

New-VM : Could not find all of the objects specified by name.

At line:1 char:7

+ new-vm <<<< -VMHost "vhost1" -name "poc1" -Location (get-folder -name "001.70.888-ACQPOC"

) -template "acqpocad1temp" -Datastore (get-datastore -VMHost "vhost1" -name "dsRTPVMware700

00") -OSCustomizationSpec "poc1" -RunAsync

Reply
0 Kudos
prospero63
Contributor
Contributor

No, but I'm also not putting all the things in quotes you are. Unless there is a space in the name, I don't use quotes anywhere. Here is my entire script (obviously I repeat the last line however many VM's I am creating at that time):

$sourceVIServer = Connect-VIServer -Server "VC_SERVER"

new-vm -VMHost SERVER -name VM_NAME -Location (get-folder -name FOLDER_NAME -location DATACENTER_NAME) -template SPEC -Datastore DATASTORE_NAME -OSCustomizationSpec SPEC -RunAsync

I just noticed how I'm doing the vCenter connect... I lifted that code from another script... The only thing in all that which is quotes for me is the datacenter name (and the VC connect line), and that's because we have a space in it. My entire script is those two lines...

Reply
0 Kudos