I am trying to create vmkernels for existing ESXi hosts in vCenter using PowerCLI.
$esxiHosts = @("esxi21.v.lab", "esxi22.v.lab", "esxi23.v.lab", "esxi24.v.lab", "esxi25.v.lab")
$vmhosts = Get-VMHost -Name $esxiHosts
foreach ($i in 31..35)
{
$ipa = "192.168.2.$i"
write-host $vmhosts
$esxiHosts | foreach-object {New-VMHostNetworkAdapter $_ -PortGroup ManagementNetwork2 -IP $ipa -subnetmask 255.255.255.0 -VirtualSwitch vSwitch0 -ManagementTrafficEnabled $true}
}
It creates vmkernels for each host, however assigns all of them the same IP address, and then shows the following error as well.
New-VMHostNetworkAdapter : 26 Sep 2019 4:21:17 PM New-VMHostNetworkAdapter An error occurred during host configuration. Operation failed, diagnostics report: A vmkernel
nic for the connection point already exists:
At line:5 char:30
+ ... ach-object {New-VMHostNetworkAdapter $_ -PortGroup ManagementNetwork2 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-VMHostNetworkAdapter], PlatformConfigFault
+ FullyQualifiedErrorId : Client20_VmHostServiceImpl_NewVMHostNetworkAdapter_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.NewVMHostNetworkAdapter
My understanding is this is due to the foreach-object running inside the loop, but if I don't do that I cannot iterate the IP addresses using the just the for each loop, or can I ?
One way to go about correcting this issue might be to use a counter variable, since your 4th octet is sequential. Something like the following:
$esxiHosts = @("esxi21.v.lab", "esxi22.v.lab", "esxi23.v.lab", "esxi24.v.lab", "esxi25.v.lab")
$vmhosts = Get-VMHost -Name $esxiHosts
$i = 31
foreach ($vmh in $vmhosts) {
$ipa = "192.168.2.$i"
Write-Host $vmh
New-VMHostNetworkAdapter -VMHost $vmh -PortGroup ManagementNetwork2 -IP $ipa -subnetmask 255.255.255.0 -VirtualSwitch vSwitch0 -ManagementTrafficEnabled $true
$i++
}