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.


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 !




    Great article Lucd

    I took some performance measurements
    Serial execution (non-parallel)
    Custom function that inventories VM’s, their network adapters,hard disk using a combination of Get-VM,Get-View,Get-HardDisk,Get-NetworkAdapter; a little sloppy but serves the purpose

    Test results
    1. Serial execution 210 VM’s across 5 hosts 388 seconds
    2. PoshRSJob 210 VM’s across 5 hosts 215 seconds
    3. WorkFlows 210 VM’s across 5 hosts 168 seconds

    With PoshRSJob I see no evidence of true multi-threading, it feels sequential observed by using Write-Warning inside threads and capturing every time 5 VM’s are inventoried. I am not sure why this is behaving as such. By adding more to the queue I do not see big payoff in execution times that you would expect with multi-threaded execution

    With workflows I can see parallel execution, and can see huge pay-offs by using a bigger queue.

    Anyhow – thanks for this easy to follow article. If you have had experience with PoshRSJob for similar functionality would like to hear your thoughts.


      Thanks for your input, very interesting.

      I’m currently preparing a post with the Parallel switch in Foreach (PSv7 & PowerCLI 12.2).
      I’ll include PoshRSJob in my tests.


    I am trying to use this workflow example for 7 vms , and i see that the for-each parallel picks up only 5 at one time eveything goes fine until the 6th one is picked up and i get an error :

    Microsoft.PowerShell.Utility\Write-Error : The specified mount name ‘vis’ is already in use.
    At line:455 char:25
    + … Receive-Job -Job $job -Wait -Verbose -Debug -ErrorAction …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Write-Error], RemoteException
    + FullyQualifiedErrorId : System.Management.Automation.RemoteException,Microsoft.PowerShell.Commands.WriteErrorCom
    + PSComputerName : [localhost]

    This mostly comes at the place where the import-module VMware.VimAutomation.Core is being called i even tried without this statement in which case it gives me an error that connect-vi server cmdlet was found but the module could not be loaded

    PowerCLI Version
    VMware PowerCLI 11.0.0 build 10380590

    Any help or ideas around this?


      Unfortunately, the PowerCLI code is not thread-safe.

      The best, and working, alternative, for now, is to use background jobs (with Start-Job)
      Workflows are in any case deprecated afaik.


    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!


    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:


      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.


        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?


      The changes are documented in the Release notes.


    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!).


      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 *


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