Alex F
Alex F

Reputation: 3539

Start-Job: Call another functions in a script

After reading a lot of Q&A here on SO about Start-Job I am still can not understand what I am doing wrong...

The main idea: I need to run a lot of functions that call another functions with different parameters. Something like this:

function Base-Function {
 PARAM(
   [string]
   $Param
 )

 # I will do something with $Param
}


function Copy-File {
  PARAM(
    [string]
    $CopyFileParam
  )
  Base-Function -Param $CopyFileParam
}

function Read-File {
  PARAM(
    [string]
    $ReadFileParam
  )

  Base-Function -Param $ReadFileParam
}

function Move-File {
  PARAM(
    [string]
    $MoveFileParam
  )

  Base-Function -Param $MoveFileParam
}

So - I am trying to call Copy-File, Read-File and Move-File simultaneously:

function Main-Function {
  $copyFileArgs = @{ "CopyFileParam" = 1 }
  Start-Job -ScriptBlock ${Function:Copy-File} -ArgumentList $copyFileArgs

  $readFileArgs = @{ "ReadFileParam" = 2 }
  Start-Job -ScriptBlock ${Function:Read-File} -ArgumentList $readFileArgs 

 ...
 ...
}

but of course I can not call Base-Function inside Copy-File function this way so I added -InitializationScript argument:

$init = {
   function Base-Function {
    PARAM(
      [string]
      $Param
    )

    # I will do something with $Param
   }
}

and then I call it like this:

function Main-Function {
  $copyFileArgs = @{ "CopyFileParam" = 1 }
  Start-Job -ScriptBlock ${Function:Copy-File} -ArgumentList $copyFileArgs -InitializationScript $init
}

but then I get an error:

OpenError: [localhost] An error occurred while starting the background process. Error reported: An error occurred trying to start process 'C:\Program Files\PowerShell\7\pwsh.exe' with working directory 'C:\Projects\powershell\project'. The filename or extension is too long..

So my question is:

  1. Any suggestion to simultaneously call different function that in they turn call to some in-script functions ?
  2. Why I get this error The filename or extension is too long ?

Here is a link to powershell script for example: Gist

  1. Run the script and let it finish
  2. In the same shell window check for jobs: Get-Job
  3. Check the output of running job: Receive-Job xxx

see that output of job is:

ObjectNotFound: The term 'Base-Function' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
ObjectNotFound: The term 'Base-Function' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Upvotes: 1

Views: 704

Answers (1)

Alex F
Alex F

Reputation: 3539

Sorry for misinforming you; the code above is correct and functional (many thanks to @mklement0 for hints and suggestions).

The actual problem that I encountered was in this line:

OpenError: [localhost] An error occurred while starting the background process. 
Error reported: An error occurred trying to start process 'C:\Program Files\PowerShell\7\pwsh.exe' with working directory 'C:\Projects\powershell\project'. 
The filename or extension is too long..

The filename or extension is too long.. -> this means that there is a character's length limit for what can be passed in the '-InitializationScript' parameter. You could check it in Gist example above - everything work OK.

Here is Stakoverflow question that give me this idea: Max size of ScriptBlock / InitializationScript for Start-Job in PowerShell

Once I put all my code instead in -InitializationScript parameter inside ps1 script file and then dot source it inside function - everything started working perfectly.

Thanks again @mklement0

Upvotes: 1

Related Questions