Tag Archives: powershell

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 ūüôā

PowervRA 1.2.2 with Tested Support for vRA 6.2.4

One of the things we did for the 1.2.2 release of PowervRA was to test all of the functions against a vRA 6.2.4 deployment. Now that we have created Pester tests for all of the functions, it is quite straightforward for us to test against different vRA versions.

While we had initially targeted vRA 7+ because of the better API support, we know that currently the majority of installations out there are 6.x.x. So we are happy to confirm that 58 of the functions work fine with 6.2.4, which was a slightly higher figure than I was expecting.

All of the functions which will not work pre v7 have been updated to include an API version check and will exit the function with a message to reflect that if you try to use  them with 6.x.x, e.g.

Get-vRAContentPackage is not supported with vRA API version 6.2

v6Support01

 

Using Pester to Automate the Testing of PowervRA

Learning Pester has been on my list to get done this year and while working on PowervRA I finally had a real project that could make significant use of it. Being able to automate the testing of each PowerShell function means that we can quickly test the impact of any changes to a function. Also, it means that we can test the whole module full of functions against new (and potentially old) versions of vRA.

There is a very useful introduction to Pester on the Hey Scripting Guy site and that is what I used to get started with it.

So after we released the first version of PowervRA I set about creating a test for each function in the module – and here is where I learned my first mistake, although to be fair I knew I was making this mistake during the initial development of PowervRA. With 70+ functions in the module at that time, I needed to write a test for each of them. So after the initial interest in learning how Pester works, I then had the boring task of writing all of the tests.

What (I knew) we should have done, was write a Pester test for each function during (or before) the development of that function. Consequently, it would not seem like such a laborious task to make them. So going forward that’s what we are doing each time we create a new function.

So what does a test look like? Well here is one for Reservation Policies:

You should see that each set of tests is grouped in a Describe section. Each test starts with the It keyword, then typically we do something and check a property of the object returned afterwards. The Should keyword enables us to specify something to check the result against. As you can see Pester has been made so that the tests should be quite nicely readable.

We then follow a pattern of New-xxx, Get-xxx, Set-xxx, Remove-xxx, which all being well leaves us with a clean environment after the tests.

For these tests, we want to check each function against a real life instance of vRA, consequently we need some values. I’m not sure if this is the best way to do it, but for the time being we’ve abstracted the data out of the test files and into a JSON file of variables. This means if we want to run the same tests against a different instance of vRA, we just need to change some of the values in that file. (There is a way to carry out Unit testing in Pester using¬†Mocking¬†which we may visit at some point)

An example of how we can use them is as follows. We can fire the tests against a vRA 7.0 instance and get the following results:

Pester02

By changing some of the variables in the JSON file, we can then fire the same tests against a vRA 7.0.1 instance:

Pester03

and so we can tell with a good degree of confidence that nothing is broken for PowervRA between the two versions. As you can see we can run 81 tests in 60 – 75 seconds, which is pretty cool ūüôā

Craig and I have discussed that we are only really scratching the surface with the tests so far and we could probably take someone onto the project who is solely dedicated to the testing (If you are interested, let me know ūüôā ¬†). For example, for the time being we are only checking one property per New-vRAxxxx thing which gets created, ideally we should really test every property. For now though, what we have got so far is a big step forward and I’m looking forward to learning more about Pester.

If you want to check out what we have done with the tests you can find them here.

Create a vRA Tenant and set Directory and Administrator Configuration with PowervRA

One of the reasons behind creating PowervRA was as a consultant I often have the need to quickly spin up vRA Tenants and / or components within those Tenants to facilitate development or testing work of other things I am automating. PowervRA contains three functions, which when joined together would make a basic vRA Tenant available for use: New-vRATenant, New-vRATenantDirectory and Add-vRAPrincipalToTenantRole.

The following code example demonstrates how to use these in conjunction with each other to make a vRA Tenant (make sure to first of all have generated an API token with Connect-vRAServer with an account that has permission to create a vRA Tenant):

Note that since New-vRATenantDirectory has a lot of parameters, I have taken advantage of the ability to instead provide the necessary JSON text directly to it.

The result is a fresh vRA Tenant with a Directory configured and admin accounts assigned to both the Tenant Admins and Infrastructure Admins roles:

CreateTenantPowervRA02

 

CreateTenantPowervRA01

Find the vRO Workflow ID for an Advanced Service Blueprint with PowervRA

A colleague asked me the other day about how it might be possible to find out which vRO workflow was mapped to an Advanced Service Blueprint (or XaaS Blueprint) in vRA. If you look in the vRA GUI after a Service Blueprint has been created you can’t see which vRO workflow is mapped.

During the creation of the Service Blueprint there is a Workflow tab to select the vRO Workflow:

ServiceBlueprintPowervRA01

 

However, once it has been created, there is no longer a Workflow tab, so you can’t see which vRO workflow is used:
ServiceBlueprintPowervRA02

