2013-05-10 211 views
8

因此,我發現這個優秀的方法persisting history in Windows PowerShellWindows 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 
+0

僅供參考,PowerShell的V3修復了一些問題,你正在努力解決。 $ MaximumHistoryCount默認爲4096,默認情況下Get-History返回所有項目。 – 2013-05-10 04:33:08

+0

> $ PSVersionTable.PSVersion Major = 3,Minor = 0,Build = -1,Revision = -1 – alexleonard 2013-05-10 04:51:15

+1

很酷。然後,您可以跳過修改$ MaximumHistoryCount並且Get-History將爲您返回所有歷史記錄(以及最多4096個項目)。除非這樣,否則你想限制節省多少。 :-) – 2013-05-10 04:55:02

回答

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()用於捕獲變量IIRC的$historyPath

+1

嘿謝謝你的答案。這是你使用的整個代碼嗎?我是否正在按照#加載上一個歷史記錄下的部分進行不必要的博客文章(如果存在)? – alexleonard 2013-05-10 04:58:27

+0

這取決於你,但隨着時間的推移,歷史將變得非常龐大。我更喜歡有一個簡單的機制來搜索我的持久歷史。 – 2013-05-10 05:42:32

+0

很酷。當我回到辦公室時,我會在明天進行測試。謝謝! – alexleonard 2013-05-10 06:33:19

6

與此問題非常相關的是「如何使用向上/向下箭頭訪問我的歷史記錄?」該PSReadLine模塊解決了這個:

Install-Package PSReadLine 

然後加:

Import-Module PSReadLine 

要將$profile

+0

請注意,PSReadLine現在隨Windows 10一起提供。:) – jhclark 2016-10-21 21:21:10

+0

默認情況下,Windows PowerShell ISE通過按'上升'和'下降'具有歷史記錄。 – 2017-03-24 07:38:49

+0

RSReadline在重新啓動後不存儲我的命令 – aumanjoa 2017-10-13 06:35:25

1

好視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 } 
+0

這很好(獲取歷史作品),但上/下不會移動歷史項目 - 任何想法如何使其工作? – mikemaccana 2016-06-27 20:41:33

+0

導入PsReadline進行上/下移動歷史。 – 2017-08-22 11:11:48