VMware Cloud Community
Joisrm
Contributor
Contributor
Jump to solution

vSphere 6.5 powercli OSCustomizationSpec 2nd IP

I hope you all are well!

I am deploying several Linux vm's using PowerCli, OSCustomizationSpec, a template and a csv file. The template is off another vm that has only 1 nic. I am able to create the new vm's and change their IP, submask and gateway successfully.

The csv file headers are the following:

Name,Cluster,Template,Customization,IP,SubnetMask,DefaultGateway

And the working code for these with only one nic is the following:

#Connect-VIServer -Server 192.168.100.1 -User xxxxxxxx -Password xxxxxxxxx
#Disconnect-VIServer -Server 192.168.100.1 -Confirm:$False
#Create the linux VM from template
$vms = Import-Csv -Path "C:\Scripts\DeployVMTest.csv"
$OSSpec = New-OSCustomizationSpec –Name LinuxCus –Domain local –DnsServer “192.168.100.1” –OSType Linux –Type NonPersistent
foreach ($vm in $vms)
{
Write-Warning "Creating $($vm.Name) in $($vm.cluster)"
New-VM -Name $vm.Name -ResourcePool $vm.Cluster -Template $vm.Template -RunAsync
$nicMapping = Get-OSCustomizationNicMapping –OSCustomizationSpec LinuxCus
$nicMapping | Set-OSCustomizationNicMapping –IpMode UseStaticIP –IpAddress $vm.IP –SubnetMask “255.255.255.0” –DefaultGateway “192.168.100.1”
# Customize the Guest VM
Start-Sleep -Seconds 5
Set-VM $vm.name -OSCustomizationSpec LinuxCus -Confirm:$False
$VMName = $vm.Name
Start-VM $VMName
}
Get-OSCustomizationSpec -Name 'LinuxCus' -ErrorAction Stop | Remove-OSCustomizationSpec -Confirm:$false

Clear-Variable -Name "vms"
Clear-Variable -Name "OSSpec"
Clear-Variable -Name "vm"
Clear-Variable -Name "nicMapping"
Clear-Variable -Name "VMName"

My issue is that this wont work on vm's that have two nic's. I need to modify the above code to also configure the 2nd nic which is also static and a different network e.g. 10.10.10.10.

I have been looking for and not able to find anything on deploying vm's with 2 nics and and setting the network details with PowerCLI

Any help is appreciated.

Regards,

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That is where the Position parameter on the Set-OSCustomizationNicMapping cmdlet comes into play.

If your OSCustomizationSpec only has 1 vNIC mapping, you have to add additional ones with one or more New-OSCustomizationNicMapping calls. Don't forget to use the Position parameter on there as well.


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

View solution in original post

Reply
0 Kudos
9 Replies
LucD
Leadership
Leadership
Jump to solution

That is where the Position parameter on the Set-OSCustomizationNicMapping cmdlet comes into play.

If your OSCustomizationSpec only has 1 vNIC mapping, you have to add additional ones with one or more New-OSCustomizationNicMapping calls. Don't forget to use the Position parameter on there as well.


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

Reply
0 Kudos
Joisrm
Contributor
Contributor
Jump to solution

Spoiler
My level of PowerCLI is basic. I have been looking into how to add another nic as you suggest, but no luck.

Would it be possible to provide an example.

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

What exactly did you try?
And which error did you get, if any?


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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You misspelled Position


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

Reply
0 Kudos
Joisrm
Contributor
Contributor
Jump to solution

-Position is written correctly  in the code. 

Posiotion is shown only in the error log.

Not sure why.

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You removed the code, can't check anymore.
But I'm pretty sure the error wouldn't state this when the code was correct.


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

Reply
0 Kudos
Joisrm
Contributor
Contributor
Jump to solution

I apologize, I was correcting the code. The miss spell must have been due to the copy paste from my VM to my physical.

Here's the code again.

I had forgotten to paste the changes and errors:

#Connect-VIServer -Server 192.168.100.1 -User xxxxxxxx -Password xxxxxxxxx
#Disconnect-VIServer -Server 192.168.100.1 -Confirm:$False
#Create the linux VM from template
$vms = Import-Csv -Path "C:\Scripts\DeployVMTest.csv"
$OSSpec = New-OSCustomizationSpec –Name LinuxCus –Domain local –DnsServer “192.168.100.1” –OSType Linux –Type NonPersistent
$OSSpec2 = New-OSCustomizationNicMapping –OSCustomizationSpec LinuxCustomization –IpMode UseStaticIP –IpAddress “10.100.0.2” –SubnetMask “255.255.240.0” –DefaultGateway “10.100.0.1”

