VMware Cloud Community
mtrohde
Enthusiast
Enthusiast
Jump to solution

Trying to create VM from a template using a guest OS specification

I have a template and guest OS specification I want to work with.  How do I pass an IP address to the customization?  

New-VM -Name TestVM -Template Win2016StdGui -NetworkName "VLAN 3" -OSCustomizationSpec "Win2016 - VLAN 3"

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

1) No, unfortunately, you have to specify all of them (that's the way the cmdlet was written).

But you can do like this.
PS: I used parameter splatting to make the code more readable.

$osCustName = 'TestWin'

$osTempCustName = 'osCustTemp'

$newIP = '192.168.20.101'


$sCust = @{

   OSCustomizationSpec = $osCustName

   Name = $osTempCustName

   Type = 'NonPersistent'

   Confirm = $false

}

$osCust = New-OSCustomizationSpec @sCust


$osNic = Get-OSCustomizationNicMapping -OSCustomizationSpec $osCust

$sNic = @{

   OSCustomizationNicMapping = $osNic

   IpMode = 'UseStaticIP'

   IpAddress = $newIP

   SubnetMask = $osNic.SubnetMask

   Dns = $osNic.Dns

   DefaultGateway = $osNic.DefaultGateway

}

$osNic = Set-OSCustomizationNicMapping @sNic


Get-OSCustomizationNicMapping -OSCustomizationSpec $osCust


Remove-OSCustomizationSpec -OSCustomizationSpec 'osCustTemp' -Confirm:$false

2) I'm assuming you mean the NetworkName parameter on the New-VM cmdlet?
You should make the distinction between:
-  the creation of the VM, where the NetworkName defines to which portgroup the vNIC will be connected.
-  the customization of the guest OS. This happens when the VM is already created and during the 1st boot of the guest OS
So if you only use 1 vNIC on the VM (by only using 1 value on the NetworkName parameter), there will only be 1 vNIC that can be customized.


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

View solution in original post

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

Reply
0 Kudos
mtrohde
Enthusiast
Enthusiast
Jump to solution

LucD

Two questions.

1) I take it from the blog I want to edit my current guest OS specification? 

Set-OSCustomizationNicMapping –OSCustomizationSpec “Win2016 - VLAN 3” –IpMode UseStaticIP –IpAddress “192.168.0.100”


Can I leave the subnet mask, default gateway, etc that are already configured in my OS Customization or will the Set command blank them if not specified?

2) Will the -NetworkName change the current NIC to the defined Network or add a new NIC setting it to the specified value?  I want change the network not add another new.

Michael

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

1) No, unfortunately, you have to specify all of them (that's the way the cmdlet was written).

But you can do like this.
PS: I used parameter splatting to make the code more readable.

$osCustName = 'TestWin'

$osTempCustName = 'osCustTemp'

$newIP = '192.168.20.101'


$sCust = @{

   OSCustomizationSpec = $osCustName

   Name = $osTempCustName

   Type = 'NonPersistent'

   Confirm = $false

}

$osCust = New-OSCustomizationSpec @sCust


$osNic = Get-OSCustomizationNicMapping -OSCustomizationSpec $osCust

$sNic = @{

   OSCustomizationNicMapping = $osNic

   IpMode = 'UseStaticIP'

   IpAddress = $newIP

   SubnetMask = $osNic.SubnetMask

   Dns = $osNic.Dns

   DefaultGateway = $osNic.DefaultGateway

}

$osNic = Set-OSCustomizationNicMapping @sNic


Get-OSCustomizationNicMapping -OSCustomizationSpec $osCust


Remove-OSCustomizationSpec -OSCustomizationSpec 'osCustTemp' -Confirm:$false

2) I'm assuming you mean the NetworkName parameter on the New-VM cmdlet?
You should make the distinction between:
-  the creation of the VM, where the NetworkName defines to which portgroup the vNIC will be connected.
-  the customization of the guest OS. This happens when the VM is already created and during the 1st boot of the guest OS
So if you only use 1 vNIC on the VM (by only using 1 value on the NetworkName parameter), there will only be 1 vNIC that can be customized.


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

Reply
0 Kudos
mtrohde
Enthusiast
Enthusiast
Jump to solution

LucD,

1) OK so your code sample creates a temp OS customization specification and populates it with supplied hostname and IP address while pulling the rest of the network information from my current os customize specification.  The second to last line of your code that I show below, what is it doing in your code?

Get-OSCustomizationNicMapping -OSCustomizationSpec $osCust

I assume next I would want to create my VM.
New-VM -name TestVM -Template Win2016StdGui -OSCustomizationSpec osCustTemp

Last line deletes the no longer needed OS customization specification.

2) NetworkName as parameter on New-VM cmdlet, yes.  The template has a portgroup assigned to its nic.  When building VMs off the template using the Web Client, we select the check box to customize VM hardware and set the nic to use desired port group.  I want to reconfigure the NIC on my new VM cloned from template to desired portgroup And not end up with an additional NIC.

Michael

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I put that line there , so you could see that the IP address is actually changed, while the other parameters are the same.
And yes, in your case, you will have to remove that line, and add a New-VM line.

Btw, the temporary OSCustomizationSpec will be removed automatically at the end of the session.


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

Reply
0 Kudos
mtrohde
Enthusiast
Enthusiast
Jump to solution

LucD,

Thank you I have the information I need.  I am going to try to build a script to do exactly what I want.

Are you going to be at VMWorld this year?  If you are and doing a breakout session on PowerCLI would love to sign up for it.  Smiley Happy

Michael

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I submitted a session, but the agenda hasn't been decided yet.
If all goes well, yes, I will be there :smileycool:


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

Reply
0 Kudos