VMware Cloud Community
Skagnola
Enthusiast
Enthusiast
Jump to solution

Deploy single VM from template; Customize Network, Hostname during deploy

Hello!

I am working with a script to deploy a VM from a template, but am running into a couple issues.

Issue 1: the VM is not deploying with the customization of network information

Issue 2: the Vm is not deploying with the preferred hostname

What is working, the VM will deploy with the template hardware settings, of course. However, note a couple items in the template are set as below and not applying during deploy

pastedImage_10.png

pastedImage_11.png

Here is the script

# Get source Template

$Template = Get-Template -Name 'RHEL 6.9'

# Get the OS CustomizationSpec

$OSCusSpec = Get-OSCustomizationSpec -Name 'RHEL VM'

#Get Cluster

$tgtClusterName = 'CLUSTER7'

$cluster = Get-Cluster -Name $tgtClusterName

# Get a host within cluster

$VMHost = Get-Cluster $cluster | Get-VMHost | Get-Random

# Get datastore

$dsName = 'ds*-datastore7*'

# Find a datastore with most space

$Datastore = Get-Datastore -Name $dsName | Sort-Object -Property FreeSpaceGB -Descending | Select -First 1

# Deploy Virtual Machine

$VM = New-VM -Name 'testvm1p02' -Template $Template -VMHost $VMHost -Datastore $Datastore

Get-OSCustomizationSpec $OSCusSpec |`

Set-OSCustomizationSpec -NamingScheme Fixed -NamingPrefix 'testvm1p02'

#Get-OSCustomizationNicMapping | `

#Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress 192.168.1.101 -SubnetMask 255.255.254.0 -DefaultGateway 192.168.1.1

I have the NicMapping lines commented out because all this is doing, also for the -NamigScheme, is hard-editing the OSCustomizationSpec and nothing more. I have to go in and change those settings back.

Looking at a few other threads similar to what I am doing, I am not able to wrap my head around what I am missing in the syntax. / order / args.

Any help is appreciated!

1 Solution

Accepted Solutions
Skagnola
Enthusiast
Enthusiast
Jump to solution

Hey, Luc!

Something interesting, when I ran

Get-OSCustomizationSpec | Select Name

... that 'temp01`, NonPersistent spec is in the list. BUT ,when I view it from within the GUI, it is not visible. I removed it with

Remove-OSCustomizationSpec temp01 -Confirm

On the network setting, what exactly do you want to achieve?

1 NIC with a static IP?

Yes, 1 NIC with a static IP and associated configs on the NIC.

Turns out, after I created a new spec named 'temp1' and ran, it made the VM as I wanted it to, pulling in the correct hostname and the NIC configuration. I added a bit to remove the temp customization spec at the end

# Get the OS CustomizationSpec and clone

$OSCusSpec = Get-OSCustomizationSpec -Name 'RHEL VM' | New-OSCustomizationSpec -Name 'temp1' -Type NonPersistent

#Update Spec with IP information

Get-OSCustomizationNicMapping -OSCustomizationSpec $OSCusSpec |

    Set-OSCustomizationNicMapping -IPMode UseStaticIP `

    -IPAddress '192.168.1.101' `

    -SubnetMask '255.255.254.0' `

    -DefaultGateway '192.168.1.2' `

    #-Dns '192.168.1.2'

#Get updated Spec Object

$OSCusSpec = Get-OSCustomizationSpec -Name 'temp1'

# Get source Template

$Template = Get-Template -Name 'RHEL 6.9'

#Get Cluster

$tgtClusterName = 'CLUSTER7'

$cluster = Get-Cluster -Name $tgtClusterName

# Get a host within cluster

$VMHost = Get-Cluster $cluster | Get-VMHost | Get-Random

# Get datastore

$dsName = 'ds*-store7*'

# Find a datastore with most space

$Datastore = Get-Datastore -Name $dsName | Sort-Object -Property FreeSpaceGB -Descending | Select -First 1

# Deploy Virtual Machine and remove temp custom spec

$VM = New-VM -Name 'testvm1p02' -Template $Template -VMHost $VMHost -Datastore $Datastore -OSCustomizationSpec $OSCusSpec | Start-VM

Remove-OSCustomizationSpec $OSCusSpec -Confirm

View solution in original post

0 Kudos
6 Replies
Skagnola
Enthusiast
Enthusiast
Jump to solution

I just tried adding some of the network configuration into the script by cloning a customization and setting it as nonpersistent but I am getting an error on run

Code

# Get the OS CustomizationSpec and clone

$OSCusSpec = Get-OSCustomizationSpec -Name 'RHEL VM' | New-OSCustomizationSpec -Name 'temp01' -Type NonPersistent

#Update Spec with IP information

Get-OSCustomizationNicMapping -OSCustomizationSpec $OSCusSpec |

    Set-OSCustomizationNicMapping -IPMode UseStaticIP `

    -IPAddress '192.168.1.101' `

    -SubnetMask '255.255.254.0' `

    -DefaultGateway '192.168.1.2' `

    #-Dns '192.168.1.2'

#Get updated Spec Object

$OSCusSpec = Get-OSCustomizationSpec -Name 'temp01'

# Get source Template

$Template = Get-Template -Name 'RHEL 6.9'

...

Error

New-OSCustomizationSpec : 12/19/2017 9:36:50 AM New-OSCustomizationSpec There is already a customization spec with the same name.

At C:\templatedeploy.ps1:2 char:67

+ ...  Gateway' | New-OSCustomizationSpec -Name 'temp01' -Type NonPersisten ...

+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [New-OSCustomizationSpec], InvalidArgument

    + FullyQualifiedErrorId : Client20_VmHostServiceImpl_NewCustomizationSpec_DuplicateCustomizationSpecClient,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewCustomizationSpec

Get-OSCustomizationNicMapping : 12/19/2017 9:36:50 AM Get-OSCustomizationNicMapping Value cannot be found for the mandatory parameter OSCustomizationSpec

At C:\templatedeploy.ps1:5 char:1

+ Get-OSCustomizationNicMapping -OSCustomizationSpec $OSCusSpec |

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-OSCustomizationNicMapping], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetOSCustomizationNicMapping

New-VM : A parameter cannot be found that matches parameter name 'OSCuscomizationSpec'.

At C:\templatedeploy.ps1:32 char:91

+ ... te -VMHost $VMHost -Datastore $Datastore -OSCuscomizationSpec $OSCusS ...

+                                              ~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [New-VM], ParameterBindingException

    + FullyQualifiedErrorId : NamedParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The 1st error is because you are using a Name of an OSCustomizationSpec that aready seems to exist.

The other errors are all a consequence of that error.

You can test if there is already an OSCustomizationSpec with that name, and the remove it before you create the new one.

On the network setting, what exactly do you want to achieve?

1 NIC with a static IP?


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

0 Kudos
Skagnola
Enthusiast
Enthusiast
Jump to solution

Hey, Luc!

Something interesting, when I ran

Get-OSCustomizationSpec | Select Name

... that 'temp01`, NonPersistent spec is in the list. BUT ,when I view it from within the GUI, it is not visible. I removed it with

Remove-OSCustomizationSpec temp01 -Confirm

On the network setting, what exactly do you want to achieve?

1 NIC with a static IP?

Yes, 1 NIC with a static IP and associated configs on the NIC.

Turns out, after I created a new spec named 'temp1' and ran, it made the VM as I wanted it to, pulling in the correct hostname and the NIC configuration. I added a bit to remove the temp customization spec at the end

# Get the OS CustomizationSpec and clone

$OSCusSpec = Get-OSCustomizationSpec -Name 'RHEL VM' | New-OSCustomizationSpec -Name 'temp1' -Type NonPersistent

#Update Spec with IP information

Get-OSCustomizationNicMapping -OSCustomizationSpec $OSCusSpec |

    Set-OSCustomizationNicMapping -IPMode UseStaticIP `

    -IPAddress '192.168.1.101' `

    -SubnetMask '255.255.254.0' `

    -DefaultGateway '192.168.1.2' `

    #-Dns '192.168.1.2'

#Get updated Spec Object

$OSCusSpec = Get-OSCustomizationSpec -Name 'temp1'

# Get source Template

$Template = Get-Template -Name 'RHEL 6.9'

#Get Cluster

$tgtClusterName = 'CLUSTER7'

$cluster = Get-Cluster -Name $tgtClusterName

# Get a host within cluster

$VMHost = Get-Cluster $cluster | Get-VMHost | Get-Random

# Get datastore

$dsName = 'ds*-store7*'

# Find a datastore with most space

$Datastore = Get-Datastore -Name $dsName | Sort-Object -Property FreeSpaceGB -Descending | Select -First 1

# Deploy Virtual Machine and remove temp custom spec

$VM = New-VM -Name 'testvm1p02' -Template $Template -VMHost $VMHost -Datastore $Datastore -OSCustomizationSpec $OSCusSpec | Start-VM

Remove-OSCustomizationSpec $OSCusSpec -Confirm

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There are two types of OSCustomizationSpec, a permanent or persistent one (visible in the Web Client) and a temporary or nonpersistent one (only visible in your current PowerShell session).

You indicate the type with the OSType parameter on the New-OSCustomizationSpec cmdlet.


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

garyman123
Contributor
Contributor
Jump to solution

can you please share the steps you follwoed
0 Kudos
garyman123
Contributor
Contributor
Jump to solution

see attached error
0 Kudos