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

How to send input to [Console]::In.ReadLine() from parent process?

Starter用于启动目标脚本进程:

# 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数据的运行空间15=]

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 数据从 父级 发送到运行空间?

出于某种原因,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.