foreach ($vm in $vms)
{
Write-Warning "Creating $($vm.Name) in $($vm.cluster)"
New-VM -Name $vm.Name -ResourcePool $vm.Cluster -Template $vm.Template -RunAsync
$nicMapping = Get-OSCustomizationNicMapping –OSCustomizationSpec LinuxCus
$nicMapping | Set-OSCustomizationNicMapping -Position 1 –IpMode UseStaticIP –IpAddress $vm.IP –SubnetMask “255.255.255.0” –DefaultGateway “192.168.100.1”

$nicMapping = Get-OSCustomizationNicMapping –OSCustomizationSpec LinuxCus
$nicMapping | Set-OSCustomizationNicMapping -Position 2 –IpMode UseStaticIP –IpAddress $vm.IP2 –SubnetMask “255.255.240.0” –DefaultGateway “10.100.0.1”

# Customize the Guest VM
Start-Sleep -Seconds 5
Set-VM $vm.name -OSCustomizationSpec LinuxCus -Confirm:$False
$VMName = $vm.Name
Start-VM $VMName
}
Get-OSCustomizationSpec -Name 'LinuxCus' -ErrorAction Stop | Remove-OSCustomizationSpec -Confirm:$false

Clear-Variable -Name "vms"
Clear-Variable -Name "OSSpec"
Clear-Variable -Name "vm"
Clear-Variable -Name "nicMapping"
Clear-Variable -Name "VMName"


Here are the errors:

Set-OSCustomizationNicMapping : 12/29/2021 1:58:41 PM Set-OSCustomizationNicMapping The property Posiotion of the object has been changed. This property represents a part of the Id of the object so this
operation is ambiguous. Please get object again and run the operation.
At line:20 char:15
+ ... icMapping | Set-OSCustomizationNicMapping -Position 2 –IpMode UseStat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [Set-OSCustomizationNicMapping], VimException
+ FullyQualifiedErrorId : Client20_ObjectVersionService_VerifyIsCurrentVersion_ExpiredObject,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMapping

Set-VM : 12/29/2021 1:58:47 PM Set-VM The OsCustomization object, that you're trying to apply, contains a different number of NIC customization settings than the VirtualMachine's network adapter count.
At line:25 char:1
+ Set-VM $vm.name -OSCustomizationSpec LinuxCus -Confirm:$Fal ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Linux1:VirtualMachineImpl) [Set-VM], InvalidArgument
+ FullyQualifiedErrorId : Client20_VmHostServiceImpl_CustomizeVM_NetworkAdapterCountMismatch,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetVM

 

Set-OSCustomizationNicMapping : 12/29/2021 1:58:48 PM Set-OSCustomizationNicMapping The property Posiotion of the object has been changed. This property represents a part of the Id of the object so this
operation is ambiguous. Please get object again and run the operation.
At line:20 char:15
+ ... icMapping | Set-OSCustomizationNicMapping -Position 2 –IpMode UseStat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [Set-OSCustomizationNicMapping], VimException
+ FullyQualifiedErrorId : Client20_ObjectVersionService_VerifyIsCurrentVersion_ExpiredObject,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMapping

Set-VM : 12/29/2021 1:58:54 PM Set-VM The OsCustomization object, that you're trying to apply, contains a different number of NIC customization settings than the VirtualMachine's network adapter count.
At line:25 char:1
+ Set-VM $vm.name -OSCustomizationSpec LinuxCus -Confirm:$Fal ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Linux2:VirtualMachineImpl) [Set-VM], InvalidArgument
+ FullyQualifiedErrorId : Client20_VmHostServiceImpl_CustomizeVM_NetworkAdapterCountMismatch,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetVM


Set-OSCustomizationNicMapping : 12/29/2021 1:58:54 PM Set-OSCustomizationNicMapping The property Posiotion of the object has been changed. This property represents a part of the Id of the object so this
operation is ambiguous. Please get object again and run the operation.
At line:20 char:15
+ ... icMapping | Set-OSCustomizationNicMapping -Position 2 –IpMode UseStat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [Set-OSCustomizationNicMapping], VimException
+ FullyQualifiedErrorId : Client20_ObjectVersionService_VerifyIsCurrentVersion_ExpiredObject,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizationNicMapping

