Deploying Stratusphere UX Network Stations to vSphere with PowerShell

One of the benefits you have when running Liquidware Labs Stratusphere UX is to ability to capture metric data about your virtual network.  This includes Network Latency (NRT) and Network Application Response Time (ART) between device connections. Stratusphere UX gathers this data using a special Virtual Machine called a Network Station. The Network Station is installed on each of your virtual hosts and captures network traffic using a promiscuous port on your virtual network.  This data is then sent to and consolidated on your Stratusphere UX Hub for reporting and analysis.

For VMware vSphere the Stratusphere Network Station is distributed as a standard OVF or as a VMDK that you can import into your vSphere infrastructure. This works great and is easy to do. However, when you have many vSphere hosts it can take some time to deploy an OVF to each individual host. Using PowerShell along with the VMware PowerCLI commands you can do this from a single script making the deployment process much easier. I will go over the various components and techniques to create this deployment script.

Pre-Requisites

There are a few things needed to get started. First deploy One Liquidware Labs Network Station.  This can be either an OVF or a VMDK install. Download information is found on the Liquidware Labs Download Page. Leave the station powered off. It will be used by the script to clone and deploy all of your remaining Network Stations.

Then make sure you have installed VMware’s vSphere PowerCLI package on the desktop where you will run the PowerShell script. This provides all of the vSphere cmdlets to perform vSphere and vCenter commands and tasks. Download and other information can be found on the VMware PowerCLI main page

You are now ready to start. You can use any editor you want to create your powershell script (.ps1 file) station deploy script. I like Notepad++.  For executing your new script you can easily run into from within the Windows PowerShell environment.

shell_1

It may not be necessary in some environments, but at the top of the script add the following to add in the VMware PowerCLI package.

#Standard PowerCLI cmdlets

Add-PSSnapin VMware.VimAutomation.Core

Then set up a variable for your vCenter server name and then use the ‘Connect-VIServer’ command. This will prompt you for your vCenter credentials when you run the script. I normally will use an administrator level credentials.

#Connect to vCenter prompt for credentials

$vcenterserver = ‘your_vCenter_serverl’

Connect-VIServer $vcenterserver

Using a CSV File

The key for this script is the use of a CSV file which will contain the list of all the network stations you wish to create, along with the information needed to create them in vSphere.  Below is a sample CSV file that I have used and will be used in the script examples provided. A comma delimited CSV file works fine. Here you can define the list of each Network Station VM to create along with its needed vSphere information. Line 1 in the CSV file is very important; those columns are what you reference in your powershell script for each compoment. Also note the Clone machine in the file. This is the Station Virtual Machine you deployed from the OVF or VMDK and all of the other stations will be cloned from.  Another important column is Network2. This is the network with the promiscuous port.

shell_2

To read from a CSV file you would use the Import-CSV command. This sets up a PowerShell variable $station which will hold the data coming from the input CSV file. Also remember to use your specific path and CSV file name in the command.

# Import Station List

$station_list = Import-CSV C:UserspeteDocumentsLWLStation_List.csv

Once you have defined the imported station list you then will do a big For Loop to go through each entry and process it. The framework for the For Loop is below.

# Loop Through Station List and Create each VM from clone

foreach ($stat in $station_list){

…………

………..

}

Inside this loop is where you put the necessary vSphere PowerCLI commands to validate your input data and create each new Network Station Virtual Machine. When you are working with the CSV file input data each component would be referenced in the following format (import variable.CSV header name)

Examples:

$station_list.Station      ( this is the Station Name read from the input file )

$station_list.Host           ( this is the vSphere Host name read from the input file )

$station_list.Network2  (this is the 2nd Network to for the Station VM, the promiscuous port network)

vSphere Validation

One of the important things you should do in your PowerShell script is to validate the data that is in the CSV file. That is to make sure that each component actually exists in vSphere/vCenter before you create the new Station VM. Things would you check include if the Clone VM exists as well as if the datastore, the host exists and the resource pool all exist.

At the top of your overall script enter the below Trap Error code. This will enable you to trap errors properly throughout your PowerShell script. It tells Powershell to continue to the next command when an error is found. Then you can process that error properly.

#Trap Errors

Trap{“$_”;Continue;}

$ErrorActionPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue

$WarningPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue

Each of these sections will validate a different component of your input file. I like to set up a variable to control if an error has been found. In these examples that variable is called $Error_SW. Then I check the switch variable to see if an error had occurred. The Write-Host command will print an error message to the screen within your PowerShell session.

#Check Clone VM

$Clone_Check = Get-VM -Name $stat.Clone -ErrorVariable myErrors

if ($myErrors.Count -gt 0) {

#Examine last exception

Write-Host “ERROR with Clone $stat.Clone – $myErrors[0].exception”

$Error_SW = ‘Y’

}

$vm_source = $stat.Clone

#Check Host

$Host_Check = Get-VMHost $stat.Host -ErrorVariable myErrors

if ($myErrors.Count -gt 0) {

#Examine last exception

Write-Host “ERROR with Host Specified $stat.Host – $myErrors[0].exception”

$Error_SW = ‘Y’

}

