Skip to content

Accessing global functions from inside ForEach-Object -Parallel #12313

@devmanlab

Description

@devmanlab

Hello,
I am processing large amount of Graph calls using powershell script block - 500K in my case. I want to process script blocks in batches. My ForEach-Object loop is defined inside class, say Library, in method, say ExportItems. Library.ExportItems call global function named Log([string]text). Method can successfully use it, however Parallel block cannot.

Steps to reproduce

using module .\logger.ps1

class SPOLibrary {
 
    [void]ExportItems (
        [string]$OutputFileName
    ) {

       try {

            # MAIN: process loaded queue 
            $this.Process( $OutputFileName)    
        
        }
        finally {


        
        }

        
    }

    [void]Process (
        [string]$OutputFileName
    ){

     
        [int]$_batchSize = 20
 

            1..$_batchSize | ForEach-Object -Parallel {

            # NOW here is the global function that is not visible to this block
            Log "text"
            }

     }

Expected behavior

global functions should be visible within script block

Actual behavior

function is not visible

Environment data

Name                           Value
----                           -----
PSVersion                      7.0.0
PSEdition                      Core
GitCommitId                    7.0.0
OS                             Microsoft Windows 10.0.19603
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-AnsweredThe question is answered.WG-Cmdlets-Corecmdlets in the Microsoft.PowerShell.Core module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions