Hi Guys,
I have scenario where I am adding host to VDS with one Network adapter using below script which is working fine.
now using the same script if I add another Network then its giving me an error saying that host is already
param(
[String] $vcenter_fqdn,
[String] $vcenter_username,
[String] $vcenter_password,
[String] $newhostname,
[String] $domainname,
[String] $dvswitchname_1,
[String] $mac_group1_list
)
Connect-VIServer -Server "$vcenter_fqdn" -User "$vcenter_username" -Password "$vcenter_password" -Force
$esxihost = Get-VMHost "$newhostname.$domainname"
$dvswitch = Get-VDSwitch $dvswitchname_1
Add-VDSwitchVMHost -VMHost $esxihost -VDSwitch $dvswitch
$esxihostnic = Get-VMHost $esxihost | Get-VMHostNetworkAdapter -Physical | Where-Object {$_.Mac -eq "$mac_group1_list"}
Add-VDSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $esxihostnic -DistributedSwitch $dvswitch -Confirm:$false
Error---
Add-VDSwitchVMHost : 6/25/20 1:54:09 PM Add-VDSwitchVMHost VMHost 'host-prog.examlab.com' is already added to VDSwitch 'dvs-1'
At /ansible-vmware/vmwarehost-config-playbooks/nic1-mac1_nic2-mac1.ps1:17 char:1
+ Add-VDSwitchVMHost -VMHost $esxihost -VDSwitch $dvswitch
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Add-VDSwitchVMHost], AlreadyExists
+ FullyQualifiedErrorId : VdsImpl_VDSwitchServiceImpl_VMHostAlreadyAdded,VMware.VimAutomation.Vds.Commands.Cmdlets.AddVDSwitchVMHost
Now i was looking for to add the if condition to check the host if it is already present then dont try to add else add in DVS but not getting the command to find the host in DVS...
can someone please help..
Thanks
I have find something like this--- but now sure whether it is right method--
param(
[String] $vcenter_fqdn,
[String] $vcenter_username,
[String] $vcenter_password,
[String] $newhostname,
[String] $domainname,
[String] $dvswitchname_1,
[String] $mac_group1_list
)
Connect-VIServer -Server "$vcenter_fqdn" -User "$vcenter_username" -Password "$vcenter_password" -Force
$esxihost = Get-VMHost "$newhostname.$domainname"
$dvswitch = Get-VDSwitch $dvswitchname_1
$vd=Get-VMHost -Name "$newhostname.$domainname" | Get-VDSwitch
$vds_name=$vd.name
if($dvswitchname_1 -eq $vds_name) {
$esxihostnic = Get-VMHost $esxihost | Get-VMHostNetworkAdapter -Physical | Where-Object {$_.Mac -eq "$mac_group1_list"}
Add-VDSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $esxihostnic -DistributedSwitch $dvswitch -Confirm:$false
}else {
Add-VDSwitchVMHost -VMHost "$esxihost" -VDSwitch "$dvswitch"
$esxihostnic = Get-VMHost $esxihost | Get-VMHostNetworkAdapter -Physical | Where-Object {$_.Mac -eq "$mac_group1_list"}
Add-VDSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $esxihostnic -DistributedSwitch $dvswitch -Confirm:$false
}
Afaik, that is currently not yet possible with a cmdlet, you'll have to fall back to the API.
See for example Re: Assign vminc to Specific Uplink on VDS
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
i did not use the APIs and above script works for me..I dont know the internal but expectation was fulfill.
Thank you.