VMware Cloud Community
mark_chuman
Hot Shot
Hot Shot
Jump to solution

WorkFlows with PowerCLI?

Let me guess.., this is expected:

script:

---------------------------------------------------------------------------------

workflow paralleltest {


parallel {

New-VM -Name VirtualMachine003 -VMHost esxserver

New-VM -Name VirtualMachine004 -VMHost esxserver

New-VM -Name VirtualMachine005 -VMHost esxserver

New-VM -Name VirtualMachine006 -VMHost esxserver

New-VM -Name VirtualMachine007 -VMHost esxserver

New-VM -Name VirtualMachine008 -VMHost esxserver

New-VM -Name VirtualMachine009 -VMHost esxserver

New-VM -Name VirtualMachine010 -VMHost esxserver

  }

}

paralleltest


---------------------------------------------------------------------------------

run in powercli and following error:

PowerCLI C:\Scripts\misc\3_17> .\new.ps1

Microsoft.PowerShell.Utility\Write-Error : The term 'New-VM' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if

path is correct and try again.

At paralleltest:12 char:12

+

    + CategoryInfo          : NotSpecified: (:) [Write-Error], RemoteException

    + FullyQualifiedErrorId : System.Management.Automation.RemoteException,Microsoft.PowerShell.Commands.WriteErrorCommand

    + PSComputerName        : [localhost]

Microsoft.PowerShell.Utility\Write-Error : The term 'New-VM' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if

path is correct and try again.

At paralleltest:12 char:12

+

    + CategoryInfo          : NotSpecified: (:) [Write-Error], RemoteException

    + FullyQualifiedErrorId : System.Management.Automation.RemoteException,Microsoft.PowerShell.Commands.WriteErrorCommand

    + PSComputerName        : [localhost]

Microsoft.PowerShell.Utility\Write-Error : The term 'New-VM' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if

path is correct and try again.

At paralleltest:12 char:12

+

    + CategoryInfo          : NotSpecified: (:) [Write-Error], RemoteException

    + FullyQualifiedErrorId : System.Management.Automation.RemoteException,Microsoft.PowerShell.Commands.WriteErrorCommand

    + PSComputerName        : [localhost]

Microsoft.PowerShell.Utility\Write-Error : The term 'New-VM' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if

path is correct and try again.

At paralleltest:12 char:12

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

It is not straightforward, but it is doable, provided you know that you need to access variable with $Using inside an inlinescript.

See the following example:

workflow test {

    param(

        [string]$vcenter,

        [string[]]$names,

        [string]$session

    )

    foreach -parallel($name in $names){

      $vm = InlineScript{

            Add-PSSnapin VMware.VimAutomation.Core

            Connect-VIServer -Server $Using:vcenter -Session $Using:session | Out-Null

            Get-VM -Name $Using:name

      }

      $vm.Name

    }

}

$vmNames = 'vm1','vm2','vm3'

test -names $vmNames -vcenter 'MyvCenter' -session $global:DefaultVIServer.SessionSecret

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
15 Replies
mark_chuman
Hot Shot
Hot Shot
Jump to solution

Just to give everyone some insight into what I'm trying to achieve.  Interested in exploring ways to "multi-thread" our automation capabilities.  For example, I've been using a script to migrate clusters from one vCenter to another (works well), but in order to hit, say 8 clusters at a time, I have to launch 8 different versions of PowerCLI.  For the next version, I'm interested in being able to have the scalability be built directly into the script logic, so we could migrate (in this example) one cluster or as many as the load to VC would allow.  Also, looking to utilize this logic to say allow for us to update say, one host per cluster in each VC across all our VCs, so we could potentially update our entire non-prod pool of ESX servers much, much quicker (ie, 10 or 20 hosts per shot, per VC, so if you have 10 VCs you are looking at ~100-200 host simultaneously).  I know this may sound like science fiction now, but I want to explore the possibilities in trying to make this happen.

