powershell 编写命令脚本以禁用快速编辑模式

7nbnzgx9  于 2023-06-23  发布在  Shell
关注(0)|答案(3)|浏览(276)

有谁知道如何从powershell脚本中禁用快速编辑模式?这个问题的“答案”不是答案:
Enable programmatically the "Quick Edit Mode" in PowerShell
(虽然可以通过编程设置注册表设置,但这样做不会影响当前会话)。
我已经查看了$host$host.UI$host.UI.RawUI对象,但找不到任何相关信息。
为了使事情更清楚一点,我不想改变注册表。特别是,我不想改变默认行为。事实上,只有一个脚本,而且实际上只有脚本的一个分支,我需要禁用快速编辑。所以我需要通过编程来禁用它。或者至少,能够通过命令行选项启动powershell来禁用快速编辑。
谢谢

b4lqfgs4

b4lqfgs41#

希望不会太晚。
该解决方案基于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
zpf6vheq

zpf6vheq2#

大卫
我不确定你是否找到了解决问题的方法,但我在研究一种让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

如果将此代码插入到脚本中要在禁用“快速编辑”的情况下运行的部分,则应获得所需的结果。希望这能帮上忙。

  • 克里夫
holgip5t

holgip5t3#

克里斯和克利福德的回答对我不起作用。我有一个powershell脚本,我想运行(一个长时间运行的脚本,不应该被鼠标点击中断)。在我的示例中,我使用DoWork.ps1作为长时间运行的脚本。
我创建了一个批处理文件来设置reg-key值:

RunScript.bat

@echo off
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%DoWork.ps1

: Disable QuickEdit
PowerShell -Command "Set-ItemProperty -Path 'HKCU:\Console' -Name 'QuickEdit' -Value 0x00000000" 
PowerShell -Command "Set-ItemProperty -Path 'HKCU:\Console\%%SystemRoot%%_System32_WindowsPowerShell_v1.0_Powershell.exe' -Name 'QuickEdit' -Value 0x00000000" 

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%PowerShellScriptPath%""' -Verb RunAs}"

: Re-enable QuickEdit 
PowerShell -Command "Set-ItemProperty -Path 'HKCU:\Console' -Name 'QuickEdit' -Value 0x00000001" 
PowerShell -Command "Set-ItemProperty -Path 'HKCU:\Console\%%SystemRoot%%_System32_WindowsPowerShell_v1.0_Powershell.exe' -Name 'QuickEdit' -Value 0x00000001"

DoWork.ps1**

$i = 0
while ($i -lt 1000)
{
    Write-Output "i:$i"
    $i++
    sleep(1)
}

将RunScript.bat和DoWork.ps1放在同一个文件夹中,然后双击/调用RunScript.bat以启动DoWork.ps1。请注意,Powershell“属性”设置中的QuickEdit复选框已禁用。
HKCU:\Console\QuickEdit设置为0将仅禁用命令提示符的QuickEdit“默认”设置,将HKCU:\Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe\QuickEdit设置为0将禁用RunScript.bat文件调用的Powershell窗口的QuickEdit“属性”设置。
AFAIK,我不认为QuickEdit“默认”设置会影响任何东西。但重要的是:

  1. HKCU:\Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe\QuickEdit必须设置为0。
    1.您希望运行的powershell脚本必须从批处理文件中调用。即使您将两个注册表项都设置为0,QuickEdit“属性”复选框仍将选中(启用)。

相关问题