有谁知道如何在 powershell 脚本中禁用快速编辑模式?这个问题的“答案”不是答案:
Enable programmatically the "Quick Edit Mode" in PowerShell
(虽然可以通过编程方式设置注册表设置,但这样做不会影响当前 session )。
我看过$host
, $host.UI
和 $host.UI.RawUI
对象,找不到任何相关的东西。
为了让事情更清楚一点,我不想更改注册表。特别是,我不想更改默认行为。事实上,只有一个脚本,而且实际上只有一个脚本分支,我需要在其中禁用快速编辑。所以我需要能够以编程方式禁用它。或者至少,能够使用命令行选项启动 powershell 以禁用快速编辑。
谢谢。
最佳答案
我希望不会太晚。
此解决方案基于 C#,但不使用任何全局设置或注册表。
基于此堆栈溢出问题的解决方案:
How to programmatic disable C# Console Application's Quick Edit mode?
$QuickEditCodeSnippet=@"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
public static class DisableConsoleQuickEdit
{
const uint ENABLE_QUICK_EDIT = 0x0040;
// STD_INPUT_HANDLE (DWORD): -10 is the standard input device.
const int STD_INPUT_HANDLE = -10;
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll")]
static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
[DllImport("kernel32.dll")]
static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
public static bool SetQuickEdit(bool SetEnabled)
{
IntPtr consoleHandle = GetStdHandle(STD_INPUT_HANDLE);
// get current console mode
uint consoleMode;
if (!GetConsoleMode(consoleHandle, out consoleMode))
{
// ERROR: Unable to get console mode.
return false;
}
// Clear the quick edit bit in the mode flags
if (SetEnabled)
{
consoleMode &= ~ENABLE_QUICK_EDIT;
}
else
{
consoleMode |= ENABLE_QUICK_EDIT;
}
// set the new mode
if (!SetConsoleMode(consoleHandle, consoleMode))
{
// ERROR: Unable to set console mode
return false;
}
return true;
}
}
"@
$QuickEditMode=add-type -TypeDefinition $QuickEditCodeSnippet -Language CSharp
function Set-QuickEdit()
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$false, HelpMessage="This switch will disable Console QuickEdit option")]
[switch]$DisableQuickEdit=$false
)
if([DisableConsoleQuickEdit]::SetQuickEdit($DisableQuickEdit))
{
Write-Output "QuickEdit settings has been updated."
}
else
{
Write-Output "Something went wrong."
}
}
现在您可以通过以下方式禁用或启用快速编辑选项:
Set-QuickEdit -DisableQuickEdit
Set-QuickEdit
关于powershell - 禁用快速编辑模式的脚本命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30872345/