Introduction

Last week figured out how to script vSphere related stuff with Python.
This week it is time for something new... how can we "script" vSphere stuff using PowerShell and how can we script NSX related stuff in particular.

So this is a list of steps (if you are really getting started) that you need to take to get started!

Install Windows

Install vSphere PowerShell

Install the DEVELOPMENT VERSION of NSX PowerShell add-on (manually)

Connect to vCenter Server (trough PowerShell)

Connect to NSX Manager (trough PowerShell)

View available NSX PowerShell commands

Verify if connection is working correctly with some “Get Commands”

Install Windows

In order to work with PowerShell this one is pretty obvious I guess ... but as this is like a "beginners" guide I have to mention it.
Once you installed Windows (I tested it on Windows 2012 R2) the you will have access to the PowerShell command Line.

Install vSphere PowerShell

Before I stared I tried to retrieve some information that was already available on the internet an I found this useful blog post.

Now before you can "use" vSphere PowerShell you need to install it first.

The latest version can be downloaded here.
The installation is fairly simple its just click Next, Next and Next and accept the license agreement that nobody ever reads and you have installed vSphere PowerShell.
From now on you can do "things" with your current vSphere environment.
For now we are not going to go deeper into this because our goal is to do "things" with our NSX environment.

Install the DEVELOPMENT VERSION of NSX PowerShell add-on (manually)

You probably noticed the title said "Install the DEVELOPMENT VERSION of NSX PowerShell add-on".
This is because there is also a RELEASED version available here.

The reason why I specifically want the DEVELOPMENT version is because this DEVELOPMENT version supports more commands so you can do more then the RELEASED version.
The DEVELOPMENT version can be downloaded here.

Some useful links in the process of installing NSX PowerShell where this and this link.

Because I was too fast and dod not READ properly I initially installed the “automatic" NSX PowerShell version which installs the RELEASE version.
And I needed to have the DEVELOPMENT version that has more capabilities.

So I download the powernsx.psm1 file (development one) here.
I actually just opened the RAW version and copy/pasted the tekst into Notepad++ and saved it as “powernsx.psm1” and saved it in the C:\ root.
Because I still have not figured out how this GitHub stuff works...
And I needed to uninstall the RELEASED version and install the DEVELOPMENT version.

When you IMPORT the module make sure you are "sitting" in the same directory as where you are installing it from.
My powernsx.psm1 file / module was just in C:\ (the root).

Connect to vCenter Server (trough PowerShell)

First we need to connect to the vCenter Server and when we do this the script asks to put in a username / password with enough rights to perform the needed actions.
We use the vCenter Server admin credentials for this.

PowerCLI C:\> Connect-VIServer
cmdlet Connect-VIServer at command pipeline position 1
Supply values for the following parameters:
Server[0]: dc1-pod11-vcsa-01
Server[1]:
WARNING: There were one or more problems with the server certificate for the
server dc1-pod11-vcsa-01:443:
* The X509 chain could not be built up to the root certificate.
* The certificate's CN name does not match the passed value.
Certificate: [Subject]
C=US, CN=dc1-pod11-vcsa-01.pod11.local
[Issuer]
OU=VMware, O=dc1-pod11-vcsa-01.pod11.local, S=California, C=US, DC=local,
DC=pod11, CN=CA
[Serial Number]
00C2E3BF920EF358D1
[Not Before]
6/16/2016 9:06:16 AM
[Not After]
6/11/2026 9:06:15 AM
[Thumbprint]
B6674ACB4EA7DE528B410977193EF5153F9B2A72
The server certificate is not valid.
WARNING: THE DEFAULT BEHAVIOR UPON INVALID SERVER CERTIFICATE WILL CHANGE IN A
FUTURE RELEASE. To ensure scripts are not affected by the change, use
Set-PowerCLIConfiguration to set a value for the InvalidCertificateAction
option.
Name Port User
---- ---- ----
dc1-pod11-vcsa-01 443 POD11.LOCAL\Administrator
PowerCLI C:\>

Connect to NSX Manager (trough PowerShell)

Then we need to connect to the NSX Manager and when we do this the script asks to put in a username / password with enough rights to perform the needed actions.
We use the NSX Manager admin credentials for this.

I am getting some errors in the above output and this is because I was connecting to an NSX Manager version 6.2.3 and apparently this is not a fully supported version... but hey it eventually worked in the end (even with these errors).

View available NSX PowerShell commands

Now we have connected to both the vCenter Server and the NSX Manager we can start looking at the list of "available" commands that we can use with PowerNSX.