Join the PowerShell 10 Year Anniversary Online Event

It seems hard to believe, but this year sees PowerShell having been around for 10 years! From the early beginnings of increasing awareness and adoption, through becoming a fundamental part of Windows and now having been made Open Source, it’s been quite a journey.

To celebrate this, the PowerShell team has arranged a day of online streaming events on their Channel 9 platform: Monday November 14th from 8:00 am to 4:00 pm (PST).

There will be opportunities to hear the team members talk about how the product has evolved, and some of the MVPs talk about community involvement and the new Open Source engagement. More details can be found from the two links below and checking out the  #PowerShell10Year hashtag on Twitter:

Join the PowerShell 10th Anniversary Celebration!

https://channel9.msdn.com/Events/PowerShell-Team/PowerShell-10-Year-Anniversary

 

 

 

 

Using Pester and PowervRO as a Unit Test Framework for vRO

vRealize Orchestrator doesn’t have an in-built Unit Test Framework, however I realised that it might be possible to use a combination of Pester and PowervRO for now to achieve similar results. Let’s take a look at an example using a very simple workflow, Workflow1. Workflow1 has two inputs, a and b, both numbers:

unittest01

Workflow1 has a single scriptable task that takes the inputs a and b, multiples them together and stores the result in c, which is output from the workflow.

unittest02

We can write the following PowerShell based test using the Pester framework and PowervRO to test that the result of running Workflow1 with inputs of a and b, should be the value we supply for c. We make a connection to the vRO Server in question, invoke the workflow and then check the result:

We can supply variables to the test via a JSON file, so its simple to then take this test to other vRO servers, or change the values we are testing:

Now we can invoke the Pester tests and check the results:

unittest03

Obviously this process could be scaled out to many more workflows with different inputs and outputs.

 

Change the MIME Type of a vRO Resource Element

When a file is imported into vRO to be used as a Resource Element, a MIME type is automatically set depending on what has been imported. For instance, in the below example a shell script has been imported, the contents of which will be used as part of some vRO automation – notice the MIME type has been set to application/x-sh.

mimetype01

This doesn’t really cause a problem in itself when using the Resource Element in a workflow, however vRO doesn’t display the content of all MIME types when looking at the file in the Viewer tab and may display the message “cannot display this kind of element” instead – *.sh is one of those :

mimetype03

 

 

It is possible to change the MIME type of an existing Resource Element with the following code:

Drop that into a workflow and you can use it like this – supply inputs for which Resource Element and what MIME type to change it to. In this example we’ll use text/plain:

mimetype02

Upon successful completion of the workflow, the type should have changed:

mimetype04

and now the contents are viewable on the Viewer tab:

mimetype05

Create Blueprints in vRA7 with PowervRA

Update 29/09/2016:

The API documentation for importing a vRA Content Package contains a warning:

At this point, we don’t support any form of rollback strategies. A failed import may potentially leave the system in an inconsistent state. Hence, its highly recommend to run a precheck/dry-run before the import to validate the package. See HTTP POST /api/packages/validate for more details. This will help catch most of the errors upfront.

face-screaming-in-fearmonkey

Consequently, in release 1.3.1 we have added a new function Test-vRAContentPackage and included it by default in Import-vRAContentPackage. This should mitigate any issues with Importing a badly crafted Content Package, but you should of course test this before using in Production……

———————————————————————————————————————

A while back I wrote a post “Create Blueprints in vRA 7 via REST and via vRO” , with some details around automating vRA 7 Blueprint creation. Since that time Craig and I have published PowervRA, but in the initial releases I had some difficulty with providing the same functionality for PowerShell as I had in the previous post with REST and vRO.

However, thanks to some Ninja skills from Craig in our other project PowervRO, I was able to take the work we did over there around importing vRO Packages / Workflows etc via PowerShell and re-use it in PowervRA for a new function Import-vRAContentPackage, which is available in the latest release of PowervRA 1.3.0. Previous releases contained: New, Get, Remove and Export-vRAContentPackage, we just did not have the last piece of the puzzle: Import.

Example:

So here’s an example of how it works. In the previous article I showed how vRA Blueprints are bundled up into Content Packages and then exported to a zip file containing YAML files which each describe the Blueprints. We need to do the same when automating this process with PowerShell and PowervRA, so first of all we need to know the IDs of any Blueprints to add to the Content Package:

Now we create a Content Package containing the Id of the centos Blueprint that we wish to export:

and then export that Content Package to a zip file:

Take a look at the contents of the zip file and you will see that it contains a metadata.yaml file and a yaml file per Blueprint in a folder composite-blueprint:

powervra01

powervra02

Take a look at the contents of the centos.yaml file to see how a Blueprint is described:

