【问题标题】:Script commands to disable quick edit mode禁用快速编辑模式的脚本命令
【发布时间】:2015-09-01 13:16:07
【问题描述】:

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

Enable programmatically the "Quick Edit Mode" in PowerShell

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

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

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

谢谢。

【问题讨论】:

  • 我看到注册表项可能是唯一的方法。
  • 你想要的都是不可能的。
  • @AnsgarWiechers 你能给我更多的信息吗?你怎么会知道这事?例如,这仅仅是因为相关的对象属性没有被暴露,还是在 shell 窗口中 I​​O 是如何完成的一个根本问题?谢谢。
  • 据我所知,$host.UI.RawUI 是在 PowerShell 中即时更改控制台属性的唯一方法。而且该对象根本不提供启用/禁用 QuickEdit 的属性或方法。

标签: powershell


【解决方案1】:

希望不会太晚。

此解决方案基于 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."
    }
}

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

Set-QuickEdit -DisableQuickEdit
Set-QuickEdit 

【讨论】:

  • 谢谢你。是的,为时已晚——我早就解决了这个问题。但是,如果我有时间对此进行测试,我会这样做并标记为答案。看起来很有前途。 OTOH,也许您可​​以在下面评论 AveYo 评论的相关性?
  • 我遇到了同样的问题,您的回答对我来说效果很好。谢谢!
【解决方案2】:

大卫,

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

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

Set-Location HKCU:\Console
New-Item ‘.%SystemRoot%_System32_WindowsPowerShell_v1.0_Powershell.exe’
Set-Location ‘.%SystemRoot%_System32_WindowsPowerShell_v1.0_Powershell.exe’
New-ItemProperty . QuickEdit –Type DWORD –Value 0x00000000
Start-Process Powershell.exe “&{ <#The scrip block you want to run with Quick Edit disabled#> }”
Set-ItemProperty . QuickEdit –Value 0x00000001
Pop-Location

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

Set-Location HKCU:\Console
Set-Location ‘.%SystemRoot%_System32_WindowsPowerShell_v1.0_Powershell.exe’
Set-ItemProperty . QuickEdit –Value 0x00000000
Start-Process Powershell.exe “&{ <#The scrip block you want to run with Quick Edit disabled#> }”
Set-ItemProperty . QuickEdit –Value 0x00000001
Pop-Location

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

-悬崖

【讨论】:

  • 感谢您的回复。是的,这会奏效,但还有其他更简单的方法可以解决我遇到的实际问题。禁用快速编辑本来可以解决我的问题,它可以简单地完成,但由于它不能简单地完成,我可以求助于其他方法来解决我的实际问题。
猜你喜欢
  • 2014-03-15
  • 1970-01-01
  • 2020-07-08
  • 2023-03-19
  • 1970-01-01
  • 2013-08-28
  • 1970-01-01
  • 2017-11-09
  • 2012-11-19
相关资源
最近更新 更多