PowerShell如何将输出重定向到另一个PowerShell屏幕
本文关键字:PowerShell 另一个 屏幕 重定向 输出 | 更新日期: 2023-09-27 18:04:27
我需要能够将一些自定义输出从当前的PowerShell屏幕(你可以看到这有工作流)重定向到另一个PowerShell屏幕,这将是第一个(父)的子屏幕
注意**重定向到一个文件不是一个选项,因为它必须是"live"
所以我想知道它是否可以简单地在PowerShell中完成,或者在c#中打开流到子PowerShell进程。
谢谢
我认为没有办法直接写入另一个控制台窗口。
最好的办法可能是创建一个脚本来监视对您创建的环境变量的更改。
然后,您可以使用Start-Process、Invoke-Item或其他一些方法(有几种方法可以做到这一点)在一个新的控制台窗口中启动该脚本。
或者您可以使用WPF或浏览器窗口而不是第二个控制台来创建UI元素。
你看过Powershell Jobs (help about_Jobs)吗?
发现一些有趣的东西http://www.vistax64.com/powershell/16998-howto-create-windows-form-without-stopping-script-processing.html
仍然需要一些微调,但似乎工作
#
# Create the form - I simplified this a bit and eliminated some unnecessary complexity
#
Add-Type -AssemblyName System.Windows.Forms
$dForm = New-Object System.Windows.Forms.Form
$dForm.Size = new-object System.Drawing.Size @(640,480)
$dForm.BackColor = 'Black'
$dForm.Text = "Debug Information"
$dText = New-Object System.Windows.Forms.TextBox
$dText.BackColor = 'Black'
$dText.ForeColor = 'red'
$dText.ScrollBars = "both"
$dText.Dock = "fill"
$dText.Multiline = $true
$dText.Parent = $dForm
$dText.ReadOnly = $true
$dForm.Add_Shown({$dform.Activate()})
#
# Create the new runspace
#
$rs = [Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace()
$rs.Open()
# pass in the form object...
$rs.SessionStateProxy.SetVariable("dForm", $dForm)
# and set up a synchronized data interchange object
# between the two runspaces...
$data = [hashtable]::Synchronized(@{text=""})
$rs.SessionStateProxy.SetVariable("data", $data)
# Start the pipeline so the form will display...
$p = $rs.CreatePipeline({ [void] $dForm.ShowDialog()})
$p.Input.Close()
$p.InvokeAsync()
#
# Utility routine to show the text. This routine
# puts the text to display into the shared synchronized
# object then calls invoke. It may be that the form
# is not actually ready so it traps this errors and the retrys after a delay...
function Add-TextToWindow()
{
PARAM(
[parameter( Position=0, Mandatory=$true, HelpMessage = "Require String name as value ", ValueFromPipeline = $True )]
[String]
[ValidateNotNullOrEmpty()]
$text
)
$data.text = $text
[eventhandler]$eh = { $this.AppendText($data.text) }
#$data.text
do
{
$retry = @($false)
trap [InvalidOperationException]
{
start-sleep -Milliseconds 100
$retry[0] = $true
continue
}
$dText.Invoke($eh, ($dText, [eventargs]::empty))
}
while($retry[0])
}
#
# Now loop for a while writing out some text...
#
#Add-TextToWindow "The date is $(get-date)`n"
#1..30 | %{ Add-TextToWindow "Some text $_`n"; start-sleep -milli 100 }
#Add-TextToWindow "The date is $(get-date)`n"
start-sleep 2
$dForm.Close()
$rs.Close()