1

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.
Alex F
  • 3,180
  • 2
  • 28
  • 40
  • Can you get a better description of what this function is supposed to do and what kind of data you're trying to pass to these functions? – another victim of the mouse Apr 28 '22 at 16:19
  • I don't see a problem with your code, and it works fine when I run it on PowerShell 7.2.2 and 7.2.3 - what version are you on? Can you get _any_ background job to work, without `-InitializationScript`? – mklement0 Apr 28 '22 at 16:52
  • @mklement0 -> I added an example + gist -> could you make my script work ? – Alex F Apr 28 '22 at 17:51
  • I'm guessing since the Base-Function isn't defined within the scriptblock, it doesn't exist when it executes. You could define all of the functions within the script block, or load the content in of all the functions from a file if that makes it cleaner. – joelforsyth Apr 28 '22 at 18:47
  • @AlexF, your Gist is missing the `-InitializationScript` part where you pass a script block with the function definition to the job. – mklement0 Apr 28 '22 at 19:15

1 Answers1

1

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

Alex F
  • 3,180
  • 2
  • 28
  • 40
  • Glad to hear my pointers helped, @Alex. I've just posted [an answer](https://stackoverflow.com/a/72060727/45375) to the linked question that details the length limits that an `-InitializationScript` argument is subject to. A workaround would be to write the functions to a `.ps1` file and dot-source it from the `-ScriptBlock` code. – mklement0 Apr 29 '22 at 16:11