Virtual Wood Blog

VMware, vSphere, ESX, ESXi, PowerCLi and everything else

Virtual Wood Blog - VMware, vSphere, ESX, ESXi, PowerCLi and everything else

Configure a LAB with simple PowerCLi commands Part 2

This blog will carry on from part 1 adding more simple functionality to the LAB being created.

The LAB has not changed since part 1 so I will not list the contents again. Please refer back to part 1 for this information.

The following will be added to the LAB in this blog:

  • NTP servers
  • Configure the 1st vSwitch by adding another vmnic
  • Create a 2nd vSwitch with vmnic added
  • Move the ‘VM Network’ portgroup from vSwitch0 to 1
  • Setup a vMotion portgroup using a csv file

To start with we will configure a couple of NTP servers (No need to change the firewall on ESXi).

I will assume that a connection to the VC through PowerCLi has been established.

Please follow the simple instructions below:

  1. Adding NTP servers to the LAB and then restarting the service:
#
#adds two ntp servers and then restarts the ntpd service
#
Add-VmHostNtpServer -NtpServer "ntp1.sandvika.net" -VMHost (Get-VMHost) -Confirm:$false
Add-VmHostNtpServer -NtpServer "ntp2d.mcc.ac.uk" -VMHost (Get-VMHost) -Confirm:$false
$ntpd = Get-VMHostService -VMHost (Get-VMHost) | where {$_.Key -eq 'ntpd'}
Restart-VMHostService $ntpd -Confirm:$false
  1. At present the two hosts that are connected in the LAB only have 1 vmnic (0) attached to vSwitch0. This means that the management vSwitch has no resilience which is of course bad. I will now show the code for adding a 2nd vmnic to this vSwitch. The first example will add the 2nd vmnic to one host and the next code will configure all the host’s the same.
#
#configures a single host vSwitch0 to have two vmnic for resilience
#
$vswitch = Get-VirtualSwitch -VMHost labnode1.lab.local -Name vSwitch0
Set-VirtualSwitch -VirtualSwitch $vswitch -Nic vmnic0, vmnic2 -Confirm:$false

Note: In the above code a single host is being interrogated and only the vSwitch called vSwitch0 is being checked. It is better to specify using –Name

#
#configures all hosts' vSwithc0 to have two vmnic for resilience
#
$hosts = Get-VMHost
$name = $hosts
Foreach ($name in $hosts)
{
$vswitch = Get-VirtualSwitch -VMHost $name -Name vSwitch0
Set-VirtualSwitch -VirtualSwitch $vswitch -Nic vmnic0, vmnic2 -Confirm:$false
}

Note In the above code I have made the code work for us. It is now reading in all the host and then passing this on to an ‘Foreach’ statement so changing all the host’s in a few lines.

  1. We will now configure a 2nd vSwitch with 4 vmnic. This will then be used for the ‘VM Network’. This will of course need to be moved from vSwitch0. The following scripts will add a new vSwitch (1) and then the following script will remove the ‘VM Network’ portgroup from vSwitch0 and create a new one on vSwitch1.
#
#creates a new switch called vSwitch1 and then adds 4 vmnic for all host's
#
$hosts = Get-VMHost
$name = $hosts
Foreach ($name in $hosts)
{
New-VirtualSwitch -VMHost $name -Name vSwitch1
$vswitch = Get-VirtualSwitch -VMHost $name -Name vSwitch1
Set-VirtualSwitch -VirtualSwitch $vswitch -Nic vmnic1, vmnic3, vmnic4, vmnic5 -Confirm:$false
}
  1. We will now remove and then create the ‘VM Network’ portgroup from vSwitch0 to vSwitch1. This will leave the management network free of virtual machine traffic.
#
#removes the 'VM Network' portgroup then creates a new one on a different switch
#
$hosts = Get-VMHost
$name = $hosts
Foreach ($name in $hosts)
{
$vswitch = Get-VirtualSwitch -VMHost $name -Name vSwitch1
$pg = Get-VirtualPortgroup -VMHost $name -Name 'VM Network'
Remove-VirtualPortgroup -VirtualPortgroup $pg -Confirm:$false
$nvmpg =  New-VirtualPortGroup -VirtualSwitch $vswitch -Name 'VM Network'
}
  1. Next we will setup a vMotion portgroup on vSwitch0 for all hosts using a csv file. Thanks has to be given to the following post which helped me get the idea for using the csv file the end product is very different. The csv file needs to be populated with the relevant information for the LAB that is being worked on. This includes the location of the csv file that will be used.
#
#for all hosts this script reads a csv file for vMotion information. This information is then used to add a vMotion portgroup to vSwitch0 adding ip, subnet and then enabling. Once that is complete the gateway is also added
#The location of the .CSV file. This can be local or on a share
#
$getinfo = Import-Csv <a href="file://\\server\share\information.csv">\\server\share\information.csv</a>
$getinfo | % {
$Type = $_.Type #!!!! Case Sensitive  !!!!!!
$gethost = Get-VMHost -Name $_.HostName
$name = $gethosts
$PortGroup = $_.PortGroupName
$IP = $_.IP
$Subnet = $_.Subnet
$kernelGW = $_.KernelGW

#Creates vMotion switch and configures vmkernel gateway (located under DNS and Routing in configuration tab)
Foreach ($name in $gethost) {
IF ($Type -eq "vMotion") {

$vswitch = Get-VirtualSwitch -VMHost $name -Name vSwitch0
$vmotion = New-VirtualPortGroup -VirtualSwitch $vswitch -Name $PortGroup
New-VMHostNetworkAdapter -VMHost $name -PortGroup $PortGroup -VirtualSwitch $vswitch -IP $IP -SubnetMask $subnet -VMotionEnabled: $true

$vmhostnetwork = get-vmhostnetwork $gethost
set-vmhostnetwork -network $vmhostnetwork -vmkernelgateway $kernelGW
}
}
}

This is all for now and I hope it has helped understand in simple examples how powerful PowerCLi is to anyone who needs to manage many hosts. I will cover in the next blog how to add storage amongst other things.

By Paul Wood