The command didn't work as the network label is not specified. Please refer the above screenshot which is taken from an esx host where this is already created. Like this I have to create vmkernel for new hosts manually from VC, which is time consuming task. :smileysad:
Network Label is pointing to the vmotion network for that particular cluster. Switch indicates the cluster name. So the network label would be ESXC1010-ESX-VMOTION-34(ESXC1010-MSL-APP-WINDOWS) as per the below screenshot.
In your command you have mentioned, $switchName = 'MySw'
Here in my case which should be the MySw value? I assumed it is vmk3 as per the above screen shot. I'm not sure.
PS C:\Windows\system32> $esxName = 'xyz.contoso.net'
$switchName = 'vmk3'
$pgName = 'vmotion'
$ip = 'x.x.x.x'
$mask = '255.255.254.0'
$esx = Get-VMHost -Name $esxName
$sw = Get-VirtualSwitch -VMHost $esx -Name $switchName
$pg = Get-VirtualPortGroup -Name $pgName -VirtualSwitch $sw
$sVmk = @{
VMHost = $esx
VirtualSwitch = $sw
PortGroup = $pg
VMotionEnabled = $true
IP = $ip
SubnetMask = $mask
Confirm = $false
}
New-VMHostNetworkAdapter @sVmk
Result:
Get-VirtualSwitch : 7/8/2020 9:22:11 AM Get-VirtualSwitch VirtualSwitch with name 'vmk3' was not found using the specified
filter(s).
At line:7 char:7
+ $sw = Get-VirtualSwitch -VMHost $esx -Name $switchName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-VirtualSwitch], VimException
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.GetVirtualSwitc
h
Get-VirtualPortGroup : Cannot validate argument on parameter 'VirtualSwitch'. The argument is null or empty. Provide an argument
that is not null or empty, and then try the command again.
At line:8 char:57
+ $pg = Get-VirtualPortGroup -Name $pgName -VirtualSwitch $sw
+ ~~~
+ CategoryInfo : InvalidData: (:) [Get-VirtualPortGroup], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.GetVirtualPortGroup
New-VMHostNetworkAdapter : Cannot bind parameter 'VirtualSwitch'. Cannot convert the "" value of type
"System.Management.Automation.PSCustomObject" to type "VMware.VimAutomation.ViCore.Types.V1.Host.Networking.VirtualSwitchBase".
At line:18 char:26
+ New-VMHostNetworkAdapter @sVmk
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-VMHostNetworkAdapter], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.NewVMHostNetworkAdapt
er