Q.15 | PowerNSX on Linux

Am I on Linux or am I on Windows...? Boundaries between operating systems are getting a bit thin nowadays!

Q.15 | PowerNSX on Linux

To get started with PowerNSX, we need to get PowerShell installed on our Linux system. As on my system I'm using Arch Linux, this guide will be mostly based on this distribution. If you are intrested in getting PowerShell on a diferent Linux flavour, please download the relevant package from this repository and process it according to your distro "ways": Miscrosoft PowerShell for Linux Repository

PowerNSX requires PowerShell v3 or above, and requires PowerCLI (Recommend v6 and above (at the time of this post v6.3R1 )) for full functionality, so if you've installed PowerShell and/or PowerCLI on your system before, please make sure to update it to the latest version before proceeding.

To install PowerShell on Arch, issue the following set of commands:

# Remember to run the following commands as a sudoer (not a root!)
# The following commands require 'git' and 'base-devel' packages
# sudo pacman -Syu git base-devel
[vlku@lab.ssh.guru]: ~>$ git clone https://aur.archlinux.org/powershell.git /tmp/powershell
[vlku@lab.ssh.guru]: ~>$ cd /tmp/powershell
[vlku@lab.ssh.guru]: ~>$ makepkg -si

Alternatively, you can use an AUR helper to do everything for you. If you're not using one already, I'd to recommend yaourt or yay:

[vlku@lab.ssh.guru]: ~>$ yay powershell
# or
[vlku@lab.ssh.guru]: ~>$ yaourt powershell

After installing (one way or another) PowerShell, you will need to create a directory for PS Modules. There are two ways you can do that:

# Option 1 - separate 'Modules' folder for every user
[vlku@lab.ssh.guru]: ~>$ mkdir /home/vlku/.local/share/powershell/Modules
# Option 2 - joint 'Modules' directory for all users in the system
[vlku@lab.ssh.guru]: ~>$ sudo mkdir /usr/local/share/powershell/Modules

Having that done, we can start PowerShell and import the PowerCLI Core module:

[vlku@lab.ssh.guru]: ~>$ pwsh
PowerShell v6.0.2
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type ‘help’ to get help.

PS /home/vlku> import-module VMware.PowerCLI 

If you've followed all of the steps above correctly, you should quickly see the following message:

$<5> Welcome to VMware PowerCLI!

Log in to a vCenter Server or ESX host: Connect-VIServer
To find out what commands are available, type: Get-VICommand
To show searchable help for all PowerCLI commands: Get-PowerCLIHelp
Once you’ve connected, display all virtual machines: Get-VM
If you need more help, visit the PowerCLI community: Get-PowerCLICommunity

Copyright (C) VMware, Inc. All rights reserved.

PS /home/vlku>

Quick note here - it is possible you will get a following error when trying to connect to vSphere and/or NSX:

Connect-VIServer : 8/18/18 05:22:51 PM Connect-VIServer Error: Invalid server certificate. Use Set-PowerCLIConfiguration to set the value for the InvalidCertificateAction option to Ignore to ignore the certificate errors for this connection.
Additional Information: Could not establish trust relationship for the SSL/TLS secure channel with authority ‘X.X.X.X’.
At line:1 char:1
+ Connect-VIServer -Server X.X.X.X
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [Connect-VIServer], ViSecurityNegotiationException
+ FullyQualifiedErrorId : Client20_ConnectivityServiceImpl_Reconnect_CertificateError,VMware.VimAutomation.ViCore.Cmdlets.Commands.ConnectVIServer

To avoid this, we can set the PowerCLI configuration to ignore SSL warnings.

PS /home/vlku> Set-PowerCLIConfiguration -InvalidCertificateAction ignore -confirm:$false

Disabling SSL verification in production environment is a very bad idea, I'm mentioning this as most of my readers do not have SSL certificates in their labs. Please do not disable SSL verification at any point in production environment. You have been warned.

Ok - we are now ready to proceed with PowerNSX installation.

First, git clone the PowerNSX repository from GitHub:

[vlku@lab.ssh.guru]: ~>$ git clone https://github.com/vmware/powernsx.git /tmp/powernsx

Next, copy the PowerNSX.psd1 and PowerNSX.psm1 from /tmp/powernsx/modules/ to your Modules directory:

[vlku@lab.ssh.guru]: ~>$ cp /tmp/powernsx/modules/PowerNSX.psm1 /home/vlku/.local/share/powershell/Modules/
[vlku@lab.ssh.guru]: ~>$ cp /tmp/powernsx/modules/PowerNSX.psd1 /home/vlku/.local/share/powershell/Modules/
# or
[vlku@lab.ssh.guru]: ~>$ cp /tmp/powernsx/modules/PowerNSX.psm1 /usr/local/share/powershell/Modules/
[vlku@lab.ssh.guru]: ~>$ cp /tmp/powernsx/modules/PowerNSX.psd1 /usr/local/share/powershell/Modules/

Finally, go back to the PowerShell, move to the Modules directory and import PowerNSX module:

[vlku@lab.ssh.guru]: ~>$ pwsh
PS /home/vlku> cd .local/share/powershell/Modules/
PS /home/vlku/.local/share/powershell/Modules> Import-Module PowerNSX

That's it! You should now be able to connect to VMware NSX by running:

PS /home/vlku/.local/share/powershell/Modules> Connect-NsxServer -vCenterServer vCenterIP.addr

...where vCenterIP.addr is your vCenter IP address or hostname.

Windows PowerShell credential request
vCenter Server SSO Credentials
User: vlku@nsxlab.ssh.guru
Password for user vlku@nsxlab.ssh.guru: ********
 
Using existing PowerCLI connection to vCenterIP.addr
 
(...)
 
PS /home/vlku/.local/share/powershell/Modules>

Please do not forget to let me know what kind of PowerNSX scripts you've created by twitter @wilk_it_wizard or #sshguru

Read all other the posts of the "Quickie" series in the archive

Related Article