如何让Azure PowerShell记住命令行历史记录?
问题描述:
Azure PowerShell会记住命令行历史记录(几十年后几乎可用的shell),但不幸的是它在退出和重新启动后会很快遭受完全失忆症。所以它不能在会话之间记住它。如何让Azure PowerShell记住命令行历史记录?
有什么办法让Azure PowerShell记住下一个会话中的命令行历史吗?
答
我根据this article电流法:
第1步:制作PowerShell配置文件,see Example 3 here。如果它已存在,请跳到步骤2.
第2步:将其添加到您的配置文件。
$MaximumHistoryCount = 1KB
if (!(Test-Path "$((Get-ChildItem $PROFILE).DirectoryName)\history.csv"))
{ New-Item "$((Get-ChildItem $PROFILE).DirectoryName)\history.csv" -ItemType File -Force
}
function bye
{ Get-History -Count $MaximumHistoryCount | Export-CSV -Path "$((Get-ChildItem $PROFILE).DirectoryName)\history.csv"
exit
}
if (Test-path "$((Get-ChildItem $PROFILE).DirectoryName)\history.csv")
{ Import-CSV "$((Get-ChildItem $PROFILE).DirectoryName)\history.csv" | Add-History
}
register-engineevent PowerShell.Exiting -action { bye }
注:
- 您可能需要使用不同的路径,如原文章的位置。
- 使用
Get-History
和Invoke-History
来运行先前的命令。 - 您也可以使用
#[tab]
,例如:键入#5然后按tab键展开原始命令。 - 一些用户报告,如果他们退出PowerShell中键入
exit
,而不是关闭窗口的历史,才会保存。 - 这是用于PowerShell历史记录,而不是控制台缓冲区。按上/下仅适用于您在当前控制台上输入的命令。
额外的资源和一些替代方法:
- Understanding the Six PowerShell Profiles
- Use PowerShell History to Speed Repetitive Commands
- Saving PowerShell commands across sessions
- PSReadLine,对PowerShell的
- The Search for a Better PowerShell Console Experience 一个bash启发实施的ReadLine 210
- PSReadLine: A Better Line Editing Experience for the PowerShell Console
PSReadLine提供了 “历史的自动保存,包括跨现场会议共享的历史。” PSReadLine除了保存历史记录外,还做了很多工作。另外,如果你想要在历史中上下更有用,最后一个列出了更多的键绑定,使它更有用。
这是一个非常翔实的答案!它让我对我通常回答的华夫饼有点羞愧;) –