【问题标题】:How to send input to [Console]::In.ReadLine() from parent process?如何从父进程向 [Console]::In.ReadLine() 发送输入?
【发布时间】:2019-06-22 16:42:49
【问题描述】:

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

【问题讨论】:

  • 为什么不更改目标脚本以接受命令行参数而不是读取主机?
  • @IggyZofrin,据我了解,命令行参数只能传递一次,但就我而言,这还不够。我的想法是在运行空间中运行阻塞方法 ReadLine 并设置 parent 进程可以访问的 syncronized variables。例如,starter 写入 exit,然后运行空间设置 exit 标志,然后 parent 访问它并进行一些清理。当 parent 进程出现终止错误并且无法将其传达给运行空间(这就是我正在寻找的)时,就会出现问题

标签: powershell runspace


【解决方案1】:

PowerShell-script-packaged-as-an-*.exe packaging script you're using 出于某种原因不会将标准输入输入传递给包装的脚本,因此您的脚本永远不会收到您从调用者发送的"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.

【讨论】:

    猜你喜欢
    • 2011-04-24
    • 1970-01-01
    • 2014-10-08
    • 2012-12-09
    • 2019-12-28
    • 2013-02-15
    • 2019-06-02
    • 2017-02-24
    • 2019-04-11
    相关资源
    最近更新 更多