我发现了一种看起来很不错的方法,可以在Windows PowerShell中持久化历史记录。
然而,当我将此粘贴到我的$PROFILE中并重新启动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个字符处: + Register-EngineEvent -SourceIdentifier powershell.exiting -SupportEvent -Action + ~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Register-EngineEvent], ParameterBindingException + FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.RegisterEngineEventCommand