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 char: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 回答
3

好吧,作为topicstarter的代码和Steven Penny稍微改变的答案的组合,这是对我有用的完整代码

################# 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 | Export-Clixml $historyPath
}.GetNewClosure()

# 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 }
于 2014-05-29T11:53:41.750 回答
1

这是我坚持历史记录时使用的代码:

$historyPath = Join-Path (split-path $profile) "history-$(Get-Date -f o).clixml"
Register-EngineEvent -SourceIdentifier powershell.exiting -SupportEvent -Action {
    Get-History | Export-Clixml $historyPath
}.GetNewClosure()

GetNewClosure()用于捕获$historyPath变量 IIRC 。

于 2013-05-10T04:42:50.057 回答