2015-06-16 41 views
1

沒有人知道如何從PowerShell腳本中禁用快速編輯模式嗎?這個問題的「答案」是不是一個答案:禁用快速編輯模式的腳本命令

Enable programmatically the "Quick Edit Mode" in PowerShell

(儘管可以設置註冊表設置編程,這樣做會不會影響當前會話)。

我看過了$host,$host.UI$host.UI.RawUI對象,並找不到任何相關的東西。

爲了使事情更清晰一些,我不想更改註冊表。特別是,我不想更改默認行爲。實際上,只有一個腳本,並且實際上只有腳本的一個分支,我需要禁用快速編輯。所以我需要能夠以編程方式禁用它。或者至少,能夠使用命令行選項啓動powershell以禁用快速編輯。

謝謝。

+0

我看到註冊表項可能是唯一的出路。 – Rahul

+0

你想要什麼是不可能的。 –

+0

@AnsgarWiechers你能給我多一點信息嗎?你怎麼會知道這事?例如,這是因爲相關的對象屬性沒有被暴露,或者它是如何在shell窗口中完成IO的根本問題?謝謝。 –

回答

0

大衛,

我不知道你是否已經找到了解決您的問題或沒有,但我在你的帖子出來,而研究辦法有一個PowerShell紙條禁用下的快速編輯選項編輯選項。據我所知,拉胡爾是正確的:「程序化」進行這種改變的唯一方法是通過註冊表。我知道你說過你不想更改註冊表,但有一種方法可以更改註冊表值,啓動新的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 

如果你將這個代碼插入到您想要在禁用「快速編輯」的情況下運行腳本的一部分,您應該獲得所需的結果。希望這有助於。

-Cliff

+0

感謝您的回覆。是的,這將起作用,但還有其他更簡單的方法來解決我所遇到的實際問題。禁用快速編輯可以解決我可以簡單完成的問題,但由於不能簡單地完成,我可以使用其他方法來解決我的實際問題。 –

1

我希望不會太遲。

該解決方案基於C#,但它不使用任何全局設置或註冊表。

解決方案基於此堆棧溢出問題: How to programmatic disable C# Console Application's Quick Edit mode?

[email protected]" 
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 
+0

謝謝你。是的,這太遲了 - 我早已解決了這個問題。但如果我有時間來測試這一點,我會這樣做,並標記爲答案。看起來很有希望。 OTOH,也許你可以評論下面AveYo評論的相關性? –

+0

我有同樣的問題,你的答案對我很好。謝謝! – Kevinoid