PowerShell window closes during `Get-LSUpdate`
Describe the bug Originall reported by @osxdude as part of #33 :
Testing this out on a few laptops, but I'm looking mainly at my T590 (20N4) on Win10 20H2.
I've noticed when I run Get-LSUpdate at a PowerShell prompt, eventually the window disappears and there isn't any indication that it's running anymore. This is especially annoying using $updates as the variable clears once the window closes. I think it does complete the Get-LSUpdate process. After checking -Debug, it looks like it during _ExternalDetection of tbtesdkfw_1015 is when it closes.
Please complete the following information:
- Computer model: 20N4
Copy and paste answer from Issue #33 below:
It's a very annoying problem, because it's apparently just the intended behavior of this Lenovo tool, which is being run as part of the detection process of this package, to hide the console it was started from. If you download this exe manually and run it from a cmd or PowerShell window, it'll hide that window too. No idea why they're doing that. You are correct in that this doesn't stop the commands from finishing, the PowerShell process continues to run normally in the background.
I have found a few ways I could prevent this:
- I could just programmatically un-hide the console again after running external commands. It would still very quickly disappear and reappear though, and it would be quite a bit of work to try and faithfully restore the previous state of the window (e.g. was it maximized, minimized, active/focused, in the background ...) - not ideal
- If I run the external command(s) through an OutOfProcessRunspace then
tbtesdkfw_1015_version.exeisn't able to find its parent console window and therefore doesn't hide it. But using runspaces adds complexity and migth introduce other bugs. I'd have to test this for a while.- Running the process with ShellExecute() instead of CreateProcess() also sufficiently isolates it from the powershell process so that it can't find and hide the window. This isn't a workable solution though because I don't want to run all external commands through ShellExecute() because it cannot capture their output which is very useful to have e.g. to diagnose cases where an update fails to install
I'm going to have to spend some time trying out things and testing the Runspace idea, one (kind of silly) thing you could do in the meantime is use the following code to un-hide the window again after it's gone missing:
Add-Type -TypeDefinition ' using System; using System.Runtime.InteropServices; public class NativeMethods { [DllImport("Kernel32.dll", SetLastError=true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern IntPtr GetConsoleWindow(); [DllImport("user32.dll", SetLastError=true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool IsWindowVisible(IntPtr hWnd); [DllImport("User32.dll", SetLastError=true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool ShowWindow([In] IntPtr hWnd, [In] Int32 nCmdShow); public static int RestoreConsoleVisibility() { IntPtr hWndConsole = GetConsoleWindow(); bool IsVisible = IsWindowVisible(hWndConsole); Console.WriteLine("Console currently visible? : {0}", IsVisible); if (hWndConsole == IntPtr.Zero) { Console.WriteLine("GetConsoleWindow returned NULL-pointer"); return Marshal.GetLastWin32Error(); } else { Console.WriteLine("GetConsoleWindow returned a valid pointer ({0}), restoring window", hWndConsole); if (ShowWindow(hWndConsole, 5) == true) { // the window was previously visible Console.WriteLine("The console window was already visible"); return 0; } else { // the window was previously hidden Console.WriteLine("Restored the console window after it was hidden"); return 0; } } } } ' Get-LSUpdate <# your parameters #>; [NativeMethods]::RestoreConsoleVisibility()This uses the ShowWindow() API to restore the hidden window to a visible state after
Get-LSUpdatehas finished.
Same problem here. Lenovo ThinkPad X280 - Model 20KF
I've fixed this in Version 1.3.1 which I'll push to the PowerShell Gallery shortly.
All external commands are now run through an out-of-process powershell runspace which provides sufficient isolation between the powershell process running LSUClient and the tbtesdkfw_1015_version.exe program to prevent this annoyance.