在Powershell环境中,是否可以隐藏标题栏或者至少移除关闭按钮?
我有一些脚本,希望用户在运行时不要干扰。我考虑将脚本设为隐藏状态,但这样系统会看起来像是卡住了一分钟或者完全完成了,但实际上在后台还在进行其他操作。
我有一些脚本,希望用户在运行时不要干扰。我考虑将脚本设为隐藏状态,但这样系统会看起来像是卡住了一分钟或者完全完成了,但实际上在后台还在进行其他操作。
您可以使用此脚本禁用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()
您可以在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
看起来会像这样:
start-process powershell.exe -ArgumentList '-noprofile -command "&{get-content c:\temp\log.txt -Wait}"'
将脚本输出重定向到文件中,这样他们就可以在窗口中看到脚本输出,但是他们在该窗口中的任何操作都不会影响脚本。在脚本结束时,删除日志文件,日志窗口将关闭。