Set-VM : 12/29/2021 1:59:01 PM Set-VM The OsCustomization object, that you're trying to apply, contains a different number of NIC customization settings than the VirtualMachine's network adapter count.
At line:25 char:1
+ Set-VM $vm.name -OSCustomizationSpec LinuxCus -Confirm:$Fal ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Linux3:VirtualMachineImpl) [Set-VM], InvalidArgument
+ FullyQualifiedErrorId : Client20_VmHostServiceImpl_CustomizeVM_NetworkAdapterCountMismatch,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetVM

 

regards,

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could do something like this

#Create the linux VM from template
$vms = Import-Csv -Path "C:\Scripts\DeployVMTest.csv"

$OSSpec = New-OSCustomizationSpec -Name LinuxCus -Domain local -DnsServer “192.168.100.1” -OSType Linux -Type NonPersistent

foreach ($vm in $vms)
{
    Write-Warning "Creating $($vm.Name) in $($vm.cluster)"

    # Clean up old entries
    Get-OSCustomizationNicMapping -OSCustomizationSpec $osspec | 
    Sort-Object -Property Position -Descending | 
    ForEach-Object -Process { 
        Remove-OSCustomizationNicMapping -OSCustomizationNicMapping $_ -Confirm:$false
    }

    # Create new NIC entries
    $nic1 = New-OSCustomizationNicMapping -OSCustomizationSpec $OSSpec -Position 1 -IpMode UseStaticIP -IpAddress $vm.IP -SubnetMask 255.255.255.0 -DefaultGateway 192.168.100.1
    $nic2 = New-OSCustomizationNicMapping -OSCustomizationSpec $OSSpec -Position 2 -IpMode UseStaticIP -IpAddress $vm.IP2 -SubnetMask 255.255.240.0 -DefaultGateway 10.100.0.1

    # Create VM
    New-VM -Name $vm.Name -ResourcePool $vm.Cluster -Template $vm.Template -RunAsync -OSCustomizationSpec $OSSpec

    # Customize the Guest VM
    Start-Sleep -Seconds 5
    Start-VM $vm.Name
}


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

Reply
0 Kudos
Joisrm
Contributor
Contributor
Jump to solution

Hi LucD,

Your code was assigning the same IP to all the nics.

So I made some changes to my code and ran it, and it assigned the corresponding IP's to each nic's on each of the 3 test vm's that were created.

The changes I made to the code were:

Removed text -Position 1 from this section:

Edited on 01/09/2022: I am providing the final code in case anyone else might need it:

