Hi,
I have an array of 10000 commands that I want to process through an application, but only 5 commands in parallel at the same time. So I will take 5 commands from the array $arrCommands, then run those in parallel, then take another 5 commands..process..and
so on.. I might have to use "scriptblock" functionality, but with current setup the chance is it will try to run thousands commands in parallel which will actually kill the server. I won't use "Foreach -parallel" because my server running
Powershell V2.
Can anyone suggest how can I use scriptblock functionality to receive and process 5 commands in parallel?
----
## Function to process commands
Function ProcessCommands
{
param
(
[string]$ParamCommand
)
[hashtable]$Return = @{}
$objProcess = New-Object System.Diagnostics.Process
$objProcess.StartInfo = New-Object System.Diagnostics.ProcessStartInfo
$objProcess.StartInfo.FileName = "\\OFCAPPSRVR\apps\calcrun.exe"
$objProcess.StartInfo.Arguments = $Parameter
$objProcess.StartInfo.UseShellExecute = $shell
$objProcess.StartInfo.WindowStyle = 1
$objProcess.StartInfo.RedirectStandardOutput = $true
$null = $objProcess.Start()
$objProcess.WaitForExit()
$ExitCode = $objProcess.ExitCode
$StandardOutput = $objProcess.StandardOutput.ReadToEnd()
$objProcess.Dispose()
$Return.ExitCode = $ExitCode
$Return.StandardOutput = $StandardOutput
Return $Return
}
## Main
$arrCommands =
@(
"STD -iMXB9010 -o\\OFCAPPSRVR\outputs\MXB9010.pdf",
"STD -iMXB6570 -o\\OFCAPPSRVR\outputs\MXB6570.pdf",
"STD -iMXB8010 -o\\OFCAPPSRVR\outputs\MXB8010.pdf",
"STD -iMXB5090 -o\\OFCAPPSRVR\outputs\MXB5090.pdf",
"STD -iMXB2440 -o\\OFCAPPSRVR\outputs\MXB2440.pdf",
.
.
"STD -iMXB8440 -o\\OFCAPPSRVR\outputs\MXB8440.pdf"
)
foreach ($Command in $arrCommands)
{
$Return = ProcessCommands -ParamCommand $Command
$arrResults += New-Object Psobject -property @{COMMAND=$Command; EXITCODE=$Return.ExitCode; OUTPUT=$Return.StandardOutput}
}
----