可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
does anyone know how to disable quick edit mode from within a powershell script? This question's "answer" is not an answer:
Enable programmatically the "Quick Edit Mode" in PowerShell
(Though one could set the registry setting programatically, doing so would not affect the current session).
I have looked at the $host
, $host.UI
and $host.UI.RawUI
objects and cannot find anything relevant.
To make things a bit more clear, I do not want to change the registry. In particular, I do not want to change the default behaviour. In fact, there is just one script, and really only one branch of the script, where I need to disable quick-edit. So I need to be able to disable it programatically. Or at the very least, be able to start powershell with command line options to disable quick edit.
Thanks.
回答1:
I hope not too late.
This solution based on C#, but it does not use any global settings or registry.
Solution based on this Stack Overflow question: 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." } }
Now you can disable or enable Quick Edit option by:
Set-QuickEdit -DisableQuickEdit Set-QuickEdit
回答2:
David,
I’m not sure if you’ve found the solution to your problem or not, but I came across your post while researching a way to have a PowerShell scrip disable the Quick Edit option under the Edit Options. As far as I know from my research, Rahul is correct: the only way to make this change ‘programmatically’ is through the registry. I know you said that you don’t want to change the registry, but there is a way to change the registry value, start a new powershell process, execute a script block in that powershell process, then change the registry value back. This is how you would do it:
Assuming the necessary registry value does not exist:
Assuming the necessary registry value does exist:
If you insert this code into the portion of your script that you want to run with Quick Edit disabled, you should get the result you’re looking for. Hopefully this helps.
-Cliff