Windows PowerShell持久化历史记录

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个字符处:
+ Register-EngineEvent -SourceIdentifier powershell.exiting -SupportEvent -Action
+                                                                         ~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Register-EngineEvent], ParameterBindingException
    + FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.RegisterEngineEventCommand

FYI,PowerShell v3 修复了您试图解决的许多问题。$MaximumHistoryCount 默认为 4096,并且默认情况下 Get-History 返回所有项目。 - Keith Hill
$PSVersionTable.PSVersion Major = 3, Minor = 0, Build = -1, Revision = -1 - alexleonard
1
很好。那么你可以跳过修改$MaximumHistoryCount,Get-History将返回所有历史记录(最多4096个项目)。除非你想限制保存的数量。 :-) - Keith Hill
1
这是我的历史记录导出/导入,随意使用 :P 链接 - Andy Arismendi
1
@AndyArismendi,你没有在任何地方声明许可证,所以我无法使用它。 - Demi
相关:有关所有 PowerShell 的历史记录,请参见 *如何在 Windows Server 2016 中查看跨所有 PowerShell 会话的命令历史记录?*。 - Peter Mortensen
3个回答

10

与这个问题密切相关的是"如何使用上/下箭头访问我的历史记录?" PSReadLine 模块解决了这个问题:

Install-Package PSReadLine

然后添加:

Import-Module PSReadLine

到你的$profile


请注意,PSReadLine现在随Windows 10一起发布。 :) - jhclark
Windows PowerShell ISE默认可通过按“上”和“下”键来查看历史记录。 - Michael S.

3

好的,以下是我为您翻译的内容:

嗯,结合了主题启动器的代码和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 }

这个很好用(get-history正常工作),但上下箭头不能在历史记录中移动 - 有什么想法让它工作吗? - mikemaccana
导入 PsReadline 可以通过历史记录进行上下移动。 - Niall Farrington

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()
< p > GetNewClosure() 用于捕获 $historyPath 变量,如果我没记错的话。


1
嘿,感谢你的回答。那是你使用的全部代码吗?我正在关注的博客文章是否在#加载先前的历史记录(如果存在)下做了一些不必要的事情? - alexleonard
1
所以我将文本按照您的方式添加到我的$PROFILE文件中,但是当我退出并重新启动PowerShell时,我仍然无法通过按F7或F8来访问历史记录。 - alexleonard
我只需要添加你在这里发布的四行代码 - 还是我还需要做其他事情?干杯! - alexleonard
这就是全部内容了。当你退出PowerShell(在我的情况下是V3)时,你应该在$Home\Documents\WindowsPowerShell目录中看到一个history-xxxx.clixml文件。 - Keith Hill
我也想指出Keith早期的回答这里更好,因为它可以修剪重复项。 :-) - Michael Sorens
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接