VMware Cloud Community
hxman101
Contributor
Contributor

Set-OSCustomizationNicMapping Not Setting IP's From csv

The following script doesn't seem to be setting a static IP from the csv file. Any ideas as to what I should check?

 

# Define the naming convention for the virtual machines.
$vmNameTemplate = "Test123-{0:D3}"

# Save the cluster in which the virtual machines should be created into a variable.
$cluster = Get-Cluster test-cluster

# Save the template on which the virtual machines should be based into a variable.
$template = Get-Template test123

# Save the static IP addresses from the stored CSV file into a variable.
$staticIpList = Import-CSV C:\StaticIPs.csv

# Create the vitrual machines.
$vmList = @()
    
for ($i = 1; $i –le 2; $i++) {
    $vmName = $vmNameTemplate –f $i
    $vmList += New-VM –Name $vmName –ResourcePool $cluster –Template $template
}

 

# Create the customization specification.
$linuxSpec = New-OSCustomizationSpec –Name test-custom –Domain test.com –DnsServer "192.168.10.1" –NamingScheme VM –OSType Linux

# Clone the customization specification to a nonpersistent type.
$specClone = New-OSCustomizationSpec –Spec $linuxSpec –Type NonPersistent

# Apply the customization specification to each virtual machine.
for ($i = 0; $i –lt $vmList.Count; $i++) {
    # Acquire a new static IP from the list
    $ip = $staticIpList[$i].IP
    
    # The specification has a default NIC mapping – retrieve it and update it with the static IP
    $nicMapping = Get-OSCustomizationNicMapping –OSCustomizationSpec $specClone
    $nicMapping | Set-OSCustomizationNicMapping –IpMode UseStaticIP –IpAddress $ip –SubnetMask "255.255.255.0" –DefaultGateway "192.168.10.1"
    
    # Apply the customization
    Set-VM –VM $vmList[$i] –OSCustomizationSpec $specClone –Confirm:$false
}


Write-Host ""

Disconnect-VIServer -Confirm:$false

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership

What exactly do you have in that CSV file?

A single column with the name IP?


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

Reply
0 Kudos
hxman101
Contributor
Contributor

Yes....

 

IP

192.168.xxx.xxx

192.168.xxx.xxx

 

All in different cells of course....

Reply
0 Kudos
LucD
Leadership
Leadership

It's not clear from you script, but do you actually power on the VM, and is the OSCustomization running at that point?

Did you check the OSCustomization logs, as documented in KB1026317


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

Reply
0 Kudos
hxman101
Contributor
Contributor

No they are not powered on at  this point.
Reply
0 Kudos
LucD
Leadership
Leadership

You do realise that the OS customisation runs at the 1st power on?


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

Reply
0 Kudos
hxman101
Contributor
Contributor

Yes, however I am only creating 1 vm to test the script and I powered it on manually and nothing happened. I added a line in the script to automatically do this, but it's still not working for some reason.....
Reply
0 Kudos
LucD
Leadership
Leadership

The logs I mentioned earlier should contain clues why it didn't do anything.

If there are no logs something fundamentally went wrong. In that case have a look at the vmware.log of the VM.


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

Reply
0 Kudos