因此,我發現這個優秀的方法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
僅供參考,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