0 Kudos
mark_chuman
Hot Shot
Hot Shot
Jump to solution

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Since this starts a new PS engine, you will have to make sure the PowerCLI pssnapin (if you're on pre-v6) is loaded.

With the v6, the module auto-load feature should take care of this


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

mark_chuman
Hot Shot
Hot Shot
Jump to solution

Thanks Luc.  Tried to add it in, but still hitting a problem.

workflow paralleltest {

parallel {

#Check and if not add powersehll snapin

if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {Add-PSSnapin VMware.VimAutomation.Core}

New-VM -Name VirtualMachine003 -VMHost esxserver

New-VM -Name VirtualMachine004 -VMHost esxserver

New-VM -Name VirtualMachine005 -VMHost esxserver

New-VM -Name VirtualMachine006 -VMHost esxserver

New-VM -Name VirtualMachine007 -VMHost esxserver

New-VM -Name VirtualMachine008 -VMHost esxserver

New-VM -Name VirtualMachine009 -VMHost esxserver

New-VM -Name VirtualMachine010 -VMHost esxserver

  }

}

paralleltest

--------------error

PowerCLI C:\Scripts\misc\3_17> .\new.ps1

At C:\Scripts\misc\3_17\new.ps1:7 char:83

+ ... tlyContinue)) {Add-PSSnapin VMware.VimAutomation.Core}

+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Cannot call the 'Add-PSSnapin' command. Other commands from this module have been packaged as workflow activities, but this command was specifically excluded. This is likely because the

Windows PowerShell session, or has behavior not suited for workflows. To run this command anyway, place it within an inline-script (InlineScript { Add-PSSnapin }) where it will be invoke

    + CategoryInfo          : ParserError: (:) [], ParseException

    + FullyQualifiedErrorId : CommandActivityExcluded

paralleltest : The term 'paralleltest' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, v

try again.

At C:\Scripts\misc\3_17\new.ps1:22 char:1

+ paralleltest

+ ~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (paralleltest:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

0 Kudos
LucD
Leadership
Leadership
Jump to solution

0 Kudos
mark_chuman
Hot Shot
Hot Shot
Jump to solution

Thanks, but I'm getting the feeling work-flows are not easily used with powercli.

A simple test such as this fails with an error about not being connected to vCenter.  I'm connected to a vCenter server via the powercli session I'm launching this script from.  I can't imagine that it would be more efficient if I have to input a connect-viserver in the script and a connection is initiated for each line of code?

------------------

workflow paralleltest {

parallel {

InlineScript{

Add-PSSnapin VMware.VimAutomation.Core

Get-VM

  }

}

}

paralleltest

------------------

PowerCLI C:\Scripts\misc\3_17> .\new.ps1

Get-VM : 03/17/2015 1:44:49 PM    Get-VM        You are not currently connected to any servers. Please connect first using a Connect cmdlet.

At paralleltest:6 char:6

+

    + CategoryInfo          : ResourceUnavailable: (:) [Get-VM], ViServerConnectionException

    + FullyQualifiedErrorId : Core_BaseCmdlet_NotConnectedError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

    + PSComputerName        : [localhost]

0 Kudos
mark_chuman
Hot Shot
Hot Shot
Jump to solution

This works, but it runs serially.  Might be getting closer.

workflow paralleltest {

InlineScript{

    Add-PSSnapin vmware.vimautomation.core

  $session="52ee00b8-fc9b-9d30-69f0-398953600885"

  $vcserver="vcentername"

  connect-viserver $vcserver -session $session

   New-VM -Name VirtualMachine003 -VMHost esxname.fmr.com

   New-VM -Name VirtualMachine004 -VMHost esxname.fmr.com

   New-VM -Name VirtualMachine005 -VMHost esxname.fmr.com

   New-VM -Name VirtualMachine006 -VMHost esxname.fmr.com

   New-VM -Name VirtualMachine007 -VMHost esxname.fmr.com

   New-VM -Name VirtualMachine008 -VMHost esxname.fmr.com

   New-VM -Name VirtualMachine009 -VMHost esxname.fmr.com

   New-VM -Name VirtualMachine010 -VMHost esxname.fmr.com

   New-VM -Name VirtualMachine011 -VMHost esxname.fmr.com

}

}

paralleltest

0 Kudos
LucD
Leadership
Leadership
Jump to solution

It is not straightforward, but it is doable, provided you know that you need to access variable with $Using inside an inlinescript.

See the following example:

workflow test {

    param(

        [string]$vcenter,

        [string[]]$names,

        [string]$session

    )

    foreach -parallel($name in $names){

      $vm = InlineScript{

            Add-PSSnapin VMware.VimAutomation.Core

            Connect-VIServer -Server $Using:vcenter -Session $Using:session | Out-Null

            Get-VM -Name $Using:name

      }

      $vm.Name

    }

}

$vmNames = 'vm1','vm2','vm3'

test -names $vmNames -vcenter 'MyvCenter' -session $global:DefaultVIServer.SessionSecret

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
mark_chuman
Hot Shot
Hot Shot
Jump to solution

Thanks a lot for this!  Definitely helped me get started.  I modified it to create VMs just as a test and it runs much quicker, but it appears to still run the creations in batches.  I'll investigate, but I may be hitting a concurrent action limit for VM creation or something.

*Curious.  How do are you pasting your code into the reply to allow for it to keep it's coloring/formatting?

workflow test {

    param(

        [string]$vcenter,

        [string[]]$names,

        [string]$session

    )

    foreach -parallel($name in $names){

      $vm = InlineScript{

            Add-PSSnapin VMware.VimAutomation.Core

            Connect-VIServer -Server $Using:vcenter -Session $Using:session | Out-Null

            New-VM -Name $Using:name -VMHost 'servernameesx.fmr.com' -DiskMB 40

      }

      $vm.Name

    }

}

$vmNames = 'vm01','vm02','vm03', 'vm04', 'vm05', 'vm06', 'vm07', 'vm08', 'vm09', 'vm10', 'vm11', 'vm12', 'vm13', 'vm14', 'vm15', 'vm16', 'vm17', 'vm18', 'vm19', 'vm20'

test -names $vmNames -vcenter 'vcname' -session $global:DefaultVIServer.SessionSecret

0 Kudos
mark_chuman
Hot Shot
Hot Shot
Jump to solution

I think it's a limit on my powershell session amount or a limit of the resources on the box I'm running it from

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, it might be a resource limitation on your PC or even on your vSphere server.

Btw, I just did a short post on this, see PowerCLI and PowerShell Workflows


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
mark_chuman
Hot Shot
Hot Shot
Jump to solution

I'm thinking it's the VDI I'm running it from as we are pushing this particular VC really hard with Jenkins related activity.  I'll go over the optimization steps here - vElemental » Blog Archive » Optimizing the execution of Powershell and PowerCLI and rule out resources on my end.  Looks like a great post on your site.  Really interested to develop mult-threading more in our environment.  Thanks!!

0 Kudos
mark_chuman
Hot Shot
Hot Shot
Jump to solution

Optimization really helped, but I think I'm getting hit by this - To My Future Self » Blog Archive » PowerShell Workflow limits concurrent InlineS...

This optimization worked for me -

http://blogs.vmware.com/PowerCLI/2011/06/how-to-speed-up-the-execution-of-the-first-powercli-cmdlet....

comment by Atanas Dimitrov Atanasov

%windir%\Microsoft.NET\Framework64\v4.0.30319\ngen.exe install “VimService55.XmlSerializers, Version=5.5.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f” /ExeConfig:%windir%\system32\WindowsPowerShell\v1.0\PowerShell_ISE.exe

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Great find !


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
mark_chuman
Hot Shot
Hot Shot
Jump to solution

I thought so as well..., at first, but now I'm finding that changing this setting (

-MaxActivityProcesses<Int32>)

isn't as straight-forward as you would think - ie, changing the setting only affects new PS sessions, so you would need to launch a new, remote session to the local box for example.  And it appears the author has come to the conclusion that he had to just live with the 5 process limit (see below):

"That meant we had to live with the 5-process limit. The way to do that is to keep InlineScript blocks short, so they don’t hold up executions processes forever and starve the scheduler. By keeping them short enough (< 1 minute each), multiple conc"

0 Kudos