PowerShell 历史记录:如何防止重复命令?

发布于 2024-08-04 23:05:21 字数 409 浏览 6 评论 0原文

背景:

PowerShell 历史记录对我来说更加有用,因为我有办法跨会话保存历史记录。

# Run this every time right before you exit PowerShell
get-history -Count $MaximumHistoryCount | export-clixml $IniFileCmdHistory;

现在,我试图阻止 PowerShell 将重复的命令保存到我的历史记录中。

我尝试使用 Get-Unique,但这不起作用,因为历史记录中的每个命令都是“唯一的”,因为每个命令都有不同的 ID 号。

Background:

PowerShell history is a lot more useful to me now that I have a way to save history across sessions.

# Run this every time right before you exit PowerShell
get-history -Count $MaximumHistoryCount | export-clixml $IniFileCmdHistory;

Now, I am trying to prevent PowerShell from saving duplicate commands to my history.

I tried using Get-Unique, but that doesn't work since every command in the history is "unique", because each one has a different ID number.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

×眷恋的温暖 2024-08-11 23:05:21

Get-Unique 还需要一个排序列表,我假设您可能想要
保留执行顺序。尝试此

Get-History -Count 32767 | Group CommandLine | Foreach {$_.Group[0]} |
Export-Clixml "$home\pshist.xml"

方法此方法使用 Group-Object cmdlet 创建唯一的命令存储桶
然后 Foreach-Object 块只抓取每个存储桶中的第一个项目。

顺便说一句,如果您希望将所有命令保存到历史文件中,我将使用限制值
- 32767 - 除非您设置 $MaximumHistoryCount 的值。

顺便说一句,如果你想在退出时自动保存它,你可以在 2.0 上执行此操作,例如
所以

Register-EngineEvent PowerShell.Exiting {
  Get-History -Count 32767 | Group CommandLine |
  Foreach {$_.Group[0]} | Export-CliXml "$home\pshist.xml" } -SupportEvent

然后要在加载时恢复你所需要的就是

Import-CliXml "$home\pshist.xml" | Add-History

Get-Unique also requires a sorted list and I assume you probably want to
preserve execution order. Try this instead

Get-History -Count 32767 | Group CommandLine | Foreach {$_.Group[0]} |
Export-Clixml "$home\pshist.xml"

This approach uses the Group-Object cmdlet to create unique buckets of commands
and then the Foreach-Object block just grabs the first item in each bucket.

BTW if you want all commands saved to a history file I would use the limit value
- 32767 - unless that is what you set $MaximumHistoryCount to.

BTW if you want to automatically save this on exit you can do this on 2.0 like
so

Register-EngineEvent PowerShell.Exiting {
  Get-History -Count 32767 | Group CommandLine |
  Foreach {$_.Group[0]} | Export-CliXml "$home\pshist.xml" } -SupportEvent

Then to restore upon load all you need is

Import-CliXml "$home\pshist.xml" | Add-History
揽月 2024-08-11 23:05:21

以下命令适用于 Windows 10 中的 PowerShell(在 v.1803 中测试)。该选项记录在此处

Set-PSReadLineOption –HistoryNoDuplicates:$True

在实践中,使用以下命令调用PowerShell(例如保存在快捷方式中)将打开带有无重复历史记录的PowerShell

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -NoExit -Command Set-PSReadLineOption –HistoryNoDuplicates:$True

The following command works for PowerShell in Windows 10 (tested in v.1803). The option is documented here.

Set-PSReadLineOption –HistoryNoDuplicates:$True

In practice, calling PowerShell with the following command (e.g. saved in a shortcut) opens PowerShell with a history without duplicates

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -NoExit -Command Set-PSReadLineOption –HistoryNoDuplicates:$True
ま昔日黯然 2024-08-11 23:05:21

与重复项没有直接关系,但同样有用,我的 $PROFILE 中的这个 AddToHistoryHandler 脚本块将简短的命令保留在我的历史记录之外:

$addToHistoryHandler = {
    Param([string]$line)
    if ($line.Length -le 3) {
        return $false
    }
    if (@("exit","dir","ls","pwd","cd ..").Contains($line.ToLowerInvariant())) {
        return $false
    }
    return $true
}
Set-PSReadlineOption -AddToHistoryHandler $addToHistoryHandler

Not directly related to duplicates, but similarly useful, this AddToHistoryHandler script block in my $PROFILE keeps short and simple commands out of my history:

$addToHistoryHandler = {
    Param([string]$line)
    if ($line.Length -le 3) {
        return $false
    }
    if (@("exit","dir","ls","pwd","cd ..").Contains($line.ToLowerInvariant())) {
        return $false
    }
    return $true
}
Set-PSReadlineOption -AddToHistoryHandler $addToHistoryHandler
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文