VMware Cloud Community
Ganapathy
Contributor
Contributor

How to Set IP for new VM

With the below script i am able to deploy VM but i am unable to set IP in new VM...

Could you please assist me in it.

Import-Csv "C:\PS\Serverdeploy\newserverdeploy.csv" -UseCulture | %{
  $viserver = Read-Host "Enter VC Name"
 
  Connect-VIServer $viserver
 
  $Domainuser = $Domainname +"\"+$Domainusername

$Randomhost = Get-Cluster $_.clustername | Get-VMHost | get-random
$portgroup = $Randomhost | Get-VirtualPortGroup | select name

$portgroup | ForEach-Object {if ($_.Name -match $_.inputportgrp) {
$Networkname1 = $_.name
Write-Host $networkname1
}}
  
$customspec =   Get-OSCustomizationSpec $_.customization | Set-OSCustomizationSpec -domain "enterprisenet.org" -domaincredentials (get-credential)
Get-OSCustomizationNicMapping -spec $_.customization | where { $_.Position -eq '1'} | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $_.ip1 -SubnetMask $_.subnet1 -DefaultGateway $_.Gateway1 -Dns $_.dns1,$_.dns2

  New-vm -Name $_.ServerName -VMhost $randomhost -Template $_.Template -Datastore $_.Datastore -OSCustomizationspec $customspec

Get-NetworkAdapter -VM $_.ServerName | where {$_.Name -match "1"} | Set-NetworkAdapter -NetworkName $Networkname1 -Connected:$true -StartConnected:$true -Confirm:$false

Start-VM $_.servername
Start-Sleep -Seconds 360


}

0 Kudos
8 Replies
mattboren
Expert
Expert

Hello, Ganapathy-

It looks like you are setting the OSCustomizationNicMapping properly -- I do not see a problem there.  To help locate the problem, there are a few places that you can check that may hold additional info about what happened:

  1. in the vSphere client, on the Task & Events tab, in the Events view, there may be entries about the customization process
  2. within the VM guest OS (presumably Windows, since you are using domain info in the cust spec), check the log file "C:\WINDOWS\TEMP\vmware-imc\guestcust.log"
  3. within the VM guest OS, the Windows Event log

Do those hold any information as to what is happening/failing for you?

Another thing that seems like a possible source for the problem is the CSV file itself.  You have likely checked already, but the column headers all match the property names that you are using in your script, correct?

As far as the VM that results from your script -- in what state is the OS after deployment?  Does it successfully get renamed/customized as expected, and just the IP address portion is failing?  Or, are there other things that fail?

      0 Kudos
      LucD
      Leadership
      Leadership

      Another point to check, are you using the 32-bit or the 64-bit version of PowerCLI to run this script ?

      The VIX-based functions require the 32-bit version.


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

      0 Kudos
      Ganapathy
      Contributor
      Contributor

      There was an eror in this line (bolded the word)

      $portgroup | ForEach-Object {if ($_.Name -match $_.inputportgrp) {

      $Networkname1

      = $_.name

      Write-Host

      $networkname1

      }}

      After assigning that to a variable it assigns IP to the Server. Now i am facing another issue it's unable to join to domain...

      Server is up with network but not in domain!!! Could you please assist LUcD

      0 Kudos
      damiankarlson
      Enthusiast
      Enthusiast

      What's the format of the domain credentials you're getting? Is it username@domain.com or domain\username?

      Twitter: @sixfootdad Blog: damiankarlson.com Podcast: professionalvmware.com/brownbags
      0 Kudos
      Ganapathy
      Contributor
      Contributor

      Thanks for your reply...Its domain\user.

      It's working on widnows 2003 but not on windows 2008 as SYS PREP differs...Any idea / suggestion please ?

      0 Kudos
      damiankarlson
      Enthusiast
      Enthusiast

      Try username@domain.com. I was having the same issue using downlevel user\domain on 2008.

      Twitter: @sixfootdad Blog: damiankarlson.com Podcast: professionalvmware.com/brownbags
      0 Kudos
      BrentCocjhran
      Contributor
      Contributor

      Found this thread and have 'borrowed' some code to get the NIC configured on some VMs at deploy.  What I'd prefer to do for OS customization is to apply an existing customization spec, and just edit the NIC portion.  That way, I don't have to worry about passing product keys and domain credentials in the scripts.  Is there a way for me to do that on the fly?  Using the Set-OSCustomizationNicMapping cmd, the stored customization spec is altered with the NIC settings that I only want to apply to a single VM.

      Here's the powershell snippet that likely needs to be modified:

      *********************************************************************************************************

      Import-CSV C:\PowerShell\Import-CSV\IP_input\Deploy_withIP.csv | Foreach {
      #
      New-VM -vmhost $_.esxhost -Name $_.vmname -Template $_.templatename -Datastore $_.datastore -DiskStorageFormat thick -Location $_.folder -OSCustomizationSpec $_.customspec

      Get-OSCustomizationNicMapping -spec $_.customspec | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $_.ip -SubnetMask $_.netmask -DefaultGateway $_.gw -Dns $_.dns1,$_.dns2

      *********************************************************************************************************

      Thanks in advance PowerCLI gurus!

      Brent

      0 Kudos
      LucD
      Leadership
      Leadership

      Also see my answer to your other question.

      You better create a clone of the OSCustomizationSpec and work on that one.

      That way the original is not changed each time.


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

      0 Kudos