ESXi and PowerCLI

jmachado81 used Ask the Experts™
I have created a 2 node vsan cluster with a witness.  The two physical hosts are interconnected with 10gb links and the vsan vmkernel resides on that particular vswitch.  I leave the mgmt on the default vSwitch0 which uses vmk0.

I'm looking for a way to script in powercli/powershell adding a route to the esxi hosts so that it can communicate with the witness.

$HostNameFQDN =

This outputs nothing on my system currently but it seems like i'm really close.
$Host_Gateway = Get-VMHost -Name $HostNameFQDN | Get-VMHostNetworkAdapter | select VMKernelGatway

New-VMHostRoute -VMHost $HostNameFQDN -Destination -Gateway $Host_Gateway -PrefixLength 20

Would somebody be able to assist with getting this to work, correctly?  I've also noticed that the Get-network commands do not work in Powershell ISE but they do in the regular powershell window.

Thanks in advance.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
PberSolutions Architect
Your syntax looks good, but looks like you need to assign it to a object and then perform a Set:

$vmhostroute = New-VMHostRoute -VMHost $HostNameFQDN -Destination -Gateway $Host_Gateway -PrefixLength 20

Set-VMHostRoute -VMHostRoute ($vmhostroute) -Destination 20


How can I pull the existing gateway from the vmk0 VMKernel (mgmt)?  When i run the below the output is blank.

Get-VMHost -Name $HostNameFQDN | Get-VMHostNetworkAdapter | select VMKernelGatway
PberSolutions Architect

Try this:

Get-VMHost -Name $HostNameFQDN | Get-VMHostNetwork | Select Hostname, VMKernelGateway -ExpandProperty VirtualNic | Select VMKernelGateway
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Solutions Architect
So for your original question:
$Host_Gateway = Get-VMHost -Name $HostNameFQDN | Get-VMHostNetwork | Select Hostname, VMKernelGateway -ExpandProperty VirtualNic | Select VMKernelGateway


This displays all vmkernels instead of just vmk0 but I'll push this line up in my script so there is only one vmk at the time of running.  Thanks
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

The above script works, but is "wrong" in it doing too much. A simple
$Host_Gateway = Get-VMHost -Name $HostNameFQDN | Get-VMHostNetwork | Select VMKernelGateway

Open in new window

is sufficient. The main point has been exchanging Get-VMHostNetworkAdapter by Get-VMHostNetwork.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial