Hi,
new to powershell and powercli scripting, so sorry for stupid beginners mistakes.
I found a similar discussion (https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Mac-Address-error-when-using-the-New-V...) but its not the same issue (i dont have spaces in my strings).
I am trying to populate a dvswitch for a new environment and fail on the scripted version of New-VMHostNetworkAdapter
Here is the code snippet that i run ( and totally off topic, why is Powershell not one of the code options? )
Write-Host pg=$pg, vmk=$vmk, vlan=$vlan, extraoptions=$pgo
Write-Host New-VMHostNetworkAdapter -Verbose -VMHost $hostip -VirtualSwitch $dvs -PortGroup $pg -IP $vmkip -SubnetMask $SubnetMask $options -Mtu $MTu
New-VMHostNetworkAdapter -Verbose -VMHost $hostip -VirtualSwitch $dvs -PortGroup $pg -IP $vmkip -SubnetMask $SubnetMask $options -Mtu $MTu
($options is empty for this vmk as it has no traffic type associated, values of the other variables can be seen below in the cli output)
I run the script in Poweshell prompt or ISE, both have the same error:
pg=DS_vmk0_NFS_vlan10 vmk=vmk0 vlan=10 extraoptions=vmk0
New-VMHostNetworkAdapter -VMHost 10.0.1.11 -VirtualSwitch DSwitch -PortGroup DS_vmk0_NFS_vlan10 -IP 10.0.10.11 -SubnetMask 255.255.255.0 -Mtu 9000
AUSFÜHRLICH: Performing operation 'Creating new VM host network adapter.' on host '10.0.1.11'
New-VMHostNetworkAdapter : 16.04.2022 13:22:52 New-VMHostNetworkAdapter An error occurred during host configuration. Operation failed, diagnostics report: Invalid MAC address
In Zeile:106 Zeichen:4
+ New-VMHostNetworkAdapter -Verbose -VMHost $hostip -Virtua ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-VMHostNetworkAdapter], PlatformConfigFault
+ FullyQualifiedErrorId : Client20_VmHostServiceImpl_NewVMHostNetworkAdapter_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.NewVMHostNetworkAdapter
AUSFÜHRLICH: 16.04.2022 13:22:52 New-VMHostNetworkAdapter Finished execution
Now if I run the same command line manually its working perfectly fine
New-VMHostNetworkAdapter -VMHost 10.0.1.11 -VirtualSwitch DSwitch -PortGroup DS_vmk0_NFS_vlan10 -IP 10.0.10.11 -SubnetMask 255.255.255.0 -Mtu 9000
Name Mac DhcpEnabled IP SubnetMask DeviceName
---- --- ----------- -- ---------- ----------
vmk0 00:50:56:62:23:f5 False 10.0.10.11 255.255.255.0 vmk0
Any idea what the cause could be?
Thanks
Instead of using Object By Name (OBN), did you already try with
$esx = Get-VMHost -Name $hostip
$switch = Get-VirtualSwitch -VMHost $esx -Name $dvs
$portgroup = Get-VirtualPortGroup -VirtualSwitch $switch -Name $pg
New-VMHostNetworkAdapter -Verbose -VMHost $esx -VirtualSwitch $switch -PortGroup $portgroup -IP $vmkip -SubnetMask $SubnetMask -Mtu $MTu
.And leave out the empty $options
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc,
brilliant, that works (partially at least). I had tried changing individual parts to objects but not all of them.
Now the vmks without options work, but how do i add the traffic type?
I split up the script to only add the $options when needed, but even then its not working
$esx = Get-VMHost -Name $hostip
$switch = Get-VDSwitch -VMHost $esx -Name $dvs
$portgroup = Get-VDPortgroup -VDSwitch $switch -Name $pg
if ($pgo -eq $vmk)
{
#$options=""
#-Verbose
New-VMHostNetworkAdapter -VMHost $esx -VirtualSwitch $switch -PortGroup $portgroup -IP $vmkip -SubnetMask $SubnetMask -Mtu $MTu
}
else
{
#-VMotionEnabled:$true
$options= -join("-", "$pgo", ':$true');
#-Verbose
New-VMHostNetworkAdapter -VMHost $esx -VirtualSwitch $switch -PortGroup $portgroup -IP $vmkip -SubnetMask $SubnetMask $options -Mtu $MTu
}
pg=DS_vmk0_NFS_vlan10 vmk=vmk0 vlan=10 extraoptions=vmk0
New-VMHostNetworkAdapter -VMHost 10.0.1.11 -VirtualSwitch DSwitch -PortGroup DS_vmk0_NFS_vlan10 -IP 10.0.10.11 -SubnetMask 255.255.255.0 -Mtu 9000
VMotionEnabled : False
FaultToleranceLoggingEnabled : False
ManagementTrafficEnabled : False
IPv6 :
AutomaticIPv6 :
IPv6ThroughDhcp :
IPv6Enabled :
Mtu : 9000
VsanTrafficEnabled : False
ProvisioningEnabled : False
VSphereReplicationEnabled : False
VSphereReplicationNFCEnabled : False
VSphereBackupNFCEnabled : False
PortGroupName : DS_vmk0_NFS_vlan10
Id : key-vim.host.VirtualNic-vmk0
VMHostId : HostSystem-host-5003
VMHost : 10.0.1.11
VMHostUid : /VIServer=xxx:443/VMHost=HostSystem-host-5003/
DeviceName : vmk0
Mac : 00:50:56:62:9c:46
DhcpEnabled : False
IP : 10.0.10.11
SubnetMask : 255.255.255.0
Uid : /VIServer=xxx:443/VMHost=HostSystem-host-5003/HostVMKernelVirtualNic=key-vim.host.VirtualNic-vmk0/
Name : vmk0
ExtensionData : VMware.Vim.HostVirtualNic
pg=DS_vmk1_vMotion1_vlan6 vmk=vmk1 vlan=6 extraoptions=VMotionEnabled
New-VMHostNetworkAdapter -VMHost 10.0.1.11 -VirtualSwitch DSwitch -PortGroup DS_vmk1_vMotion1_vlan6 -IP 10.0.6.11 -SubnetMask 255.255.255.0 -VMotionEnabled:$true -Mtu 9000
New-VMHostNetworkAdapter : 16.04.2022 14:10:04 New-VMHostNetworkAdapter The MAC address -VMotionEnabled:$true is invalid.
In Zeile:136 Zeichen:5
+ ... New-VMHostNetworkAdapter -VMHost $esx -VirtualSwitch $swi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-VMHostNetworkAdapter], InvalidArgument
+ FullyQualifiedErrorId : Common_SharedParameterHelper_TryValidateStaticMacAddress_InvalidMacAddress,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.NewVMHostNetworkAdapter
You might want to give it a try with splatting.
$esx = Get-VMHost -Name $hostip
$switch = Get-VirtualSwitch -VMHost $esx -Name $dvs
$portgroup = Get-VirtualPortGroup -VirtualSwitch $switch -Name $pg
$sVmk = @{
Verbose = $true
VMHost = $esx
VirtualSwitch = $switch
PortGroup = $portgroup
IP = $vmkip
SubnetMask = $SubnetMask
Mtu = $MTu
}
if ($pgo -eq $vmk){
$sVmk.Add('vMotionEnabled',$true)
}
New-VMHostNetworkAdapter @sVmk
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
If I need to add the api call anyway it makes more sense to use that for all of them ... so I switched to
New-VMHostNetworkAdapter -VMHost $esx -VirtualSwitch $switch -PortGroup $portgroup -IP $vmkip -SubnetMask $SubnetMask -Mtu $MTu
if ($pgo -ne $vmk) #check options value - if options = vmk name its "off", else the value has the traffic type
{
Write-Host Adding $pgo to $vmk
Get-VMHost -Name $hostip -PipelineVariable esxh |
ForEach-Object -Process
{
$vmkMgr = Get-View -Id $esxh.ExtensionData.ConfigManager.VirtualNicManager
$vmkMgr.SelectVnicForNicType($pgo,$vmk)
}}
which still errors at this point in time:
Adding vmotion to vmk1
Ausnahme beim Aufrufen von "UpdateVirtualNic" mit 2 Argument(en): "A specified parameter was not correct: Vim.Host.VirtualNic.Specification.Ip"
In Zeile:204 Zeichen:4
+ $netMgr.UpdateVirtualNic($vmk, $spec)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : VimException
Edit - error from wrong line - disregard
What do you have in $pgo and $vmk?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I moved the block to
if ($pgo -ne $vmk) #check options value - if options = vmk name its "off", else the value has the traffic type
{
Write-Host Adding $pgo to $vmk
if ($pgo -eq "vsan")
{
Get-VMHost -Name $hostip -PipelineVariable esxpv |
ForEach-Object -Process
{
$vmkMgr = Get-View -Id $esxpv.ExtensionData.ConfigManager.VirtualNicManager
$vmkMgr.SelectVnicForNicType("vsan",$vmk)
}
}
elseif ($pgo -eq "faultToleranceLogging")
...
}
}
elseif ($pgo -eq "vmotion")
{
Get-VMHost -Name $hostip| Get-VMHost -PipelineVariable esxpv |
ForEach-Object -Process
{
$vmkMgr = Get-View -Id $esxpv.ExtensionData.ConfigManager.VirtualNicManager
$vmkMgr.SelectVnicForNicType("vmotion",$vmk)
}
}
}
but that made things worse, now i get
ForEach-Object : Fehlendes Argument für den Parameter "Process". Geben Sie einen Parameter vom Typ "System.Management.Automation.ScriptBlock[]" an, und versuchen Sie es erneut.
In Zeile:2 Zeichen:21
+ ForEach-Object -Process
+ ~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [ForEach-Object], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.ForEachObjectCommand
pgo and vmk contain strings for the vmknames/ traffic options
$vmks=@("vmk0"
,"vmk1"
,"vmk2" ...
$pgoptions=@("vmk0"
,"vmotion"
,"vsan"
,"faultToleranceLogging"
,"vmk4"...
$vmk=$vmks[$j]
$pgo=$pgoptions[$j]
The curly-brace needs to be on the same line as the -Process parameter
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ok, fixed that myself .. seem PowerCLI is picky with the brackets...
ForEach-Object -Process
{
fails
ForEach-Object -Process {
works...
Edited - the error presented was from another line ... this seems to work now.
Not sure why the other code now fails though, need to look into it (its code from to set default gw and select the checkbox)
Ok, current snippet is
if ($pgo -ne $vmk) #check options value - if options = vmk name its "off", else the value has the traffic type
{
Write-Host Adding $pgo to $vmk
Get-VMHost -Name $hostip -PipelineVariable esxpv |
ForEach-Object -Process {
$vmkMgr = Get-View -Id $esxpv.ExtensionData.ConfigManager.VirtualNicManager
$vmkMgr.SelectVnicForNicType($pgo,$vmk)
}
}
which works for vmotion, buit not for vsan
pg=DS_vmk1_vMotion1_vlan6 vmk=vmk1 vlan=6 extraoptions=vmotion
New-VMHostNetworkAdapter -VMHost 10.0.1.11 -VirtualSwitch DSwitch -PortGroup DS_vmk1_vMotion1_vlan6 -IP 10.0.6.11 -SubnetMask 255.255.255.0 -Mtu 9000
VMotionEnabled : False
FaultToleranceLoggingEnabled : False
ManagementTrafficEnabled : False
IPv6 :
AutomaticIPv6 :
IPv6ThroughDhcp :
IPv6Enabled :
Mtu : 9000
VsanTrafficEnabled : False
ProvisioningEnabled : False
VSphereReplicationEnabled : False
VSphereReplicationNFCEnabled : False
VSphereBackupNFCEnabled : False
PortGroupName : DS_vmk1_vMotion1_vlan6
Id : key-vim.host.VirtualNic-vmk1
VMHostId : HostSystem-host-5003
VMHost : 10.0.1.11
VMHostUid : /VMHost=HostSystem-host-5003/
DeviceName : vmk1
Mac : 00:50:56:68:07:75
DhcpEnabled : False
IP : 10.0.6.11
SubnetMask : 255.255.255.0
Uid : /VMHost=HostSystem-host-5003/HostVMKernelVirtualNic=key-vim.host.VirtualNic-vmk1/
Name : vmk1
ExtensionData : VMware.Vim.HostVirtualNic
Adding vmotion to vmk1
pg=DS_vmk2_vSan1_vlan7 vmk=vmk2 vlan=7 extraoptions=vsan
New-VMHostNetworkAdapter -VMHost 10.0.1.11 -VirtualSwitch DSwitch -PortGroup DS_vmk2_vSan1_vlan7 -IP 10.0.7.11 -SubnetMask 255.255.255.0 -Mtu 9000
VMotionEnabled : False
FaultToleranceLoggingEnabled : False
ManagementTrafficEnabled : False
IPv6 :
AutomaticIPv6 :
IPv6ThroughDhcp :
IPv6Enabled :
Mtu : 9000
VsanTrafficEnabled : False
ProvisioningEnabled : False
VSphereReplicationEnabled : False
VSphereReplicationNFCEnabled : False
VSphereBackupNFCEnabled : False
PortGroupName : DS_vmk2_vSan1_vlan7
Id : key-vim.host.VirtualNic-vmk2
VMHostId : HostSystem-host-5003
VMHost : 10.0.1.11
VMHostUid : /VMHost=HostSystem-host-5003/
DeviceName : vmk2
Mac : 00:50:56:62:87:11
DhcpEnabled : False
IP : 10.0.7.11
SubnetMask : 255.255.255.0
Uid : /VMHost=HostSystem-host-5003/HostVMKernelVirtualNic=key-vim.host.VirtualNic-vmk2/
Name : vmk2
ExtensionData : VMware.Vim.HostVirtualNic
Adding vsan to vmk2
ForEach-Object : Ausnahme beim Aufrufen von "SelectVnicForNicType" mit 2 Argument(en): "The operation is not supported on the object."
In Zeile:171 Zeichen:5
+ ForEach-Object -Process {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [ForEach-Object], MethodInvocationException
+ FullyQualifiedErrorId : VimException,Microsoft.PowerShell.Commands.ForEachObjectCommand
but vsan and vmotion should both be valid values according to the Enum HostVirtualNicManagerNicType you linked
Can you add the VSAN option for that VMK from the web client?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Actually I can't access any of the newly created vmks...
All stuck in this screen 😞
Are they returned by Get-VMHostNetworkAdapter?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes
Get-VMHostNetworkAdapter -VMHost 10.0.1.11
Name Mac DhcpEnabled IP SubnetMask DeviceName
---- --- ----------- -- ---------- ----------
vmnic0 24:4b:fe:b8:df:4b False vmnic0
vmnic1 24:4b:fe:b8:df:4c False vmnic1
vmnic2 94:40:c9:af:a0:9c False vmnic2
vmnic3 94:40:c9:af:a0:9d False vmnic3
vmnic4 00:07:43:4a:8e:b0 False vmnic4
vmnic5 00:07:43:4a:8e:b8 False vmnic5
vmk20 00:50:56:6b:af:81 False 10.0.1.11 255.255.255.0 vmk20
vmk0 00:50:56:66:e3:f0 False 10.0.10.11 255.255.255.0 vmk0
vmk1 00:50:56:68:07:75 False 10.0.6.11 255.255.255.0 vmk1
vmk2 00:50:56:62:87:11 False 10.0.7.11 255.255.255.0 vmk2
Look fine in Gui too
Ran the script again (deleting all vmks & recreating them)-
this fixed the issue with non working vmks in the GUI.
I can set vSan to vmk2 without problems in the Gui
I Frankensteined a working solution...
if ($pgo -eq "vsan") #check options value - if options = vmk name its "off", else the value has the traffic type
{
Get-VMHostNetworkAdapter -VMHost $esx -Name $vmk | Set-VMHostNetworkAdapter -VsanTrafficEnabled:$true -Confirm:$false
}
elseif ($pgo -ne $vmk) #check options value - if options = vmk name its "off", else the value has the traffic type
{
Write-Host Adding $pgo to $vmk
Get-VMHost -Name $hostip -PipelineVariable esxpv |
ForEach-Object -Process {
$vmkMgr = Get-View -Id $esxpv.ExtensionData.ConfigManager.VirtualNicManager
$vmkMgr.SelectVnicForNicType($pgo,$vmk)
}
}
else
{
Write-Host $vmk no special traffic type
}
Still wonder why I cant set vsan via the api call though.
Thanks a lot for your help! 🙂