Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get-RunspaceData error: Collection was modified; enumeration operation may not execute #52

Open
janegilring opened this issue Dec 15, 2017 · 1 comment

Comments

@janegilring
Copy link

I am leveraging Invoke-Parallel to process VM actions in parallel in an Azure Automation runbook:

        $AzureRMVMs | Invoke-Parallel -ScriptBlock {

            $ARMVM = $PSItem

            $VMStatus = $ARMVM | Get-AzureRmVM -Status | Select-Object Name, @{n = 'PowerState'; e = {($PSItem.Statuses | Where-Object Code -Like "*PowerState*").DisplayStatus}}

            switch ($using:VMAction) {

                'Start' {                        

                    if ($VMStatus.PowerState -ne 'VM running') {

                        Write-Output "VM $($ARMVM.Name) is not running - starting"

                        $ARMVM | Start-AzureRmVM

                    } else {

                        Write-Output "VM $($ARMVM.Name) is already running - no action triggered"

                    }

                }
                'Stop' {

                    if ($VMStatus.PowerState -eq 'VM running') {
                        
                        Write-Output "VM $($ARMVM.Name) is running - stopping"
                        
                        $ARMVM | Stop-AzureRmVM -Force
                        
                    } else {
                        
                        Write-Output "VM $($ARMVM.Name) is already stopped - no action triggered"
                        
                    }

                }
                'Default' {Write-Output "VM Action $($using:VMAction) not defined in runbook, no action taken"}

            }

        } -RunspaceTimeout 300 -NoCloseOnTimeout

This works as expected, however, the following error is generated in the error stream:
Get-RunspaceData : Collection was modified; enumeration operation may not execute.
At Invoke-Parallell 542 char:13

  •         Get-RunspaceData -wait
    
  •         ~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : CloseError: (:) [Write-Error], InvalidOperationException
    • FullyQualifiedErrorId : Get-RunspaceData

Any idea what might be causing this?

@vors
Copy link
Collaborator

vors commented Dec 15, 2017

Nice catch!
Indeed there is a collection modification inside a foreach loop
The simplified version of this code is:

Foreach($runspace in $runspaces) {
...
                    $temphash = $runspaces.clone()
                    $temphash | Where-Object { $_.runspace -eq $Null } | ForEach-Object {
                        $Runspaces.remove($_)
                    }
...
}

https://github.com/RamblingCookieMonster/Invoke-Parallel/blob/master/Invoke-Parallel/Invoke-Parallel.ps1#L325-L328

UPD: nevermind, it happens outside of this loop, it should be something else...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants