PFB代码。我试图直接捕获输出,但没有运气。然后我尝试将输出放入日志文件中,然后从日志文件中捕获文本。这也没有用。你能告诉我这有什么不妥吗?
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 600
$Form.height = 600
$Form.Text = "Form Title"
$Form.startposition = "centerscreen"
$Form.FormBorderStyle= [System.Windows.Forms.FormBorderStyle]::Fixed3D
Function ShowProcess(){
$Textbox1 = New-Object System.Windows.Forms.RichTextBox
#$text = Get-Process | Out-String
$Textbox1.Size = New-Object System.Drawing.Size(550, 400)
$Textbox1.Multiline = $true
$Textbox1.Font ="Lucida Console"
$Textbox1.WordWrap = $false
Start-Transcript -Path C:\Mywork\log.txt
&C:\Mywork\Script\test.bat
Stop-Transcript
$Textbox1.Text = Get-Content C:\Mywork\log.txt
$Form.Controls.Add($Textbox1)
}
$button1 = New-Object System.Windows.Forms.Button
$button1.Location = New-Object System.Drawing.Point(350, 450)
$button1.Size = New-Object System.Drawing.Size(120, 100)
$button1.Text = "Press"
$button1.FlatAppearance.BorderSize=0
$Form.Controls.Add($button1)
$button1.Add_Click({ShowProcess})
$Form.ShowDialog()
以下是test.bat
的内容:
PS C:\Users\sghosh> Get-Content C:\Mywork\Script\test.bat tree pause
答案 0 :(得分:1)
从批处理文件中删除test.bat
。它会提示用户进行确认,但由于您从PowerShell代码以非交互方式运行脚本,因此会导致批处理脚本挂起。因此,没有任何批输出到达表单。
将@echo off
tree
更改为:
Start-Transcript -Path C:\Mywork\log.txt
&C:\Mywork\Script\test.bat
Stop-Transcript
$Textbox1.Text = Get-Content C:\Mywork\log.txt
并在PowerShell脚本中更改这些行
$Textbox1.Text = & C:\Mywork\Script\test.bat *>&1 | Out-String
到此:
@media screen and (max-width: 800 px)