mrfite
asked on
Trouble with adding nics to a Virtual Distributed Switch with PowerCli
We have a massive project to rename many of our ESXi hosts in VCenter. I am having issue with a powercli command to handle it.
When I run the Whatif - I get the results I expect:
get-vdswitch -name $net.switch | Add-VDSwitchPhysicalNetwor kAdapter -VMHostNetworkAdapter $PhysicalAdapters -Confirm:$false -WhatIf
What if: Performing the operation "Adding physical network adapter(s) 'vmnic5,vmnic7'" on target "vDS-NCE-VRA".
When I remove the Whatif - The system gives me an error:
get-vdswitch -name $net.switch | Add-VDSwitchPhysicalNetwor kAdapter -VMHostNetworkAdapter $PhysicalAdapters -Confirm:$false
Add-VDSwitchPhysicalNetwor kAdapter : 8/29/2019 7:19:03 AM Add-VDSwitchPhysicalNetwor kAdapter The object 'vim.HostSystem:host-62771 6' has already been deleted or has not been completely created
At line:1 char:35
+ ... t.switch | Add-VDSwitchPhysicalNetwor kAdapter -VMHostNetworkAdapter ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~
+ CategoryInfo : NotSpecified: (:) [Add-VDSwitchPhysicalNetwo rkAdapter] , ManagedObjectNotFound
+ FullyQualifiedErrorId : Core_VirtualNetworkService Impl_Remov eVDSwitchP hysicalNet workAdapte r_UpdateVi ewData,VMw are.VimAut omation.Vd s.Commands .Cmdlets.A ddVDSwitch PhysicalNe tworkAdapt er
Below is the script I am using. It does successfully record the current Distributed Switch Information, Removes the Distributed Switch, removes the Esxi Host from VCENTER, Readds the ESXI host to vcenter, Readds the Virtual Distributed Switch. - It fails at adding the ESXi Host Nics to the Distributed Switch.
PowerCLI Version
----------------
VMware PowerCLI 11.3.0 build 13990089
$PSVersionTable
Name Value
---- -----
PSVersion 5.1.17763.592
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17763.592
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
Here is the code I am using:
When I run the Whatif - I get the results I expect:
get-vdswitch -name $net.switch | Add-VDSwitchPhysicalNetwor
What if: Performing the operation "Adding physical network adapter(s) 'vmnic5,vmnic7'" on target "vDS-NCE-VRA".
When I remove the Whatif - The system gives me an error:
get-vdswitch -name $net.switch | Add-VDSwitchPhysicalNetwor
Add-VDSwitchPhysicalNetwor
At line:1 char:35
+ ... t.switch | Add-VDSwitchPhysicalNetwor
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Add-VDSwitchPhysicalNetwo
+ FullyQualifiedErrorId : Core_VirtualNetworkService
Below is the script I am using. It does successfully record the current Distributed Switch Information, Removes the Distributed Switch, removes the Esxi Host from VCENTER, Readds the ESXI host to vcenter, Readds the Virtual Distributed Switch. - It fails at adding the ESXi Host Nics to the Distributed Switch.
PowerCLI Version
----------------
VMware PowerCLI 11.3.0 build 13990089
$PSVersionTable
Name Value
---- -----
PSVersion 5.1.17763.592
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17763.592
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
Here is the code I am using:
$CurrentDate = Get-Date -format "MM-dd-yy"
$CurrentDate = [String]$CurrentDate
$CurrentTime = Get-Date -Format "HH:MM tt"
$ExportLogPath = "c:\temp"
$ExportError = "Errors-$($CurrentDate).txt"
# Create Directory if it does not exsist
Write-host "Checking for existing files and folders....."
if ((test-path $ExportLogPath) -eq $False)
{
New-Item -ItemType "Directory" -Path $ExportLogPath | Out-Null
Write-host "Created new directory " $ExportLogPath
}
ELSE { Write-host $ExportLogPath " already exsists" }
$vcenters = 'd05nutlp0002.domain.local'
$rootcreds = (Get-Credential -Message "ESXi root password is required" -UserName 'root')
$creds = (Get-Credential -Message "Provide your Vcenter Credentials")
Connect-VIServer -Server $vCenter -Credential $creds
$location = Get-VMHost | Select-Object location
$vmhosts = 'ncepevraxxh0020.domain.local'
$vmhosts = get-vmhost $vmhosts
$nets =@()
foreach ($vmhost in $vmhosts){
$location = $vmhost | Select-Object Parent
$VDSs = $vmhost | Get-VDSwitch | Sort-Object name
$esxihostname = $vmhost.name
Write-Output "Probing Host $($vmhost.name)"
foreach ($VDS in $VDSs){
Write-Output "Probing Switch $($VDS)"
$UplinkPortGroups = $VDS | Get-VDPort -Uplink | Where-Object {$_.ProxyHost.Name -eq $ESXiHostName} | Sort-Object Name
$PhysicalAdapters = $VDS | Get-VMHostNetworkAdapter -Physical | Where-Object {$_.VMHost.Name -eq $ESXiHostName} | Sort-Object Name
$VMKernelAdapters = $VDS | Get-VMHostNetworkAdapter -VMKernel | Where-Object {$_.VMHost.Name -eq $ESXiHostName} | Sort-Object Name
$VDPortGroup = $VDS | Get-VDPortgroup
$PNicInfo = $UplinkPortGroups | Select-Object @{ l = 'Change'; e = { "Before" } },Proxyhost,Switch,ConnectedEntity,Name
$nets += $PNicInfo
$VDS | Remove-VDSwitchVMHost -VMHost $esxihostname -Confirm:$false
}
}
#Remove from VCenter
Set-VMHost -VMHost $VMHost -State 'Disconnected' -Confirm:$false -ErrorAction 'continue' | Remove-VMHost -Confirm:$false -ErrorAction 'stop'
Start-Sleep 5
Add-VMHost -Name $VMHost -Server $vCenter -Location $vmhost.Parent -Credential $rootcreds -Force -Confirm:$false -ErrorAction 'Stop'
Start-sleep 5
#after Rejoin to vcenter
foreach ($net in $nets){
$vmhost = get-vmhost $net.ProxyHost
get-vdswitch -name $net.switch | Add-vdswitchvmhost -vmhost $vmhost -Confirm:$false
get-vdswitch -name $net.switch | Add-VDSwitchPhysicalNetworkAdapter -VMHostNetworkAdapter $PhysicalAdapters -Confirm:$false
}
$nets2=@()
foreach ($vmhost in $vmhosts){
$VDSs = Get-VMHost -Name $vmhost | Get-VDSwitch | Sort-Object name
$esxihostname = $vmhost.name
Write-Output "Probing Host $($vmhost.name)"
foreach ($VDS in $VDSs){
Write-Output "Probing Switch $($VDS)"
$UplinkPortGroups2 = $VDS | Get-VDPort -Uplink | Where-Object {$_.ProxyHost.Name -eq $ESXiHostName} | Sort-Object Name
$PhysicalAdapters2 = $VDS | Get-VMHostNetworkAdapter -Physical | Where-Object {$_.VMHost.Name -eq $ESXiHostName} | Sort-Object Name
$VMKernelAdapters2 = $VDS | Get-VMHostNetworkAdapter -VMKernel | Where-Object {$_.VMHost.Name -eq $ESXiHostName} | Sort-Object Name
$VDPortGroup2 = $VDS | Get-VDPortgroup
$PNicInfo2 = $UplinkPortGroups2 | Select-Object @{ l = 'Change'; e = { "After" } },Proxyhost,Switch,ConnectedEntity,Name
$nets2 += $PNicInfo2
}
}
$comp = Compare-Object -ReferenceObject $nets -DifferenceObject $nets2 -IncludeEqual -PassThru -ExcludeDifferent
$comp | Export-csv -path 'c:\temp\DSW-Migration.csv' -NoTypeInformation
Disconnect-VIServer * -Force -confirm:$false
}
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.