I have 3 powershell scripts. First one run second(monitor for third) and third(do some actions).
When execution of third one is done (it's called in blocking way (we are waiting for exit)), i want to tell second(monitor that is called in non-blocking way) to end his action and return some values. How should I do it? I am checking in monitor script if user pressed s
key and return some values if true. I need to "press" that key in first script.
My code is like this:
#some code
$psi = new-object "Diagnostics.ProcessStartInfo"
$psi.FileName = "artillery"
$arguments = "my fancy args"
$psi.Arguments = $arguments
$proc = [Diagnostics.Process]::Start($psi)
$psi2 = new-object "Diagnostics.ProcessStartInfo"
$psi2.FileName = "artillery_metric_getter.ps1"
$proc2 = [Diagnostics.Process]::Start($psi2)
$proc2.Start();
if ( $waitForExit ) {
$proc.WaitForExit();
$stdout = $proc.StandardOutput.ReadToEnd()
$stderr = $proc.StandardError.ReadToEnd()
Write-Host "stdout: $stdout"
Write-Host "stderr: $stderr"
}
#here i want to "press 's' key for $proc2 and catch values he return
The code of powershell script that is called in $proc2
is like:
for(($i=1),($i>0),($i++)){
#there is some code
$key = $Host.UI.RawUI.ReadKey()
if ($key.Character -eq 's') {
$meanMemRet = 123 #there is some calc
$meanProcRet = 456 #there is some calc
return $meanProcRet,$meanMemRet
}
}
TLDR: So we need to "press" 's' button in first script to stop execution of monitor script. How to do it?
Given that you're okay with having a solution using a Runspace, this is how you can approach your code. Worth noting, passing arguments to the runspace is fairly easy, certainly easier than passing arguments to a
Process
.Example using
.AddParameters
:In your case, you can add a
param
block to yourartillery_metric_getter.ps1
. It's also worth noting that, the script invokingartillery_metric_getter.ps1
must block, awaiting for either ans
key press or the task completion.Following up from comments, since the runspace is hooked to your
$Host
listening ons
key press is also possible inside it, using this as an example forartillery_metric_getter.ps1
:Then from your main thread you leave the while loop empty, blocking until the task is done (until
s
key is pressed):