共用方式為


about_PSConsoleHostReadLine

簡短描述

說明如何建立自定義 PowerShell 在主控台提示字元中讀取輸入的方式。

詳細描述

從 Windows PowerShell 3.0 開始,您可以撰寫名為 PSConsoleHostReadLine 的函式,以覆寫控制台輸入處理的預設方式。

例子

下列範例會啟動 記事本,並從使用者建立的文字檔取得輸入:

function PSConsoleHostReadLine
{
  $inputFile = Join-Path $env:TEMP PSConsoleHostReadLine
  Set-Content $inputFile "PS > "

  # Notepad opens. Enter your command in it, save the file, and then exit.
  notepad $inputFile | Out-Null
  $userInput = Get-Content $inputFile
  $resultingCommand = $userInput.Replace("PS >", "")
  $resultingCommand
}

REMARKS

根據預設,PowerShell 會以所謂的「烹飪模式」從控制台讀取輸入,其中 Windows 控制檯子系統會處理所有按鍵、F7 功能表和其他輸入。 當您按下 Enter 或 Tab 鍵時,PowerShell 會取得您輸入到該點的文字。 在按下 Enter 或 Tab 之前,您無法知道您按下 Ctrl-R、Ctrl-A、Ctrl-E 或任何其他按鍵。在 Windows PowerShell 3.0 中,PSConsoleHostReadLine 函式可解決此問題。 當您在PowerShell控制台主機中定義名為 PSConsoleHostReadline 的函式時,PowerShell 會呼叫該函式,而不是「烹飪模式」輸入機制。

另請參閱