gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:52:19 29 4
gpt4 key购买 nike

入门程序用于启动目标脚本过程:

# 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)创建运行空间,该运行空间同步等待 启动程序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数据发送到运行空间?

最佳答案

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

以上 yield :
Submitting task 'doStuff'
Submitting exit request.
Background: Performing task: doStuff
Background: Exiting.

关于powershell - 如何从父进程向[Console]::In.ReadLine()发送输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56717302/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com