Now we can either modify that file and import back into the same system if we want to change the existing Blueprint or we can take it further if we want to create more Blueprints. Let’s say we want to add a second, similar Blueprint. All we need to do is copy the existing centos.yaml file, make changes in it (I’ve just given larger CPU and memory values), then update the metadata.yaml file to reference the extra file. So they would end up like this:

centosb.yaml:

metadata.yaml:

Now create a new zip file containing the updated metadata.yaml file and the two blueprint yaml files:

powervra04

We can then import that zip file into a vRA Tenant. I’m going to use the Tenant that currently contains no Blueprints:

powervra03

Import the content package:

and we have two Blueprints 🙂

powervra05

centosb has those higher resource settings of 2CPUs and 2048MB memory which we changed in the yaml file:

powervra06

Exporting and Importing vRO Packages with PowervRO

One thing that I know colleagues and others are keen to automate with PowerShell and vRO is exporting and importing vRO packages. If you’re not familiar with a vRO package it is typically used to bundle up all of the Workflows / Actions / Configuration Elements / Resource Elements which make up the code for a particular project and use the package to transport the code to another system. So you may for instance wish to export a package and copy it to another vRO server or maybe into a version control system, or you may wish to automate the deployment of vRO itself and include importing the code as a final step.

PowervRO includes two functions to assist with this and potentially make use of for your automation of Packages, Export-vROPackage and Import-vROPackage.

To export a package we only need to supply the name of the vRO package and the file path to export it to:

 

powervro21

To import the package into another vRO system, transfer the file to the other system and run the following:

Before:

powervro22

powervro23

After:

powervro24

powervro25

 

Check the help for both functions for additional parameters covering fine tuning options that you might need as part of the export or import.

Export vRO Workflow Schema Images with PowervRO

Aside from any documentation around your vRO workflows, one of the best ways to quickly get up-to-speed with what it does and visualise how it is put together is to look at the schema. Wouldn’t it be handy if you could easily get hold of an image of the schema for one or multiple workflows? Well with PowervRO and your PowerShell console you can!

The REST API supports this, so we have included a function Export-vROWorkflowSchema . In this first example, we will export the Schema of a single workflow Test06.

We can then view the file on our desktop:

powervro19

Or if we want to export multiple vRO Workflow Schemata, say for all Workflows in a particular Category:

Happy Days 🙂

powervro20

 

Exporting and Importing vRO Workflows with PowervRO

There are a number of different ways to get your developed vRO content from one system to another: exporting / importing single items, exporting / importing vRO Packages containing multiple items, synchronising content directly between vRO systems.

In this example I’ll show you how to use PowervRO to export and import workflows from and to vRO.

Export vRO Workflows

To export a single workflow is pretty straightforward with the function Export-vROWorkflow:

However, without much extra effort we can leverage some standard PowerShell functionality and export a whole folder (category) of vRO workflows:

PowervRO14

PowervRO15

PowervRO16

 

Import vRO Workflows

Say we are now working on a new vRO system with the same folder structure, we can import one of the exported workflows with the following (note we need to find the ID of the category that we are importing into first):

PowervRO17

Or with a bit of extra work, we can import all of the exported workflows:

PowervRO18

 

Obtaining vRO Workflow State and Result with PowervRO

In the previous episode we looked at how to invoke a vRO workflow with PowerShell, via PowervRO and the Invoke-vROWorkflow function. Once you have kicked the workflow off you are likely to then want to find out the state of the workflow, when it has finished and any output from the workflow. Here’s how to do this via PowervRO.

Check the Workflow State:

First of all, we need to identify which execution of a particular workflow we want to check the state of. In GUI terms this means “which one of these?”:

PowervRO11

Typically, we will want to look at the active one, i.e. the most recent. We can find that as follows:

You can see there is a State property which gives a basic indication of what’s going on. We can find out more details with Get-vROWorkflowExecutionState:

Get the Workflow Result

If the workflow has Output parameters set we can retrieve the results with Get-vROWorkflowExecutionResult. In this example the workflow will generate a random number and include it as an output:

PowervRO12

PowervRO13b

We simply pipe the output of Get-vROWorkflowExecution into Get-vROWorkflowResult 🙂

Automate vRealize Orchestrator with PowerShell: Introducing PowervRO

For the PowerCLI book 2nd Edition I helped put together a chapter on vRealize Orchestrator. Most of the chapter was focused on running PowerShell scripts from vRO, which was something I’d had a fair bit of experience with in projects I had been on and also thought would be what most people reading would be interested in. At the end of the chapter I added a few functions using the vRO REST API to run things in vRO from PowerShell as a bit of an after-thought.

I was quite surprised when during a review phase my co-author Brian Graf suggested swapping the content of the chapter around because he thought there might be more interest in driving vRO with PowerShell, rather than having vRO execute PowerShell scripts. That didn’t quite happen due to time constraints on the book, but I kept that thought in mind having been sparked by the interest.

