简体   繁体   English

如何通过运行PowerShell脚本显示实时输出?

[英]How to show real-time output by running a PowerShell Script?

I am trying to run powershell script from VB and I want to see the output of the script as it is running inside a console application. 我试图从VB运行powershell脚本,我想看到脚本在控制台应用程序中运行时的输出。 With my script (shown below) when I run from powershell it shows "Command Sleep Starting" and then waits for 5 seconds and then displays the other text. 使用我的脚本(如下所示),当我从powershell运行时,它显示“命令睡眠启动”,然后等待5秒,然后显示其他文本。

However, when I run from VB.NET program, the execution waits for 5 seconds and dumps all the text output at once. 但是,当我从VB.NET程序运行时,执行等待5秒并立即转储所有文本输出。 It doesn't execute first Write-Output command and then wait and then output as it should. 它不执行第一个写输出命令然后等待然后输出它应该。

Write-Output "Command Sleeping Starting"
Start-Sleep -Seconds 5
Write-Output "Command ran successfully"

Any idea on how to show real-time execution output when I run the script from VB .Net Program? 当我从VB .Net程序运行脚本时,有关如何显示实时执行输出的任何想法吗?

Just for more info below is the code I used. 以下是我使用的代码。

            Dim start As New ProcessStartInfo
            Dim ScriptsFolder As String = GetKeyValue("ScriptsFolder")
            Console.WriteLine(ScriptsFolder.ToString())
            start.FileName = "powershell.exe"
            start.Arguments = ScriptsFolder + "\" + ScriptFile
            start.UseShellExecute = False
            start.RedirectStandardOutput = True
            start.RedirectStandardError = True

            Dim myproc As New Process
            myproc.StartInfo = start
            myproc.Start()
            Dim so As System.IO.StreamReader
            Dim se As System.IO.StreamReader
            se = myproc.StandardError
            so = myproc.StandardOutput
            myproc.WaitForExit()
            Console.WriteLine(so.ReadToEnd)
            Console.WriteLine(se.ReadToEnd)

Have you considered executing a PowerShell runspace programmatically using the System.Windows.Automation namespaces rather than starting the process? 您是否考虑过使用System.Windows.Automation命名空间以编程方式执行PowerShell运行空间而不是启动该进程? You can then pass in a logger object to the pipeline and log to it, displaying the messages in realtime. 然后,您可以将记录器对象传递给管道并记录到它,实时显示消息。

Here is a simple snippet for spinning up a Runspace in VB.NET 这是一个简单的片段,用于在VB.NET中启动Runspace

    ' create Powershell runspace 
    Dim MyRunSpace As Runspace = RunspaceFactory.CreateRunspace()

    ' open it 
    MyRunSpace.Open()

    ' create a pipeline and feed it the script text 
    Dim MyPipeline As Pipeline = MyRunSpace.CreatePipeline()

    MyPipeline.Commands.AddScript(scriptText)

    ' pass in a logger object you can use instead of Write-Output
    MyPipeline.Runspace.SessionStateProxy.SetVariable("logger", SomeLoggerObject) 

    ' execute the script 
    Dim results As Collection(Of PSObject) = MyPipeline.Invoke()

    ' close the runspace 
    MyRunSpace.Close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM