gpt4 book ai didi

Windows PowerShell 持久历史记录

转载 作者:可可西里 更新时间:2023-11-01 12:38:29 27 4
gpt4 key购买 nike

我发现了 persisting history in 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 : 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

最佳答案

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

Install-Package PSReadLine

然后添加:

Import-Module PSReadLine

到你的$profile

关于Windows PowerShell 持久历史记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16474973/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com