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 :)!