By using PowervRA though we find this information. The object returned by Get-vRAServiceBlueprint contains a WorkflowId property:

ServiceBlueprintPowervRA03

We can now take that WorkflowId and find the corresponding workflow in vRO. Unless you have memorised all of the workflow IDs then you can issue a REST request to vRO to find out more. The following example uses PowerShell to query the vRO REST API for the WorkflowID above (note that we have to deal with self-signed certificates):

If we look at the data stored in the Workflows variable, we can see the name of the workflow in vRO (OK in this example it’s the same name as the Service Blueprint, but it might well not be in another example):

ServiceBlueprintPowervRA04

Also, if you look at $Workflows.relations.link you will see the first result shows the path in vRO to track down the workflow:ServiceBlueprintPowervRA05

i.e., it can be found in the top-level folder named Test:

 

ServiceBlueprintPowervRA06

Automate vRealize Automation with PowerShell: Introducing PowervRA

While putting together¬†the PowerCLI book 2nd Edition we initially included in the proposed Table of Contents a chapter on vRealize Automation. However, it was fairly apparent that at that time (early 2015) there wasn’t a lot which could be done to fill out the chapter with good content. Firstly, most of the relevant content would be included in the vRO chapter, i.e. use vRA to call a vRO workflow to run PowerShell scripts. Secondly, automating elements within vRA 6.x could be done in part via the REST API, but a) there was a roughly 50 / 50 split between what was in the REST API vs Windows IaaS and b) I didn’t really have the time to make both a PowerShell toolkit for vRA and write a book about PowerCLI.

So we shelved that chapter and I put the thought to the back of my mind that I would revisit the idea when vRA 7 came out and the likelihood of greater coverage in the vRA REST API. At the start  of 2016 this topic came up in a conversation with Xtravirt colleague Craig Gumbley who it turned out had the same idea for making a PowerShell vRA toolkit. So we decided to combine our efforts to produce a PowerShell toolkit for vRA for both our own use as consultants and also to share with the community; consequently the project PowervRA was born.

Initial Release

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

vRA:¬†version 7.0 – some of the functions may work with version 6.2.x, but we haven’t tested them (yet). Also, they have not been tested with 7.0.1.

PowerShell: version 4 is required. ¬†We haven’t tested yet with version 5, although we wouldn’t expect significant issues.

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.

PowervRA01

PowervRA02

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 vRA, you will first of all need to make a connection to the vRA appliance. If you are using self signed certificates, ensure that you use the IgnoreCertRequirements parameter. :

You’ll receive a response, which most importantly contains an authentication token. This response is stored automatically in a Global variable:¬†$vRAConnection. Values in this variable will be reused when using functions in the module, which basically means you don’t need to get a new authentication token 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://powervra.readthedocs.org

Example Use Case: Create a vRA Tenant

Having made a connection to the appliance, it’s now time to start using some of the functions. To create a Tenant in vRA we need to have made a connection to vRA with an account that has permissions to do so in the default tenant (typically [email protected]) and then it is as simple as the following:

PowervRA03

PowervRA04

If you look through the rest of the functions, you may notice that a lot of them contain a JSON parameter. So if you know the JSON required for the REST API request or are working with a system that produces it as an output, you can do something like the following:

The Future

VMware may put out something official at some point (I have no inside info on that, it could be weeks, months, years away or not even planned right now). Until that happens Craig and I have various things planned including greater coverage of the API, dealing with any feedback from this release and looking at automating some of our own testing so that we can more easily figure out which vRA versions are supported.

In the meantime, fill your boots and if you want to help us, feel free to get involved via the Develop branch on GitHub.

 

PowerCLI Book 2nd Edition is Now Available!

…..well in Kindle format anyway ūüôā The paperback version will be available on 11th January in the US and 12th January in the UK¬†and according to the publisher, apparently in book shops from the 19th January. Seriously, if you actually see one in a book shop then please send me a photo, since I’ve not seen that happen outside of a VMworld.

 

9781118925119.pdf

With my fellow authors, Luc Dekens, Glenn Sizemore, Brian Graf, Andrew Sullivan and Matt Boren, we spent the best part of 2015 putting this book together. The 1st edition was written in 2010 and published in 2011; since then the VMware virtualisation landscape has changed significantly from pretty much VMware vSphere infrastructure and P2V projects and maybe some desktop work with VMware View to a wide variety of Management, Desktop, Application, Infrastructure and Cloud products.

Given this expansion of products and the fact the we all buy tech books ourselves and want value for money from them, we didn’t want to just ship an updated version to cover what was in the 1st Edition and make sure it worked with the latest versions of vSphere, PowerCLI and PowerShell. So as well as updating every chapter to make sure it worked with vSphere 6, PowerCLI 6 and PowerShell 4, and adding or replacing content and code to make sure it was still relevant (for instance the Distributed Switch is now covered out of the box, previously we contributed our own functions), we had the foolish excellent idea to add a number of additional chapters covering things such as:

  • vCloud Director
  • vCloud Air
  • vRealize Orchestrator
  • Site Recovery Manager
  • PowerActions
  • and an introduction to DevOps topics