#Connects to the vCenter Server
Connect-VIServer -Server xxx.xxx.xxx.xxx -User xxxxxxxx@vsphere.local -Password xxxxxxx
#
$errorActionPreference = "SilentlyContinue"
$vmsVerify = Import-Csv -Path "C:\path\Deploy_VMs_2NIC.csv"
$Erronum = 0
$NewVMCount = 0
#
#Here it verifies if the VM name exists in the Vsphere inventory. 
foreach ($vmverify in $vmsVerify) 
{
    $Exists = get-vm -name $vmverify.Name
    if(-not $Exists) 
    {
        Write-Host $vmverify.Name -NoNewline; Write-Host -ForegroundColor green " √"
    }
        else
    {
        $Erronum++
        Write-Host -ForegroundColor yellow $vmverify.Name -NoNewline; Write-Host -ForegroundColor red " X"
    }
}
#
#If the variable is equal or greater than one
if($Erronum -ge 1)
    {
        Write-Host
        Write-Host -ForegroundColor yellow "Virtual Machine name already exist! Please verify your data."
        Clear-Variable -Name "errorActionPreference"
        Clear-Variable -Name "Erronum"
        Clear-Variable -Name "Exists"
        Clear-Variable -Name "vms"
        Clear-Variable -Name "vmsVerify"
        Clear-Variable -Name "vmverify"
        Clear-Variable -Name "NewVMCount"
        Clear-Variable -Name "OSSpec"
        Clear-Variable -Name "vm"
        Clear-Variable -Name "nicMapping"
        Clear-Variable -Name "nicMapping2"
        Disconnect-VIServer -Server xxx.xxx.xxx.xxx -Confirm:$False
        Break
    }
    else
    {
        Write-Host
        Write-Host
        $errorActionPreference = "SilentlyContinue"
        #Connects to the vCenter Server
        Connect-VIServer -Server xxx.xxx.xxx.xxx -User xxxxxxxx@vsphere.local -Password xxxxxxx
        #Create the linux VM,s from template
        $vms = Import-Csv -Path "C:\path\Deploy_VMs_2NIC.csv"
        #Creates the customizing for nic 1
        $OSSpec = New-OSCustomizationSpec –Name LinuxCus2NIC –Domain local –DnsServer “xxx.xxx.xxx.xxx” –OSType Linux –Type NonPersistent 
        #Creates the customizing for nic 2
        $OSSpec2 = New-OSCustomizationNicMapping –OSCustomizationSpec LinuxCus2NIC –IpMode UseStaticIP –IpAddress “10.0.0.1” –SubnetMask “255.0.0.0” –DefaultGateway “0.0.0.0”
        #Starts the creation of the vm's
        
            foreach ($vm in $vms) 
            {
                $NewVMCount++
                #Displays a message
                Write-Host "Creating: " -ForegroundColor yellow -NoNewline; Write-Host "$($vm.Name) " -ForegroundColor Cyan -NoNewline; Write-Host "in " -ForegroundColor Yellow -NoNewline; Write-Host "$($vm.cluster) " -ForegroundColor Cyan 
                #Asigns the header from the csv files
                New-VM -Name $vm.Name -ResourcePool $vm.Cluster -Template $vm.Template
                #Sets the custimizing for nic 1
                $nicMapping = Get-OSCustomizationNicMapping –OSCustomizationSpec LinuxCus2NIC 
                $nicMapping | Set-OSCustomizationNicMapping –IpMode UseStaticIP –IpAddress $vm.IP –SubnetMask “255.255.255.0” –DefaultGateway “xxx.xxx.xxx.xxx”
                #Sets the custimizing for nic 2
                $nicMapping2 = Get-OSCustomizationNicMapping –OSCustomizationSpec LinuxCus2NIC
                $nicMapping2 | Set-OSCustomizationNicMapping -Position 2 –IpMode UseStaticIP –IpAddress $vm.IP2 –SubnetMask “255.0.0.0” –DefaultGateway “0.0.0.0”
                Write-Host "Configuring settings on " -ForegroundColor yellow -NoNewline; write-host $($vm.name) -ForegroundColor Cyan -NoNewline
                #
                Start-Sleep -Seconds 30

                #Customize the Guest VM
                Set-VM $vm.name -OSCustomizationSpec LinuxCus2NIC -Confirm:$False

                #Starts the vm's
                Start-VM  $vm.Name | Wait-Tools

                $folder = Get-Folder -Name $vm.Folder -Type VM
                Move-VM -VM $vm.Name -InventoryLocation $folder -Confirm:$false
            }
      }

    if($NewVMCount -eq 1)
        {
            Write-Host $NewVMCount -ForegroundColor yellow -NoNewline; Write-Host " vm " -ForegroundColor Cyan -NoNewline; Write-Host "has been created." -ForegroundColor yellow
        }
            else
        {
            Write-Host $NewVMCount -ForegroundColor yellow -NoNewline; Write-Host " vm's " -ForegroundColor Cyan -NoNewline; Write-Host "have been created." -ForegroundColor yellow
        }

Write-Host
Write-Host "Completed " -ForegroundColor green

Clear-Variable -Name "errorActionPreference"
Clear-Variable -Name "Erronum"
Clear-Variable -Name "Exists"
Clear-Variable -Name "vms"
Clear-Variable -Name "vmsVerify"
Clear-Variable -Name "vmverify"
Clear-Variable -Name "NewVMCount"
Clear-Variable -Name "OSSpec"
Clear-Variable -Name "vm"
Clear-Variable -Name "nicMapping"
Clear-Variable -Name "nicMapping2"
Clear-Variable -Name "vmsMove"
Clear-Variable -Name "vmMove"
Clear-Variable -Name "folder"
Get-OSCustomizationSpec -Name 'LinuxCus2NIC' -ErrorAction Stop | Remove-OSCustomizationSpec -Confirm:$false

#Disconnects from the vCenter Server
Disconnect-VIServer -Server xxx.xxx.xxx.xxx -Confirm:$False

I will be accepting your first answer as it did point me in the right direction and help me solve my issue.

Keep safe.

Regards

Reply
0 Kudos