在PowerShell中隐藏标题栏

4
在Powershell环境中,是否可以隐藏标题栏或者至少移除关闭按钮?
我有一些脚本,希望用户在运行时不要干扰。我考虑将脚本设为隐藏状态,但这样系统会看起来像是卡住了一分钟或者完全完成了,但实际上在后台还在进行其他操作。

哦,有趣的问题。我不知道如何实现这个功能。虽然这没有回答你的问题,但我认为让用户不要去查看这些东西的最佳方法是给他们一些反馈,以向他们保证脚本没有停止运行或其他什么问题。在 PowerShell 中查找一些进度条的实现,有很多示例可以参考。否则,你可以每隔几秒钟输出一些信息。 - tnw
如果您想自定义窗口,可能需要编写自己的宿主来运行它。否则,通过进度条和/或状态消息向用户提供反馈,以便他们知道它没有卡住。 - alroc
3个回答

3

您可以使用此脚本禁用Windows控制台的关闭按钮。然而,用户仍然可以从任务栏关闭控制台,并且它不适用于控制台替代品,如ConEmu。

$code = @'
using System;
using System.Runtime.InteropServices;

namespace CloseButtonToggle {

 internal static class WinAPI {
   [DllImport("kernel32.dll")]
   internal static extern IntPtr GetConsoleWindow();

   [DllImport("user32.dll")]
   [return: MarshalAs(UnmanagedType.Bool)]
   internal static extern bool DeleteMenu(IntPtr hMenu,
                          uint uPosition, uint uFlags);

   [DllImport("user32.dll")]
   [return: MarshalAs(UnmanagedType.Bool)]
   internal static extern bool DrawMenuBar(IntPtr hWnd);

   [DllImport("user32.dll")]
   internal static extern IntPtr GetSystemMenu(IntPtr hWnd,
              [MarshalAs(UnmanagedType.Bool)]bool bRevert);

   const uint SC_CLOSE     = 0xf060;
   const uint MF_BYCOMMAND = 0;

   internal static void ChangeCurrentState(bool state) {
     IntPtr hMenu = GetSystemMenu(GetConsoleWindow(), state);
     DeleteMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
     DrawMenuBar(GetConsoleWindow());
   }
 }

 public static class Status {
   public static void Disable() {
     WinAPI.ChangeCurrentState(false); //its 'true' if need to enable
   }
 }
}
'@

Add-Type $code
[CloseButtonToggle.Status]::Disable()

2

您可以在PowerShell中使用Windows窗体,并隐藏控制框:

[Void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    $form = New-Object Windows.Forms.Form
    $form.ControlBox = $false
    $form.Text = "Test Form"
    $Button = New-Object Windows.Forms.Button

看起来会像这样:

在此输入图像描述


运行得很好...应该选择为最佳答案。谢谢。 - user5954196

2
唯一的选择是隐藏运行脚本的窗口,然后将以下内容添加到您的脚本中:
start-process powershell.exe -ArgumentList '-noprofile -command "&{get-content c:\temp\log.txt -Wait}"'

将脚本输出重定向到文件中,这样他们就可以在窗口中看到脚本输出,但是他们在该窗口中的任何操作都不会影响脚本。在脚本结束时,删除日志文件,日志窗口将关闭。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接