Additionally the original Storage and Network chapter was split into two and content on new technologies VSAN and NSX added appropriately. To be honest each of VSAN and NSX could probably have their own chapters…maybe next time ūüėČ

This took the page count to 984, around almost 200 pages more than the 1st¬†edition, which is almost the size on its own of some tech books. Unfortunately, this partly led to the book taking longer than it should have to complete and there were still areas that we would have liked to include, but had to make some tough decisions not to or it would never have seen the light of day. As a wise man often says….

JeffreySnover2

While there is some introductory content in some of the chapters, this is not a book that typically runs you through how to use each of the PowerCLI cmdlets in their relevant areas, rather this is a book that goes beyond what ships out of the box and into areas where you will need to do some of the hard work yourself.  We had some negative feedback that the 1st edition was not better in introductory areas and may well do so again this time; there are other great PowerCLI books out there now which will do a better job for beginners. So imagine the front cover has Deepdive stamped across it and that will give you a better feel for what to expect.

Personally I feel I made a way better contribution to this book than the previous one for various reasons:

  • I was involved from the beginning, rather than parachuting in halfway through to try and get the book out of the door.
  • Consequently I was able to think about and plan for the content of the chapters I was responsible for, rather than just trying to make the deadline for each chapter.
  • Being used to the publisher’s Word template and what was required in them. There are so many rules about fonts, spacing and expectations around how the chapter needs to be laid out, that you seem to be just expected to know and first time round¬†caused a lot of pain and time wasted.
  • A supportive employer, Xtravirt, who were interested in what I was doing, rather than one who previously was not.
  • A big thank you to our Technical Editor, Matt Boren, who left no stone unturned in making sure my code was not only accurate, but also some outstanding suggestions to help me improve. Some of which I have taken forward for everything PowerShell based that I write now. In fact he liked the experience so much, he even got roped into contributing some of the book content himself.
  • I actually had a reasonable idea about the technologies involved……;-)

 

Last time round I guessed how much time I spent on my contribution. Being curious, I decided to track it this time.¬†It’s also something I get asked about now and again by people considering writing a tech book, so now I can refer them to some data ūüėČ

Originally I was going to track the data by week, but since it went on so long it ended up being by month. I’m not sure if writing a book with code samples requires more or less effort than a tech book without them. On the one hand you almost have to produce double the content; explanatory written text and create code to make stuff happen. On the other hand at least the code uses up some pages when printed in the book ūüôā

BookHours

In total I spent 330.5 hours (that extra 0.5 was crucial) of my own time on the effort for this book between Jan and Nov 2015, and remember there were 5 other authors. So if you are thinking about getting involved in a project like this then consider carefully whether you are able to commit to the level of time you may have to give up for it. I’m not sure how representative this is for authors of other tech books, but I never talked to one who didn’t say something like “you have no idea how much effort it takes”. Similar to last time, I pretty much worked on it most evenings Mon – Thu and stayed away from it over the weekend so that it did not impact my family life too much. (Although my children are now 5 years older and consequently don’t go to bed so early – which meant starting later and finishing too late regularly) It did mean I didn’t see my friends much¬†during weekday evenings, which is when I tend to catch up with them, for about 6 months, but something has to give. I think my blog and podcast content suffered a fair bit too.

You’ll see that most of the original effort was in the early months either producing content for new chapters, or updating content for existing ones. I was actually originally expecting to be pretty much done by April, but for various reasons I took on a bit more than originally planned and April – June was spent producing more content and some early feedback reviews. August – November was pretty much taken up with reviews.

Reviews are something which I think vary from publisher to publisher, but for Wiley it pretty much follows this:

 

Author Review

If you can recognise your original text after it has been through all that process, then well done ūüôā

So at least three responses required per chapter, multiply that by the number of chapters you commit to and then try finding time to write new chapters and respond to reviews both on similar deadlines. Pro tip: get your new content out of the way early on so you can do the reviews in peace – ¬†see Jan / Feb in the chart ūüėČ

With all that effort though, it is a personally (not financially) rewarding experience. I think both times I’ve said “Never again!” too many times, but I suspect it may happen one day. I’m confident that if it’s something you are interested in and you buy a copy that you will find it useful. I genuinely would happily buy a copy with my own money for the content I have seen the other guys produce for it.

Final comment: Writing in a foreign language also brings some difficulties. Having your own English ‘corrected’ to the US version requires a fair amount of patience. Not necessarily spelling differences, but phrases that are commonly understood this side of the Atlantic, but not the other. I’ll just leave this here……

Update 03/02/2016: Due to some issues mentioned in the comments around downloading the sample files, I have attached (most of) them here: