1

I need to run some code in powershell using multi-thread, i have tested a simple snippet and it runs fine on a powershell console. however when i try to run on a cmd.exe the code doesnt execute and no error was thrown wondering what is going on? if someone you help on this.

sample code as follow

$throttleLimit = 10

$iss = [system.management.automation.runspaces.initialsessionstate]::CreateDefault()
$Pool = [runspacefactory]::CreateRunspacePool(1, $throttleLimit, $iss, $Host)
$Pool.Open()

$ScriptBlock = {
param($id)
Start-Sleep -Seconds 2
Write-Host "Done processing ID $id"
[System.Console]::WriteLine("Done processing ID $id")
}

for ($x = 1; $x -le 40; $x++) {
$powershell = [powershell]::Create().AddScript($ScriptBlock).AddArgument($x)
$powershell.RunspacePool = $Pool
$handle = $powershell.BeginInvoke()
}

my batch file code is as follow

powershell -Command .\multiT.ps1 2>&1
1
  • Why aren't you using PowerShell Jobs instead? Commented Feb 6, 2013 at 14:34

1 Answer 1

1

In the ISE, the script finishes before the output from the threads starts to show up. I added start-sleep -sec 10 to the end of the code and I get output from cmd now. For some reason the output is doubled, though (as in, I get 2 lines for each thread).

Sign up to request clarification or add additional context in comments.

1 Comment

its now showing on the console but like you said it has duplicate printout. i guess you were right the console exit before the thread finishes. thanks for the help, i will look into why showing duplicate results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.