12

Windows PowerShell で履歴を永続化する、見栄えのするこの方法を見つけました。

# Persistent History
# Save last 200 history items on exit
$MaximumHistoryCount = 200
$historyPath = Join-Path (split-path $profile) history.clixml

# Hook powershell's exiting event & hide the registration with -supportevent (from nivot.org)
Register-EngineEvent -SourceIdentifier powershell.exiting -SupportEvent -Action {
    Get-History -Count $MaximumHistoryCount | Export-Clixml (Join-Path (split-path $profile) history.clixml)
}

# Load previous history, if it exists
if ((Test-Path $historyPath)) {
    Import-Clixml $historyPath | ? {$count++;$true} | Add-History
    Write-Host -Fore Green "`nLoaded $count history item(s).`n"
}

# Aliases and functions to make it useful
New-Alias -Name i -Value Invoke-History -Description "Invoke history alias"
Rename-Item Alias:\h original_h -Force
function h { Get-History -c  $MaximumHistoryCount }
function hg($arg) { Get-History -c $MaximumHistoryCount | out-string -stream | select-string $arg }

ただし、これを $PROFILE に貼り付けて PowerShell を再起動すると、次のエラーが表示されます。

Register-EngineEvent : パラメータ 'Action' の引数がありません。タイプのパラメーターを指定します
「System.Management.Automation.ScriptBlock」を実行して、もう一度やり直してください。
D:\path\to\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:20 文字:73
+ Register-EngineEvent -SourceIdentifier powershell.exiting -SupportEvent -Action
+ ~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Register-EngineEvent]、ParameterBindingException
    + FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.RegisterEngineEventCommand
4

3 に答える 3

10

この質問に非常に関連しているのは、「上/下矢印を使用して履歴にアクセスするにはどうすればよいですか?」です。PSReadLineモジュールはこれを解決します。

Install-Package PSReadLine

それから加えて:

Import-Module PSReadLine

あなたに$profile

于 2013-10-25T16:10:00.187 に答える