PowerCLI and Networking

In this post I’d try to demonstrate the usage of “VMware PowerCLI” in case of working in some network area, for more clear, I’d create new “Virtual Machine Port Group” and then would add new vNIC to VMs and connect them to the newly created “Port Group”.

Step 1 – Creating new “Port Group”

First of all, I’d checked the networking configuration of two standalone hosts under “HostConfigurationNetworking” and as it shown in the bellow screen shots, there is no “Virtual Machine Port Group” under “vSwitch1” and it’s only contain one “VMKernel” for vMotion purpose for each host.

 

After running bellow “PowerCLI” command, one port group would be created on each host with indicated name as shown.

$vmh = Get-VMHost; $vmh | %{ Get-VirtualSwitch -VMHost $_ | where {$_.name -like "vSwitch1"} | New-VirtualPortGroup -Name "Sync Channel"}

  

Now lets check each part of code:

Bellow code would get a list of connected host and insert them in the “$vmh” variable, so this variable would contain names, and other information of connected hosts.

$vmh = Get-VMHost;

Bellow piece of code would repeat internal command which replaced by “dots” for each item in “$vmh” variable. if “$vmh” contain 2 items, internal commands would run twice and if “$vmh” contains 100 items then those commands would be run 100 times, each time for each item.

$vmh | %{ ...... }

Now the first part of internal commands would be “Get-VirtualSwitch -VMHost $” and it will get the vSwitch information for each item passed to it;
For being more clear, command would select 1 item among 100 in “$vmh” and then run the “Get-VirtualSwitch -VMH0st $
” command for it while the host name would be equal to the selected item the pipeline would pass the current information to the next command.

$vmh | %{ Get-VirtualSwitch -VMHost $_ | .... }

The first pipeline would help us to filter some data by passing data from left side to the right side, “Where {$.name -like “vSwitch1″}”; up to now, one host selected and then its virtual switches and now by using “$.name -like ***” only those switches would be selected which have the given pattern or name.
Here, I’ve filter those switches which had “vSwitch1” name and then the result would be pass to the next part (right side) by using the pipeline again. 😉

$vmh | %{ Get-VirtualSwitch -VMHost $_ | where {$_.name -like "vSwitch1"} | .... }
The second command which is important and would do the main job is "New-VirtualPortGroup -Name "Sync Channel""; as it self describe, this command would create new port group with name of "Sync Channel" on the vSwitch1 which passed from previous pipeline (left side) on the selected host from "$vmh".
[powershell]$vmh | %{ Get-VirtualSwitch -VMHost $_ | where {$_.name -like "vSwitch1"} | New-VirtualPortGroup -Name "Sync Channel"}

Step 2 – Add new vNIC to VMs and connecting them

I’m checking the VMs network connection both from both host networking view and from “Virtual Machine Properties” as shown in bellow pictures.

   

As it shown in the above pictures, their two vNIC only connected to default “Virtual Machine Port Group” named “VM Network”; I did same check by using bellow “PowerCLI” commands.
First get all VMs which are started by “NSCE” using “Get-VM NSCE*” and then get their vNICs using bellow line.

Get-VM NSCE* | Get-NetworkAdapter

It’s time to add new vNIC (3rd) to these VMs and it’s possible by just simply replacing the “Get-NetworkAdapter” with “New-NetworkAdapter”; this command needed some parameter in order to correctly and completely work.
When passing the VM name using pipeline (from left to right), it’s possible to run the commend by only providing the “Port Group” which VMs should connect to; bellow I’ve added two more parameters for meeting my VMs requirement.

Get-VM NSCE* | New-NetworkAdapter -NetworkName "Sync Channel" -StartConnected:$true -Type "e1000"

Bellow image showing the VMs network adapter information I get using “PowerCLI”, “Virtual Machine Properties” windows and host networking view.

    

Note:
There might be other ways for doing these tasks by using “VMware PowerCLI” but as I’m beginner this is as far as I got now, if I find other way for these tasks, I’d update this post as well.

It's your kindness to leave a reply/feedback