about_PSConsoleHostReadLine
適用於: Windows PowerShell 4.0, Windows PowerShell 5.0
主題
about_PSConsoleHostReadLine
簡短描述
說明如何自訂 Windows PowerShell 在主控台提示中讀取輸入的方式。
詳細描述
從 Windows PowerShell V3 開始,您可以寫入名為 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
}
備註
Windows PowerShell 預設會以所謂的「修正模式」從主控台讀取輸入,在此模式下,Windows 主控台子系統會處理所有所按字符、F7 功能表和其他輸入。當您按下 Enter 鍵或 Tab 鍵時,Windows PowerShell 會取得您已輸入至該點的文字。在按下 Enter 鍵或 Tab 鍵之前,其無法得知您已按下 Ctrl-R、Ctrl-A、Ctrl-E 或任何其他按鍵。在 Windows PowerShell 第 3 版中,PSConsoleHostReadLine 函式解決了這個問題。當您在 Windows PowerShell 主控台主機中定義名為 PSConsoleHostReadline 的函式時,Windows PowerShell 會呼叫該函式來代替「修正模式」輸入機制。
另請參閱
about_Prompts