$vm_host = $stat.Host

#Check DataStore

$Check_Store = Get-Datastore $stat.DataStore -ErrorVariable myErrors

if ($myErrors.Count -gt 0) {

#Examine last exception

Write-Host “ERROR with DataStore Specified $stat.DataStore – $myErrors[0].exception”

$Error_SW = ‘Y’

}

$DataStore = $stat.DataStore

#Check Resource Pool

$Pool_Check = Get-ResourcePool -Name $stat.ResourcePool -ErrorVariable myErrors

if ($myErrors.Count -gt 0) {

#Examine last exception

Write-Host “ERROR with Resource Pool Specified $stat.ResourcePool – $myErrors[0].exception”

$Error_SW = ‘Y’

}

Creating the Network Station VM

The primary PowerCLI command to create a New Virtual Machine and in this case a cloned Network Station is ‘New-VM’. In our example to use this command you will pass the VM name, the vSphere host, the datastore and the resource pool read from the CSV file for the new cloned station.

Before running the commands I did set up some variables from the input CSV file. The others were set up in the validation code discussed previously.

$vm_name =  $stat.Station

#Create Cloned Station VM

New-VM -Name $vm_name -VM $vm_source -VMHost $vm_host -Datastore $Datastore -ResourcePool $Resource_Pool

Since the Network Stations are host specific we don’t want them being VMotioned to other hosts via vSphere DRS. The below command will disable DRS for your newly created Station VM. The –confirm:$false parameter indicates don’t give an interactive confirmation message.

#Disable DRS for new VM

Get-VM -Name $vm_name | Set-VM -DrsAutomationLevel ‘Disabled’ -confirm:$false

The final piece we need to do is set the Virtual Networks for the newly created Network Station. The 1st Network Adapter will be the management network while the 2nd Network Adapter will be the network with the promiscuous port. The below code will check the virtual network exists using the ‘Get-VirtualPortGroup’ command and then if it exists will add it to the new VM using the ‘Set-NetworkAdapter’ command.

Set Network Adapters

$Net_Check1 = Get-VirtualPortGroup -VMHost $vm_host -Name $vm_net1 -ErrorVariable myErrors

if ($myErrors.Count -gt 0) {

#Examine last exception

Write-Host “ERROR with Network Adapter 1 $vm_net1 for $vm_host – $myErrors[0].exception”

}

else

{

Get-NetworkAdapter -VM $vm_name | where {$_.Name -eq “Network adapter 1”} | Set-NetworkAdapter -NetworkName $vm_net1 -Confirm:$false

}

$Net2_Check = Get-VirtualPortGroup -VMHost $vm_host -Name $vm_net2 -ErrorVariable myErrors

if ($myErrors.Count -gt 0)

{

#Examine last exception

Write-Host “ERROR with Network Adapter 2 $vm_net2 for $vm_host – $myErrors[0].exception”

}

else

{

$Network2 = $stat.Network2

Get-NetworkAdapter -VM $vm_name | where {$_.Name -eq “Network adapter 2”} | Set-NetworkAdapter -NetworkName $vm_net2 -Confirm:$false

}

Logging

Through the course of your PowerShell script you may want to log what is occurring. A good practice would be to create a general log file for messages as well as an error log file. Below is sample good defining those files and using the ‘Write-Output’ command to send data to those files.

#Output Files

$log_file = “C:UserspeteDocumentsLWLstation_log.txt”

$err_file = “C:UserspeteDocumentsLWLstation_error.txt”

Get-Date |Out-File $log_file

Get-Date |Out-File $err_file

Write-Output “Liquidware Labs Station Deploy Log File” | Out-File $log_file -Append

Write-Output “Liquidware Labs Station Deploy Error File” | Out-File $err_file -Append

Write-Output ” ” |Out-File $log-file -Append

Write-Output ” ” |Out-File $err-file -Append

Write-Output “VM Name is  $vm_name” | Out-File $log_file -Append

Write-Output “VM Source is  $vm_source” | Out-File $log_file -Append

Write-Output “VM Host is  $vm_host” | Out-File $log_file -Append

Write-Output “DataStore is  $DataStore” | Out-File $log_file -Append

Write-Output “Resource Pool is  $Resource_Pool” | Out-File $log_file -Append

Write-Output “Network 1 is $vm_net1” | Out-File $log_file -Append

Write-Output “Network 2 is $vm_net2″ | Out-File $log_file -Append

Write-Output ” ” |Out-File $log_file -Append

Summary

PowerShell combined with vSphere PowerCLI enables you to work with almost all aspects of the vSphere and vCenter infrastructure. These examples are one method of how take those commands and deploy Stratusphere Network Stations by cloning an initially installed Station Virtual Machine. The input data comes from a simple CSV file holding the stations to deploy and their various vSphere components. Also remember to test each aspect of your script in a testing environment to make sure it is working as expected before moving to production.

Pete Del Rey

Technology Evangelist

Liquidware Labs

http://www.liquidwarelabs.com