2

I am running some PowerShell scripts in C# using the System.Management.Automation namespace. This works perfectly, except when I invoke the script, I very briefly see a command window flash onto the screen and then disappear. This behavior is not acceptable in my application.

I can avoid this behavior by launching a new Process and setting CreateNoWindow=true, however I would really prefer to use the managed PowerShell library. How can I do this in a non-interactive way that does't flash on the screen?

An example of some PowerShell that causes the window flash:

using (PowerShell ps = PowerShell.Create())
{
    ps.AddCommand(@"Get-WindowsUpdateLog");
    ps.AddParameter(@"-LogPath", @"C:\WindowsUpdate.Log");
    var psOutput = ps.Invoke();
}
2
  • i think you need a custom runspace msdn.microsoft.com/en-us/library/dn569260(v=vs.85).aspx Commented Apr 24, 2018 at 21:13
  • I took a look at the custom runspace and initial session state but couldn't find anything pertaining to UI interactivity. Commented Apr 24, 2018 at 21:27

1 Answer 1

2

I believe the flash you're seeing is because the Get-WindowsUpdateLog function internally calls tracerpt.exe which is a console app. Running a console app from a GUI results in a flash. (You'll see the same flash if you run GetWindowsUpdateLog from the ISE.) Unfortunately there isn't much you can do about it. You could modify you Win32 app to allocate a console handle at startup controlling when you see the flash but you'll still see it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.