VMware Cloud Community
trav3ll3r
Contributor
Contributor

Apply OSCustomization to an existing virtual machine using PowerCLI

As part of a project, I am tasked with sysprepping, renaming and re-IP'ing about 500 servers in vcenter.

I created a OS customization template that allows me to accomplish this.

Here is what I do:

1. Power the VM off.

2. Right click and apply OS Customization

3. Choose and apply OS Customization template. At this point, I am prompted to enter a computer name and an IP address.

4. Click OK and wait for the customization task to complete.

5. Power the VM back on at which point, the VM undergoes OS rename, sysprep and IP change and ready to be delivered to downstream teams.

I am trying to find a way to automate this process using powercli but I haven't been able to figure out how to pass computer name and IP address.

This is about as far as I've got to.

$vmname = Get-VM -Name MyVM

$custspec = Get-OSCustomizationSpec -Name 'PDC_Windows'

Get-VM -Name $vmname | Set-VM -OSCustomizationSpec $custspec

Tags (1)
0 Kudos
5 Replies
LucD
Leadership
Leadership

If I understand your process correctly, you just need to do a Start-VM.


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

0 Kudos
trav3ll3r
Contributor
Contributor

Hi Luc! Thanks for responding.

If I merely do a Start-VM at the end of the script, the OS customization does run but the VM is neither renamed nor re-IP'ed as part of the customization process.

I am also not prompted to enter this information after I run the script.

I am looking for a way to specify a computer name and IP address in the script itself but I have not been able to figure this out.

$vmname = Get-VM -Name MyVM

$custspec = Get-OSCustomizationSpec -Name 'PDC_Windows'

Get-VM -Name $vmname | Set-VM -OSCustomizationSpec $custspec -ComputerName 'NewName' -IpAddress '192168.1.10' << Trying to do something like this.

0 Kudos
LucD
Leadership
Leadership

Ok, gotcha.

Changing the hostname inside the guest OS is afaik not available through OSCustimizationSpec directly.

You could use the GuiRunonce parameter if the guest OS is Windows based.

Otherwise, you will have to fall back on the Invoke-VMScript cmdlet (after the customisation and the power on). Also note that this requires VMware Tools to be running.

For the IP address you have to configure your OSCustomizationSpec via the Get- and Set-OSCustomizationNicMapping cmdlet

There is a series of blog posts on the subject on the PowerCLI Blog.

This should provide you with some detailed info


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

0 Kudos
trav3ll3r
Contributor
Contributor

Sorry for the late reply. Thank you, Luc.

But it appears I will have to do this manually seeing that I have a mix of Windows and Linux servers some of which either don't have vmtools installed or installed but not running.

However, I am surprised to see something that is easily accomplished via the GUI cannot be accomplished via PowerCLI. Hopefully this will be remedied in a future release.

Thanks again!

0 Kudos
LucD
Leadership
Leadership

I'm not sure I understand what you mean by "...something that is easily accomplished via the GUI... ".

You can manipulate/create OSCustomizationSpec definitions, including the network settings (via OSCustomizationNicMapping).

And that for Windows and Linux boxes.
You can configure the hostname via the NamingScheme and NamingPrefix in the OSCustomizationSpec.
Set the scheme to Fixed and fill in the name on the NamingPrefix.
And you can update that value for each VM your are deploying.

What will be difficult is to be prompted for the hostname.

What you can not do with these OSCustomizationSpec definitions, and definitely not via the Web Client, is run scripts inside the Guest OS.

The requirement there is that VMware Tools are installed and running.


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

0 Kudos