PowerShell and the Nimble Storage REST API

If you read my previous blog article on “PowerShell Function to Determine the Installed VSS Providers” then you’re already aware that I recently migrated one of my customers to a Nimble Storage Area Network.

While Nimble does have a PowerShell module and it’s decent, I wanted to see how difficult it is to work with their REST API directly with PowerShell. Their REST API documentation also seems to be decent.

Note: All of the functions shown in this blog article are a proof of concept and should be thoroughly tested before attempting to use them in a production environment.

First of all, you need to connect to the Nimble SAN group so I wrote a function for that:

I’m not a big fan of globally scoping variables unless absolutely necessary, but in this scenario unless you want to authenticate with every command, it does seem to be necessary. Their SAN uses a self-signed SSL certificate and I found that fellow Microsoft MVP Joel Bennett had written a PowerShell module named TunableSSLValidator that adds an Insecure parameter to Invoke-RestMethod which kept me from having to worry about writing the code to handle it myself.

Update: Based on feedback from Joel Bennett, I changed the variable scoping from Global to Script to limit their access to only the module.

While many examples you might find store things like the value for Body parameter in a variable, the password is decrypted and then transmitted using the SSL encryption so I didn’t want the plain text password hanging around in a variable for however long I leave my current PowerShell session open so I decided to perform the decryption inline when that line of code is executed.

This next function gets the session tokens for users who are connected to the Nimble SAN group:

I decided to go ahead and turn these functions into a module so it would be easier to add both custom formatting and custom types. One thing I’ve found is most SAN vendors simply give you everything back from the API. That ends up being an overwhelming amount of information so I decided to only return a few key properties in a table by default without having to hard code anything or pipe to format table. That’s where the custom formatting comes into play. This .format.ps1xml file also includes custom formatting for the next function we’ll look at.

You could also add custom formatting so piping to Format-List would only return specific properties unless all properties were explicitly requested either with Format-Table -Property * or Select-Object -Property *. An example of how to write custom formatting for list views can be found in my blog article about “My Solution: August 2015 PowerShell Scripting Games Puzzle“.

I also added a custom types file which adds a human readable date for the session token creation and modified time stamps. The value that the API returns is the number of seconds since January 1, 1970. The .types.ps1xml file simply adds a couple of script properties without having to manually add them to the function itself which seemed to be a cleaner solution.

To me, the really cool function is the one I wrote to retrieve a list of volumes:

Do you see how splatting is used to dynamically build the commands and how it’s used to reduce the amount of redundancy and minimize the amount of code you have to write?

The functions shown in this blog article are part of my MrNS PowerShell module which can be downloaded from my Nimble repository on GitHub.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: