PowerCLI 11.0 is Out – Easy Update and Features Galore!

PowerCLI fans unite and celebrate as the launch of the latest edition is now public.  If you’re a reader of my blog then there is a good chance you probably found me from PowerShell or PowerCLI  content which also is a hint of much more to come in the next while.

The first thing you will want to do as the new release is out is to make sure you get your install up to date.  If you haven’t already deployed PowerCLI using the modular approach and get past a couple of common gotchas with these blogs.

This is the list of added goodness as shared from the official PowerCLI blog on the update

  • Added a new Security module
  • Added new cmdlets for Host Profiles
  • Added a new cmdlet to interact with NSX-T in VMware Cloud on AWS
  • Support for vSphere 6.7 Update 1
  • Support for NSX-T 2.3
  • Support for Horizon View 7.6
  • Support for vCloud Director 9.5
  • Multiplatform support for the Cloud module
  • Updated the Get-ErrorReport cmdlet
  • Removed the PCloud module
  • Removed the HA module

These are handy as you get rolling with the most recent versions of vSphere and if you are a vCloud Director fan.  The security updates are probably the most prominent with the update to adding more with both native vSphere 6.7 Update 1 and the vSphere Platinum edition.

Running the Update using Update-Module in PowerShell

This example here shows how to easily update using PowerShell Module management which I’m showing from a Mac OSX installation.

You want to check to see which version you have first which is done using the Get-Module -Name VMware.PowerCLI first:

That confirms our current version.  This happens to be running 10.1.1 before the update.  All you have to do to get the latest update is run the Update-Module -Name VMware.PowerCLI and walk through the prompts. You will be asked about whether to trust the repository or not (spoiler alert:  you have to say Yes)

NOTE: If you’re reading this after future updates beyond 11.0 then you will get the latest edition available at that time from the PSGallery.

Here’s a quick 2 minute video to show you the install in action!

Happy Scripting!




Using jq to pretty print JSON output

If you haven’t already discovered jq, you definitely need to take a look.  This nifty little tool is handy for manipulating JSON content at the command line and within scripts.  The first quick thing I think will be helpful is showing how to pipe raw JSON output to jq to pretty print it (aka show it in the nice nested view).

Once you’ve installed jq, you can run the raw command to get the help output:

03-jq-help

Here is some raw JSON output that we get from a basic cURL command:

01-curl-json

It’s not super easy to read when it is all packed on one line, so let’s pipe the output to the jq command and see the same results:

02-curl-json-jq

You can see the nice nested layout of the JSON output there.  This is a small example, so let’s take something a little larger.

