用于禁用快速编辑模式的脚本命令

Dav*_*osh 6 powershell

有谁知道如何从 powershell 脚本中禁用快速编辑模式?这个问题的“答案”不是答案:

在 PowerShell 中以编程方式启用“快速编辑模式”

(虽然可以通过编程方式设置注册表设置,但这样做不会影响当前会话)。

我查看了$host$host.UI$host.UI.RawUI对象,但找不到任何相关内容。

为了让事情更清楚一点,我不想更改注册表。特别是,我不想更改默认行为。事实上,只有一个脚本,而且实际上只有一个脚本分支,我需要在其中禁用快速编辑。所以我需要能够以编程方式禁用它。或者至少,能够使用命令行选项启动 powershell 以禁用快速编辑。

谢谢。

Kri*_*isz 6

我希望还不算太晚。

该解决方案基于 C#,但它不使用任何全局设置或注册表。

基于此 Stack Overflow 问题的解决方案: 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."
    }
}
Run Code Online (Sandbox Code Playgroud)

现在您可以通过以下方式禁用或启用快速编辑选项:

Set-QuickEdit -DisableQuickEdit
Set-QuickEdit 
Run Code Online (Sandbox Code Playgroud)


小智 1

大卫,

\n\n

我\xe2\x80\x99m不确定你\xe2\x80\x99是否找到了问题的解决方案,但我在研究一种让PowerShell脚本禁用编辑选项下的快速编辑选项的方法时看到了你的帖子。据我的研究所知,拉胡尔是正确的:以编程方式进行此更改 \xe2\x80\x98\xe2\x80\x99 的唯一方法是通过注册表。我知道你说你不想更改注册表,但是有一种方法可以更改注册表值,启动一个新的powershell进程,在该powershell进程中执行一个脚本块,然后更改注册表值后退。您将这样做:

\n\n

假设必要的注册表值不存在:

\n\n
Set-Location HKCU:\\Console\nNew-Item \xe2\x80\x98.%SystemRoot%_System32_WindowsPowerShell_v1.0_Powershell.exe\xe2\x80\x99\nSet-Location \xe2\x80\x98.%SystemRoot%_System32_WindowsPowerShell_v1.0_Powershell.exe\xe2\x80\x99\nNew-ItemProperty . QuickEdit \xe2\x80\x93Type DWORD \xe2\x80\x93Value 0x00000000\nStart-Process Powershell.exe \xe2\x80\x9c&{ <#The scrip block you want to run with Quick Edit disabled#> }\xe2\x80\x9d\nSet-ItemProperty . QuickEdit \xe2\x80\x93Value 0x00000001\nPop-Location\n
Run Code Online (Sandbox Code Playgroud)\n\n

假设必要的注册表值确实存在:

\n\n
Set-Location HKCU:\\Console\nSet-Location \xe2\x80\x98.%SystemRoot%_System32_WindowsPowerShell_v1.0_Powershell.exe\xe2\x80\x99\nSet-ItemProperty . QuickEdit \xe2\x80\x93Value 0x00000000\nStart-Process Powershell.exe \xe2\x80\x9c&{ <#The scrip block you want to run with Quick Edit disabled#> }\xe2\x80\x9d\nSet-ItemProperty . QuickEdit \xe2\x80\x93Value 0x00000001\nPop-Location\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果将此代码插入到要在禁用快速编辑的情况下运行的脚本部分,您应该会得到您\xe2\x80\x99 正在寻找的结果。希望这有帮助。

\n\n

-悬崖

\n