1

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. 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.

However, when I run from VB.NET program, the execution waits for 5 seconds and dumps all the text output at once. 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?

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)
1
  • Someone was trying to do this with Java. I'm not sure stdout can be read until the process exits.. Commented Mar 1, 2012 at 22:07

1 Answer 1

4

Have you considered executing a PowerShell runspace programmatically using the System.Windows.Automation namespaces rather than starting the process? 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

    ' 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()
Sign up to request clarification or add additional context in comments.

3 Comments

Agree with this. Instead of invoking PowerShell.exe, just use the API :-)
I do agree the above approach is the way to run powershell command but I am sorry that my question was not that. I wanted to know if we could show incremental output as the script is running. Will this approach show incremental output?
If you pass in a logger object to your script and then replace the write-output calls with calls to your logger, then the ability to show real-time data from your logger will depend on it's implementation. An Example would be a TraceListener model where you could subscribe any listener you wish and visualize the logging output anyway you want.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.