PowerCLI and PowerShell Workflows

In PowerShell v3 the Workflow feature was introduced. But until now there haven’t been too many examples available on how to use PowerCLI in PowerShell Workflows. Today I was triggered by a thread from Mark in the PowerCLI VMTN community, to revise some of my Workflow code snippets I had laying around.

Workflow-PowerCLI

And if you didn’t have enough arguments yet to upgrade to PowerCLI v6, which brings MODULES, the Workflow feature will give you another one !

To get a quick introduction on what PowerShell Workflows are about, read the excellent article by Aleksandar, called PowerShell Workflows,  in the PowerShellMagazine. Aleksandar also did a session on the subject at the Nordic Infrastructure Conference in 2015, you can find a recording of his session here.  For some more in depth information consult the TechNet article called Getting Started with Windows PowerShell Workflow.

When trying to use PowerCLI in PowerShell Workflows, there are a couple of things you should be aware off.

  • Inside a Worklfow script not all cmdlets are supported. The Add-PSSnapin cmdlet is one of those. When you are using PowerCLI v6 this no problem, since you will be using the module autoloading feature.
  • Inside a Workflow script not all variables are available. This includes the $global:DefaultVIServer variable, so you will have to pass this information to the Workflow script via one or more parameters.
  • Your current vSphere Server connection is not available inside the Workflow. You can use the SessionId to connect to your current session inside the Workflow script
  • To run commands, that are normally not supported inside a Workflow script, you can use the InlineScript feature.
  • Accessing variables inside an InlineScript block requires that you use the $Using scope

The following simple example shows how you can “get” a number of Virtual Machines in parallel workflows.

When you are not yet migrated to PowerCLI v6, you will have to include the Add-PSSnapin cmdlet, The following shows the same parallel workflow but then for a pre-PowerCLI v6 environment.

These sample scripts are quite simple in what they actually do, but you can use any of the PowerCLI cmdlets in the InlineScript. You can for example create ten new Virtual Machines in parallel with this technique.

If you have some exciting use cases with PowerCLI and PowerShell Workflows feel free to add them to the comments.

Enjoy !

 

9 Comments

    Lewis

    So cool that you can use PowerShell to manage VMware products 🙂

    I found a great script that emails people when they’ve left a snapshot on a VM for more than a day. Pretty useful to have as a scheduled task!

    Fdo

    HI,, I’ve been trying this very basic worklow script with PowerCLI 6.0 and it crashes every time with error below. Built new server loaded Powercli 6.0 and same thing, but it works fine with PowerCLI 5.8. Am I missing something???
    —————-
    Problem signature:
    Problem Event Name: CLR20r3
    Problem Signature 01: powershell.exe
    Problem Signature 02: 6.2.9200.16398
    Problem Signature 03: 50337c74
    Problem Signature 04: VimService60.XmlSerializers
    Problem Signature 05: 6.0.0.265

      LucD

      Strange, I also ran this against PowerCLI 6, and I didn’t get that problem.
      Which PowerShell version are you using ? PS v4 ?
      Is module autoloading active ? If not, then you would need to load the module(s) in the code block.

        fdo

        Thanks for the reply,,,PS v4.0, .Net 4.5 and PowerCLI 6.0 on Windows 2008 R2 – not working for me. I can run a workflow script but when i use the vmware modules it fails after it goes into the inline script. However, if i remove downgrade it to .net 4. 0 and powercli 5.8 i can run the scripts fine… upgrade back to PowerCli 6.0 – it forces to upgrade .net to 4.5 and same errror….. Will keep looking into this issue.

    Andrew Wark

    Great stuff! I just installed pcliV6 and checking the changes.

    do you know if any of the common cmdlets changed? Looking for a change document from v5 to v6, seen one?

      LucD

      The changes are documented in the Release notes.

    jrob

    Glad to see the post on workflows. We starting implementing them last year to save time with simple tasks as our environment is quite large. We started with a workflow that changes the root password on all of our 2000+ hosts. Before workflows it took several hours (3+ hours) per vCenter for our script to finish, after re-writing with workflows we can change the root password for the entire environment just under one hour. We then used the same construct and wrote a workflow that changed the admin password for iDrac, which turnout to be a huge time savings for us as well. There are several other area’s we intend to introduce workflows this year as they have been great (even though they can be challenging at first!).

      LucD

      Jason, thanks for those great examples of using Workflows as a time saver.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

Buy the Book

Top Viewed Content