VMware Cloud Community
ichalac
Contributor
Contributor

PowerCLI ForEach-Object -parallel

So I was trying out the -parallel parameter of the new Powershell 7 Core, but it doesn't seem to be working with PowerCLI. Here is the simple snippet:

Connect-VIServer "vcenter" -User "user" -Password "pass"

Measure-Command{
    $vmArr = Get-VM
    $vmArr | ForEach-Object {
        Get-SpbmEntityConfiguration -HardDisk (Get-HardDisk -vm $_)
    }
}


I'm getting the following error:

Get-HardDisk:

Line |

   2 |          Get-SpbmEntityConfiguration -HardDisk (Get-HardDisk -vm $_)

     |                                                 ~~~~~~~~~~~~~~~~~~~

     | You have  modified the global:DefaultVIServer and global:DefaultVIServers system variables. This is not allowed. Please reset them to $null and reconnect to the vSphere server.

Without the -parallel parameter it works fine.

Versions: Powershell Core 7.0, PowerCLI 11.5

4 Replies
LucD
Leadership
Leadership

The current PowerCLI version (11.5.0) does not support PowerShell v7.

From the Compatibility Matrixes for VMware PowerCLI 11.5.0

Supported Windows PowerShell Versions

VMware PowerCLI 11.5.0 is compatible with the following PowerShell versions:

  • Windows PowerShell 4.0
  • Windows PowerShell 5.0
  • Windows PowerShell 5.1
  • PowerShell Core 6.x (for Ubuntu and macOS)


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

Reply
0 Kudos
Jahedges
Contributor
Contributor

Not sure if new thread is best, but I am now running into this same issue on supported version 12.0 .

I have tried to scope the -server variable with using and global and it doesn't seem to make any difference.

My use case is vmkping via esxcli to test if there any packet drops.

$mgmtVMK = get-vmhostnetworkadapter -VMKernel | Where-object {$_.ManagementTrafficEnabled -eq $true}

        $mgmtIP = $mgmtVMK.IP

        if ($turboCore)  #powershell 7 check

        {

            $mgmtVMK | ForEach-Object -Parallel {

                $vmk = $_

                foreach ($ip in $using:mgmtIP)

                {

                    $esxcli = Get-ESXCli -VMHost $vmk.VMHost -V2 -Server $global:DefaultVIServer

                    $param = $esxcli.network.diag.ping.CreateArgs()

                    $param.host = "$ip"

                    $param.interface = "$vmk"

                    $param.count = "2"

                    $pingResult = $esxcli.network.diag.ping.Invoke($param)

                    if ($pingResult.Summary.PacketLost -ne 0)

                    {

                        Write-HTML "<h3>$($vmk) on $($vmk.VMHost.Name) dropped a packet</h3>"

                        $PacketDrops += 1

                    }

                }

Reply
0 Kudos
LucD
Leadership
Leadership

That is unfortunately exactly the same problem as the 1st entry in this thread.

The -Parallel switch causes issues with how some of the global variables that PowerCLI uses are implemented.

The Dev Team is working on a solution.

In the meantime, upvote the idea to show that this is a critical feature in PSv7 for many PowerCLI users.


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

Reply
0 Kudos
LucD
Leadership
Leadership

I should, of course, provide a link to the idea Support for the PSv7 feature Foreach -Parallel


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