0

I am trying to run multiple processes and then wait in the script for their execution to terminate. However, when calling $procs | Wait-Process an exception is thrown.

$SCRIPT_PATH = "path/to/Program.exe"
$procs = Get-ChildItem -Path $DIR | Foreach-Object {

    if ($_ -like "Folder") { 
        # Do nothing 
    }
    else {

        $ARG1_VAR = "Directory\$($_.BaseName)"
        $ARG2_VAR = "Directory\$($_.BaseName)\SubDirectory"
        $ARG3_VAR = "Directory\$($_.BaseName)\SubSubDirectory"

        if (Test-Path -Path $ARG1_VAR)
        {
            $ARG_LIST = @( "-arg1 $ARG1_VAR", "-arg2 $ARG2_VAR")

            Start-Process -FilePath $SCRIPT_PATH -ArgumentList $ARG_LIST -PassThru -NoNewWindow
        }
        else
        {
            
            $ARG_LIST = @( "-arg1 $ARG1_VAR", "-arg3 $ARG3_VAR")

            Start-Process -FilePath $SCRIPT_PATH -ArgumentList $ARG_LIST -PassThru -NoNewWindow
        }
    }
}

# ... Execute bunch of other code ...
# ...
# end of the script

$procs | Wait-Process

The exception says:

Wait-Process : The input object cannot be bound to any parameters of the command because the command does not accept pipeline inputs or the input and its properties do not match any of the parameters that accept pipeline input.
+ $procs | Wait-Process

    + CategoryInfo          : InvalidArgument: (GPSObject) [Wait-Process], ParameterBindingException       
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.WaitProcessCommand

I have tried to output both string "$procs" as well as non-string $procs. The string "$procs" outputs System.Object[] whereas $procs lists correct content (process name, ID, etc.) of all started processes. Therefore, the variable $procs seems to be filled correctly. However, is actually System.Object[] the correct data type to be passed to $procs | Wait-Process ? Shouldn't the data type look something like System.Diagnostics.Process[] instead of System.Object[]?

I am using PowerShell V5.1.

Any help would be much appreciated! Thank you :)!

SimpleThings
  • 147
  • 2
  • 12
  • Does `$procs | % GetType | Select -Unique` report more than just `[System.Diagnostics.Process]`? As an aside: `$_ -like "Folder"` doesn't filter out directories (assuming that is the intent); use `-File` in your `Get-ChildItem` call instead. – mklement0 Jul 08 '22 at 19:20
  • Did you try use -wait argument of start-process cmdlet? [start-process](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-7.2) – Alexey I. Kuzhel Jul 08 '22 at 20:06
  • No, I have not used -wait argument of start-process cmdlet because the idea of this script is to run the processes in parallel and not sequential so the runtime is shorter. – SimpleThings Jul 09 '22 at 05:54
  • `$procs | % GetType | Select -Unique` reports that the variable's `BaseType` is `System.ComponentModel.Component` – SimpleThings Jul 09 '22 at 06:05
  • The intent of `$_ -like "Folder"` is to recognize a particular folder inside of the directory `$DIR ` and do no action for it / ignore it. – SimpleThings Jul 09 '22 at 06:07

1 Answers1

0

Pass the array directly as an argument instead of piping it, like this:

$v1 = Start-Process -FilePath 'cmd' -PassThru
$v2 = Start-Process -FilePath 'cmd' -PassThru
Wait-Process -InputObject @($v1,$v2)
jmik
  • 311
  • 2
  • 10
  • Hello, I have actually tried to do that before as well. This works however it is another solution, and I want to understand why my original code does not work. I did slightly different than you since there is a foreach loop: `$procs = New-Object -TypeName "System.Collections.ArrayList" ... $proc = Start-Process -FilePath $SCRIPT_PATH -ArgumentList $ARG_LIST -PassThru -NoNewWindow $procs.Add($proc)` – SimpleThings Jul 09 '22 at 14:17