While putting together PowervRA I had some thoughts about expanding on what I had done in the book around PowerShell and vRO, improving what I had done for the book based on some feedback and incorporating everything we had learnt while putting that toolkit together. Thankfully Craig was up for another project and so PowervRO was born!

Over the course of the last two months or so we have put a toolkit of PowerShell functions together covering a significant portion of the vRO REST API and thankfully it has been a lot more pleasurable than what we have experienced with PowervRA  😉

Initial Release

For the initial release we have 59 functions available covering a sizeable chunk of the vRO REST API. Compatibility is currently as follows:

vRO: versions 6.x and 7.0.1

PowerShell: version 4  and above is required.

You can get it from Github  or the PowerShell Gallery

We have provided an install script on Github if you are using PowerShell v4. If you have v5 you can get it from the PowerShell Gallery with:

Getting Started

Get yourself a copy of the module via one of the above methods or simply downloading the zip, unblocking the file and unzipping,  then copying it to somewhere in your$env:PSModulePath.

PowervRO01

PowervRO02

Import the module first:

You can observe the functions contained in the module with the following command:

Before running any of the functions to do anything within vRO, you will first of all need to make a connection to a vRO server. If you are using self signed certificates, ensure that you use the IgnoreCertRequirements parameter. :

You’ll receive a response, which most importantly contains an encoded password. This response is stored automatically in a Global variable: $vROConnection. Values in this variable will be reused when using functions in the module, which basically means you don’t need to get a new encoded password or server URL each time, nor have to specify it with a function – it’s done for you.

Each of the functions has help built-in, alternatively you can visit this site http://powervro.readthedocs.org

Example Use Case: Invoke a vRO Workflow with a Single Parameter

Having made a connection to vRO, it’s now time to start using some of the functions from the PowervRO module. To invoke a vRO workflow we need to determine the Id of the workflow and whether the workflow requires any parameters to be sent in order to run it correctly. Let’s look at an example of a workflow Test02.

The Id is 5af6c1fd-3d12-4418-8542-0afad165cc08

PowervRO03

The workflow has a single parameter, a, which is a String:

PowervRO04

The schema of the workflow is very simple, there is a single Scriptable Task which logs what the input parameter a was:

PowervRO05

PowervRO06

We can get the ID of a vRO workflow with Get-vROWorkflow:

and we could use that ID directly with Invoke-vROWorkflow. However, we made this easier by adding Pipeline support to Invoke-vROWorkflow, so all you need to do is this:

And here’s the result:

PowervRO07

Example Use Case: Invoke a vRO Workflow with Multiple Parameters

So that was fine for a single parameter, but what to do if the workflow has multiple parameters? The parameter set for Invoke-vROWorkflow that we used in the previous example only supports a single parameter.

Let’s look at another example. The workflow Test03 has two inputs, a and b of different types, String and Number:

PowervRO08

Again it has a single Schema element, which does the following:

PowervRO09

Step forward New-vROParameterDefinition. We can use a combination of this function from PowervRO and Invoke-vROWorkflow and the Parameters parameter to submit the request to run the vRO workflow supplying multiple parameters.

To do this, create an array of parameters using New-vROParameterDefinition and supply them to Invoke-vROWorkflow:

and here’s the result of the workflow execution:

PowervRO10

Stay tuned for more examples on using PowervRO, you can also follow @PowervROModule for updates 🙂

Importing a vRO Package via the API – tagImportMode

I like the new Swagger UI for the vRO API, it makes it really easy to use:

tagImportMode01

While using it to figure out some stuff around importing a package, I hit an issue with the tagImportMode parameter:

tagImportMode02

Depending on which option selected, the following additions to the URL were listed in the documentation as follows:

https://vroserver.fqdn:8281/vco/api/packages?overwrite=false&importConfigurationAttributeValues=true&tagImportMode=Do%20not%20import%20tags.

https://vroserver.fqdn:8281/vco/api/packages?overwrite=false&importConfigurationAttributeValues=true&tagImportMode=Import%20tags%20and%20overwrite%20existing%20values.

https://vroserver.fqdn:8281/vco/api/packages?overwrite=false&importConfigurationAttributeValues=true&tagImportMode=Import%20tags%20but%20preserve%20existing%20values.

However, none of these choices seemed to work, just resulted in 400 (Bad request). Some trial and error followed with different possible combinations, but eventually I found two of them documented in the Developing a Web Services Client for VMware vCenter Orchestrator guide for vCO 5.5.1 and so was able to guess the third.

&tagImportMode=DoNotImport

&tagImportMode=ImportAndOverwriteExistingValue

&tagImportMode=ImportButPreserveExistingValue