Customisation: Lesson 3 – Networking

Lessons so far in this blog post series:

Lets get down and dirty, by now you should not only starting to become familiar with the way things are working but also probably trying new things out, Great !

Now, as there are multiple ways of configuring your vSwitches/Portgroups and Nics I will explain a few ways that should show you the basics, everything else you should be able to work out from there.  As always any questions or comments are welcome !

So, the first thing we need to do is think about how we would configure this without scripts, often when I am writing a script I will write down the procedure a step at a time following it through in the GUI, I will then convert this into a script.


So to setup our networks if we have a good networks team or have patched the servers ourselves we would have consistent nic patching, lets keep it easy for this example but you can obviously go wild !

So for this example I will choose the following configuration:

nic0 –> Service Console

nic1 –> VMotion

nic2 –> VLAN Trunked Network #1

nic3 –> VLAN Trunked Network #1

nic4 –> VLAN Trunked Network #2

nic5 –> VLAN Trunked Network #2


I know this doesn’t keep to best practice etc etc but lets keep it easy for the examples.

So first thing we would do during the build is add the IP address to vSwitch0 and choose our nic to use with the service console so that’s sorted.

Second I normally setup the VMotion (or vMotion depending on which VMware doc you read) network, this we can do by the following:

New-VirtualSwitch -VMHost $VMHost -Name vSwitch1 -Nic nic1

I don’t think I have to explain that line to you !

Now we need to set the VMotion IP address and subnet mask and tick the box that says we are going to use this portgroup which i have called ‘VMotion’ for VMotion, here’s the few lines of code:

$VMotionIP =
$VMotionSubnet =
New-VMHostNetworkAdapter -PortGroup VMotion -VirtualSwitch vSwitch1 -IP $VMotionIP -SubnetMask $VMotionSubnet -VMotionEnabled:$true

So the first line stores our VMotion IP address, the second stores our subnet and the third works our magic, it adds both of our settings to the vSwitch and at the end it ticks the box to say we want to use it for VMotion.

Next, we want to setup our VLAN Tagged vSwitches, I’m sure you have already worked out the vSwitch part of things but just in case:

New-VirtualSwitch -VMHost $VMHost -Name vSwitch2 -Nic $VMNetwork1Nics

and for the second VM vSwitch (if you didnt work this one out then quit now!):

New-VirtualSwitch -VMHost $VMHost -Name vSwitch3 -Nic $VMNetwork2Nics

So now we have all our vSwitches setup and attached to our nics, we also have our VMotion setup nicely, go for a coffee and we will cover how to add Portgroups to these vSwitches, I have a very nice trick too, stay tuned for the next exciting episode !



Our Script so far:

Connect-VIServer myviserver

$VMHost =

Remove-VMHostNtpServer -VMHost $VMHost -NtpServer

Add-VMHostNtpServer -VMHost $VMHost -NtpServer

Get-VmHostService -VMHost $VMHost |Where-Object {$_.key-eq ntpd} |Start-VMHostService

Get-VmhostFirewallException -VMHost $VMHost -Name NTP Client |Set-VMHostFirewallException -enabled:$true
Get-VmhostFirewallException -VMHost $VMHost -Name SNMP Server  |Set-VMHostFirewallException -enabled:$true

New-VirtualSwitch -VMHost $VMHost -Name vSwitch1 -Nic nic1
$VMotionIP =
$VMotionSubnet =
New-VMHostNetworkAdapter -PortGroup VMotion -VirtualSwitch vSwitch1 -IP $VMotionIP -SubnetMask $VMotionSubnet -VMotionEnabled:$true

New-VirtualSwitch -VMHost $VMHost -Name vSwitch2 -Nic $VMNetwork1Nics

New-VirtualSwitch -VMHost $VMHost -Name vSwitch3 -Nic $VMNetwork2Nics

4 thoughts on “Customisation: Lesson 3 – Networking”

  1. It is June 2013, 4 years after you wrote these lessons, and these lessons have helped me tremendously recently. Thanks, much appreciated!

  2. Hi Al – These lessons you have been doing are fantastic. For anyone trying to learn scripting with the toolkit, you really sum it up. In my scenario, we create two vswitches. One for service console and heartbeat(vswitch0), and the other for all vm’s(vswitch1). We simply create a new port group if we are adding another VLAN. I would definitley like to see how to do that etc. It would make our build that much easier! Thank you again Al.

    1. Kenny,

      Thanks for your comments, im glad you are finding these useful, I never know if im writing to myself or not !

      As for the portgroups, I have just the script for you in my next post which will be this week, one of my favourite script blocks so far.


Leave a Reply