Windows PowerShell持续历史记录
因此,我发现这个优秀的方法persisting history in Windows PowerShell。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 }
然而,当我粘贴到我的$配置文件,重新启动PowerShell中,我得到了 以下错误
Register-EngineEvent : Missing an argument for parameter 'Action'. Specify a parameter of type 'System.Management.Automation.ScriptBlock' and try again. At 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
这是我使用的时候我坚持我的历史使用的代码
$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()
用于捕获变量IIRC的$historyPath
。
嘿谢谢你的答案。这是你使用的整个代码吗?我是否正在按照#加载上一个历史记录下的部分进行不必要的博客文章(如果存在)? – alexleonard 2013-05-10 04:58:27
这取决于你,但随着时间的推移,历史将变得非常庞大。我更喜欢有一个简单的机制来搜索我的持久历史。 – 2013-05-10 05:42:32
很酷。当我回到办公室时,我会在明天进行测试。谢谢! – alexleonard 2013-05-10 06:33:19
与此问题非常相关的是“如何使用向上/向下箭头访问我的历史记录?”该PSReadLine模块解决了这个:
Install-Package PSReadLine
然后加:
Import-Module PSReadLine
要将$profile
。
好视topicstarter代码的组合和微微一变答案由史蒂芬便士,这是代码的完整片这是为我工作
################# 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 }
这很好(获取历史作品),但上/下不会移动历史项目 - 任何想法如何使其工作? – mikemaccana 2016-06-27 20:41:33
导入PsReadline进行上/下移动历史。 – 2017-08-22 11:11:48
仅供参考,PowerShell的V3修复了一些问题,你正在努力解决。 $ MaximumHistoryCount默认为4096,默认情况下Get-History返回所有项目。 – 2013-05-10 04:33:08
> $ PSVersionTable.PSVersion Major = 3,Minor = 0,Build = -1,Revision = -1 – alexleonard 2013-05-10 04:51:15
很酷。然后,您可以跳过修改$ MaximumHistoryCount并且Get-History将为您返回所有历史记录(以及最多4096个项目)。除非这样,否则你想限制节省多少。 :-) – 2013-05-10 04:55:02