htcfreek / SimpleLapsGui

A simple and fast GUI for Microsoft LAPS (legacy) and Windows LAPS. With this tool you can query passwords and change the expiration timestamp.
MIT License
37 stars 2 forks source link

[PowerShell only] Auto hide PS window #4

Closed htcfreek closed 1 year ago

htcfreek commented 1 year ago

Same as the title.

F1reF0x commented 1 year ago

A possible implementation you can find at https://gist.github.com/F1reF0x/abbd05668719b5670f07736b1eb5ac5f

htcfreek commented 1 year ago

@F1reF0x Can you post the auto hide code here? Can't access the link.

F1reF0x commented 1 year ago
# .Net methods for hiding/showing the console in the background
# Usage: show-console -show / show-console -hide
function Show-Console
{
    param ([Switch]$Show,[Switch]$Hide)
    if (-not ("Console.Window" -as [type])) { 

        Add-Type -Name Window -Namespace Console -MemberDefinition '
        [DllImport("Kernel32.dll")]
        public static extern IntPtr GetConsoleWindow();

        [DllImport("user32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
        '
    }

    if ($Show)
    {
        $consolePtr = [Console.Window]::GetConsoleWindow()

        # Hide = 0,
        # ShowNormal = 1,
        # ShowMinimized = 2,
        # ShowMaximized = 3,
        # Maximize = 3,
        # ShowNormalNoActivate = 4,
        # Show = 5,
        # Minimize = 6,
        # ShowMinNoActivate = 7,
        # ShowNoActivate = 8,
        # Restore = 9,
        # ShowDefault = 10,
        # ForceMinimized = 11

        $null = [Console.Window]::ShowWindow($consolePtr, 5)
    }

    if ($Hide)
    {
        $consolePtr = [Console.Window]::GetConsoleWindow()
        #0 hide
        $null = [Console.Window]::ShowWindow($consolePtr, 0)
    }
}

Before showing the Dialog just show-console -hide

When you start the powershell script: before the Dialog is shown, the console window will be hidden. After ShowDialog() you can: show-console -show

htcfreek commented 1 year ago

Fixed and fix released with version 1.2.