如何从父进程向[Console] :: In.ReadLine()发送输入?

时间:2019-06-22 16:42:49

标签: powershell runspace

启动器用于启动目标脚本过程:

# STARTING PS (TARGET) SCRIPT COMPILED TO EXE 
$processStartInfo = New-Object System.Diagnostics.ProcessStartInfo
$processStartInfo.FileName = $somePath
$processStartInfo.WorkingDirectory = (Get-Location).Path
$processStartInfo.RedirectStandardInput = $true
$processStartInfo.RedirectStandardError = $true
$processStartInfo.UseShellExecute = $false
$process = [System.Diagnostics.Process]::Start($processStartInfo)

# SOME OTHER CODE ...

# HERE I'M SENDING "EXIT" TO RUNSPACE RUNNING INSIDE TARGET SCRIPT
$process.StandardInput.WriteLineAsync("exit") | Out-Null

目标脚本(编译为* .exe)创建运行空间,该运行空间同步等待来自 starter

ReadLine数据
function main {
  . createRunspace
  while ($true) {
     # PARENT LOOP RUNS IN PARALLEL TO RUNSPACE LOOP
     sleep -s 1
     try {
       if ($hash.flags.exit) {
         # CLEAN UP AND BREAK
       } else {
         # RUN OTHER CODE
       }
     } catch {
       # CAN NOT NOTIFY RUNSPACE ABOUT ERROR USING SYNCHRONIZED HASTABLE,
       # BECAUSE RUNSPACE IS STUCK ON `ReadLine`.
       # ALSO CAN NOT WRITE TO STANDRAD INPUT (DON'T KNOW HOW).
     }
  }
}

function createRunspace {
  #CREATING RUNSPACE WITH SYNCHRONIZED HASTABLE
  $hash = [hashtable]::Synchronized(@{ flags: @{} })
  $runspace= [runspacefactory]::CreateRunspace()
  $runspace.Open()
  $runspace.SessionStateProxy.SetVariable('hash', $hash)
  $powershell= [powershell]::Create()
  $powershell.Runspace = $runspace
  $powershell.AddScript({
    # RUNSPACE LOOP
    while ($true) {
      $value  = [Console]::In.ReadLine()
      if ($value -eq "exit") {
         $hash.flags.exit = $true
         break
      } elseif ($value -eq "valueFromParent") {
         # DO STUFF
      }
    }
  }) | Out    
}

# OTHER CODE
. main 

是否可以将standard input数据从父级发送到运行空间?

1 个答案:

答案 0 :(得分:1)

由于某种原因,PowerShell-script-packaged-as-an-*.exe packaging script you're using不会将stdin输入传递给包装的脚本,因此您的脚本永远不会收到您从调用方发送的"exit"行。

我不知道您的确切要求,但是下面是一个简化的解决方案,它表明您的方法原则上可行:

# The code to execute in the background.
$backgroundScript = {
  while ($true) {
    $value = [Console]::In.ReadLine()
    if ($value -eq "exit") {
      "Background: Exiting."
      break
    }
    else {
      "Background: Performing task: $value"
    }
  }
}

# Start the background script.
$processStartInfo = [System.Diagnostics.ProcessStartInfo] @{
  FileName = "powershell.exe"
  Arguments = '-NoProfile', '-Command', $backgroundScript -replace '"', '\"'
  WorkingDirectory = $PWD.ProviderPath
  RedirectStandardInput = $true
  RedirectStandardError = $true
  RedirectStandardOutput = $true
  UseShellExecute = $false
}
$process = [System.Diagnostics.Process]::Start($processStartInfo)

# Ask the background script to perform a task.
"Submitting task 'doStuff'"
$process.StandardInput.WriteLine("doStuff")

# Ask the background script to exit.
"Submitting exit request."
$process.StandardInput.WriteLine("exit")

# Wait for the background script's process to exit,
# then print its stdout.
$process.WaitForExit()
$process.StandardOutput.ReadToEnd()

以上结果:

Submitting task 'doStuff'
Submitting exit request.
Background: Performing task: doStuff
Background: Exiting.