7 Replies Latest reply on May 30, 2019 9:05 AM by LucD

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

    mtrohde Novice
      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"

        • 2. Re: Trying to create VM from a template using a guest OS specification
          mtrohde Novice

          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

          • 3. Re: Trying to create VM from a template using a guest OS specification
            LucD Guru
            vExpertCommunity WarriorsUser Moderators

            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.

            • 4. Re: Trying to create VM from a template using a guest OS specification
              mtrohde Novice

              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

              • 5. Re: Trying to create VM from a template using a guest OS specification
                LucD Guru
                vExpertUser ModeratorsCommunity Warriors

                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.

                • 6. Re: Trying to create VM from a template using a guest OS specification
                  mtrohde Novice

                  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. 

                   

                  Michael

                  • 7. Re: Trying to create VM from a template using a guest OS specification
                    LucD Guru
                    Community WarriorsUser ModeratorsvExpert

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