UPDATED PRO TIP:  If you add the -s directive to the cURL command to get rid of the download output as per @shmick (https://twitter.com/shmick/status/777506873041756160)

I’ll use the William Lam Github example here for the VMworld fans.  William has posted JSON content for the VMworld session content from the US event at his Github page:

04-vmworld-top-sessions

Let’s click the Raw button on the page to render the real content URL which we will consume:

05-vmworld-sessions-raw

It’s not too readable in the browser, or the command line as you can see when we run the cURL command:

06-curl-raw

All we have to do to fix that up is to pipe the output from our cURL command to jq and we are able to see the pretty printed version of the JSON:

07-curl-pretty-print

There is much, much more to what you can do with the jq tool, but this was something that I thought was a good start.  Make sure to download it at the jq site, and it is already included in some platforms like CoreOS out of the box.




Downloading Files with cURL, Including Text and Binary Objects

Many orchestration and automation processes will need to download content from external or internal sources over protocols like HTTP and FTP. The simple way to do this is to leverage lightweight, commonly supported and available tools. The most common and popular tool that I’ve found for managing scripting of downloads inside configuration management systems and other script management tools is with cURL.

What is cURL?

cURL stands for command Line URL and is a simple, yet powerful, command line utility that gives the ability to download content using a lightweight executable that provides cross-platform support. cURL is community supported and is often a packaged part of some *nix systems already.

You can download revisions of cURL for a varying set of platforms from https://curl.haxx.se/download.html even including AmigaOS if you so desire 🙂

Why use cURL?

The most common comparative tool to cURL is wget. There is a fully featured matrix of options that are available across a number of different tools, but for simplicity, cURL and wget tend to be the goto standards for *nix and Windows systems because of the small footprint and flexibility.

cURL and wget have many similarities including:

  • download via HTTP, HTTPS, and FTP
  • both command line tools with multiple platforms supported
  • support for HTTP POST requests

cURL does provide additional feature support that isn’t available from wget including:

  • many protocols including DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMTP, SMTPS, Telnet and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, HTTP/2, cookies, user+password authentication (Basic, Plain, Digest, CRAM-MD5, NTLM, Negotiate and Kerberos), file transfer resume, proxy tunneling and more. (source: curl.haxx.se)
  • API support with using libcurl across platforms

Let’s take a look at our example code to see how to make use of cURL.

Download Files With cURL

As we discussed, one of the most useful thing to do with curls is to download a file with cURL.  You can download multiple different file types with cURL, so now let’s take a closer look at how to do this, paying special attention to parameters we need to download different file types.

cURL, Download File type HTML or Text

It’s frighteningly simple to download text and non-binary objects with cURL. You simply use this format:

curl <source URL>

This will download the target URL and output to STDOUT, which will be to the console in most cases.

curl-google

If you wanted to output it to a file, you just add -o to the command line with a target file name (note: that is a lower case o):

curl-google-file

Downloading Binary content with cURL

If we had a binary file, we obviously can’t write it to STDOUT on the console, or else we would get garbage output. Let’s use this image as an example:

target-still-real

The raw URL for this file is https://raw.githubusercontent.com/discoposse/memes/master/StillReal.jpg to use for our examples.

Using the same format, we would try to do a curl https://raw.githubusercontent.com/discoposse/memes/master/StillReal.jpg which gives us this rather ugly result:

curl-binary-fail

To download a binary file, we can use the -O parameter which pulls down the content exactly as the source file specified dictates, including the name as such:

curl -O https://raw.githubusercontent.com/discoposse/memes/master/StillReal.jpg

curl-binary-success

It isn’t that the -O was required to succeed, but it meant that it treated the output exactly as the input and forced it to output to a file with the same name as the source on the target filesystem. We can achieve the same result by using the -o parameter option also and specifying a target filename:

curl https://raw.githubusercontent.com/discoposse/memes/master/StillReal.jpg -o StillReal.jpg

binary-specified-name-same

This is handy if you want to change the name of the file on the target filesystem. Let’s imagine that we want to download something and force a different name like anyimagename.jpg for example:

curl https://raw.githubusercontent.com/discoposse/memes/master/StillReal.jpg -o anyimagename.jpg

binary-specified-any-name

You can pass along all sorts of variable goodness into the name for output when you want to do something programmatically, which is all sorts of awesome as you get to using cURL for automated file management and other neat functions.

We will tackle more cURL options again in the future, but hopefully this is a good start!




Virtual Box for Windows – VBoxManage and Interface Names

virtualbox-logoIf you’re trying out Virtual Box on a Windows host, there is a little catch with the virtual network interface names that happens. It’s a platform specific thing because you are running Windows rather than Linux, and it isn’t an issue unless you are following command line scripts and docs from people using the other platform.

For those who haven’t tried out VirtualBox, it’s freely available, and you will find that it is a common platform for the OpenStack lab work that I do, as well as with the vBrownBag sessions. Add on Vagrant and you’re really ready for fun, but that’s another post altogether 🙂

VBoxManage

The VirtualBox product comes with command line tools to be able to control nearly every aspect of your virtualization environment, including guest control, virtual disk management and in the case of what we are talking about here, host-only virtual networking.

The full VBoxManage commands and parameters list is available here: http://www.virtualbox.org/manual/ch08.html#vboxmanage-general

In this case we are focusing on this command:

VBoxManage hostonlyif create

VBoxManage hostonlyif ipconfig networkname –ip n.n.n.n –netmak n.n.n.n

The *Nix Version

If you are running on a Mac platform, or on a Linux system, you can run the commands above to follow along with what many blogs use. This example adds a host-only network range and assigns it 172.16.0.254/24 as the IP.

VBoxManage hostonlyif create

VBoxManage hostonlyif ipconfig vboxnet0 –ip 172.16.0.254 –netmask 255.255.255.0

So from this command we see that we create a new network, and then we assign the IP based on the name vboxnet0 which is the default name. Subsequent networks will be named vboxnet1, vboxnet2 and so on.

My Windows VBoxManage Isn’t Happy

Now if were to follow along with the commands as provided above on Windows, I run into a distinct problem. First let’s run the first command VBoxManage hostonlyif create

vboxmanage-1

It works like a charm right? But if you see the results, you will notice that the output says Interface ‘VirtualBox Host-Only Ethernet Adapter’ was successfully created.

So if I was to blindly move on to the next command VBoxManage hostonly ipconfig vboxnet0 –ip 172.16.0.254 –netmask 255.255.255.0 can you guess what happens?

vboxmanage-02

Sad trombone 🙁

Lot’s of folks I’ve talked to have hit this wall and then thought they had a problem, but the only issue is that the interface names are platform specific. For Windows hosts, the names will be VirtualBox Host-Only Ethernet Adapter followed by VirtualBox Host-Only Ethernet Adapter #2 then VirtualBox Host-Only Ethernet Adapter #3 and so on.

Time to Think on our Feet!

The fix is simple for us, but just requires us to work with any scripts and documentation according to our platform. In the case of our Windows host, here is the command to make this example work:

VBoxManage hostonlyif create

VBoxManage hostonlyif ipconfig “VirtualBox Host-Only Ethernet Adapter” –ip 172.16.0.254 –netmask 255.255.255.0

And the results are much nicer now:

vboxmanage-03

We can confirm from the GUI also, under File | Preferences | Network which shows our adapter list:

vboxmanage-gui

Now you can play along with the shared scripts using the modified command line. Enjoy!