【问题标题】:How to hide console but not GUI in a self elevating Powershell script如何在自升式 Powershell 脚本中隐藏控制台而不是 GUI
【发布时间】:2019-10-04 19:38:49
【问题描述】:

我想为我的 Powershell 脚本制作一个 GUI,以便其他人也可以轻松使用它们。 我有一个主菜单脚本,女巫调用了其他一些脚本。 对于其中一个,我需要一个提升的 Powershell 进程。

if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSFilePath`"" -Verb RunAs; exit }

现在我的问题是,不仅显示来自 $PSFilePath 的 GUI,而且在后台显示一个空的控制台窗口

我尝试使用 -WindowStyle 隐藏

if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSFilePath`"" -WindowStyle Hidden -Verb RunAs; exit }

但这导致控制台和 GUI 都被隐藏了。

无论如何要隐藏那个控制台窗口而不是 GUI?

【问题讨论】:

    标签: winforms powershell


    【解决方案1】:

    Try...

    # Hide PowerShell Console
    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);
    '
    $consolePtr = [Console.Window]::GetConsoleWindow()
    [Console.Window]::ShowWindow($consolePtr, 0)
    

    但是您的帖子可能与此重复..

    Opening PowerShell Script and hide Command Prompt, but not the GUI

    【讨论】:

    【解决方案2】:

    我前段时间找到了一个解决方案,它被埋在这里的某个地方,在 StackOverflow 上,但现在找不到指向它的链接。它将打开并立即关闭空控制台窗口。

    只需将其粘贴到脚本的开头即可:

    $dllvar = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
    add-type -name win -member $dllvar -namespace native
    [native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多