PowerShell的清除历史记录不会清除历史记录 [英] PowerShell's Clear-History doesn't clear history

查看:243
本文介绍了PowerShell的清除历史记录不会清除历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我不得不运行一个命令,不幸的是我需要在命令行上键入密码.

Recently I had to run a command that unfortunately required me to type a password right on the command line.

然后,我用清除"清除了我的屏幕,但还想清除命令历史记录,这样就不会在会话历史记录中显示有问题的命令.不幸的是,清除历史 cmdlet似乎并没有实际执行其文档所声称的功能-运行Clear-History似乎对会话历史记录没有任何影响.

Afterwards, I cleared my screen with "Clear", but also wanted to clear the command history so the offending command wouldn't show up in the session history. Unfortunately, the Clear-History cmdlet doesn't seem to actually do what its documentation claims - running Clear-History doesn't seem to have any impact on the session history whatsoever.

我仍然可以在弹出的历史记录菜单中看到以前的命令,并通过按向上键在旧命令之间滚动.这是展示问题的屏幕截图:

I can still see previous commands in the pop-up history menu, and scroll through old commands by pressing the up key. Here's a screengrab demonstrating the problem:

我已经通过 Get-Command进行了验证 Clear-History实际上正在执行预期的内置PowerShell cmdlet.

I've verified with Get-Command that Clear-History is indeed executing the expected built-in PowerShell cmdlet.

我尝试了一些变体,例如清除历史记录-计数10-最新",但都没有显示任何效果.当我指定确切的历史记录ID(例如"Clear-History -id 3")时,会收到如下错误:

I've tried a few variations, such as "Clear-History -count 10 -newest", all failing to show any effect. When I specify an exact history ID, such as "Clear-History -id 3", I receive an error like this:

Clear-History : Cannot locate history for Id 3.

即使我在屏幕上可以看到命令3.

Even if I can see command #3 on the screen.

推荐答案

以补充 CB.的有用答案 JVimes的有用答案:

  • PowerShell自己的历史记录机制(Get-HistoryClear-History)是与主机无关的 ,这就是为什么-有点意外-您还需要分别清除主机的命令历史记录 .

  • PowerShell's own history mechanism (Get-History, Clear-History) is host-independent, which is why - somewhat unexpectedly - you also need to clear the hosts's command history separately.

关于控制台主机自己的历史记录功能:

  • doskey样式的历史记录功能,在PowerShell随附模块PSReadline之前(见下文):

  • doskey-style history feature, before module PSReadline shipped with PowerShell (see below):

  • 没有已保存的历史记录-仅在 current 会话期间保留历史记录.
  • 必须使用
  • Alt + F7 清除控制台的历史记录,而没有(显而易见的)编程方式来执行此操作(在cmd.exe控制台窗口中,您可以使用doskey /reinstall,但在PS中不起作用.
  • CB.的答案向您展示了如何模拟这种键盘组合;请记住:必须在 Clear-History的补充中使用.
  • There is no saved history - a history is kept only for the duration of the current session.
  • Alt+F7 must be used to clear the console's history, with no (obvious) programmatic way to do it (in a cmd.exe console window you could use doskey /reinstall, but that doesn't work in PS).
  • CB.'s answer shows you how to simulate this keyboard combination; remember: this must be used in addition to Clear-History.

PSReadline 模块来了Windows 10上使用PowerShell v5,并且还将随Windows Server 2016一起提供;它用更复杂的功能替换了doskey样式的行编辑和命令历史记录功能;也可以使用 PowerShell库(PSv3和PSv4必须首先安装 PowerShellGet ).

The PSReadline module comes with PowerShell v5 on Windows 10 and will also ship with Windows Server 2016; it replaces the doskey-style line-editing and command-history features with more sophisticated functionality; it is also possible to retrofit older Windows editions / PS versions (>= v3) versions with it, using the PowerShell Gallery (PSv3 and PSv4 must first install PowerShellGet).

  • 命令历史记录现在跨会话保存 ,在文件中
    (Get-PSReadlineOption).HistorySavePath .
  • [Microsoft.PowerShell.PSConsoleReadLine]::ClearHistory()可用于清除当前会话的历史记录(请注意,v1.2 +还支持 Alt + F7 进行交互清除当前历史记录).
    • 注意:使用PSReadline的默认历史保存样式SaveIncrementally,在您致电所有敏感命令已经保存. >,并且将重新出现在下一个会话中.
    • 处理此问题的唯一方法是删除保存的历史文件,如 JVimes的答案所示,但是,总是清除整个历史记录.
    • 如果,您将自己的个人资料设置为每次会话开始时都呼叫Set-PSReadlineOption -HistorySaveStyle SaveAtExit-设置显然不会自己粘"-您应该能够只需调用[Microsoft.PowerShell.PSConsoleReadLine]::ClearHistory()(除了Clear-History之外)即可完成操作,而不必删除已保存的历史记录文件,在这种情况下,您不会丢失以前会话中的已保存历史记录. 但是,从v1.2版本开始,SaveAtExit完全崩溃-根本没有保存任何历史记录;参见 https://github.com/lzybkr/PSReadLine/issues/262
    • Command history is now saved across sessions, in file
      (Get-PSReadlineOption).HistorySavePath.
    • [Microsoft.PowerShell.PSConsoleReadLine]::ClearHistory() can be used to clear the current session's history (note that v1.2+ also supports Alt+F7 for interactive clearing of the current history).
      • CAVEAT: With PSReadline's default history-saving style, SaveIncrementally, any sensitive commands have already been saved by the time to you call [Microsoft.PowerShell.PSConsoleReadLine]::ClearHistory(), and will reappear in the next session.
      • The only way to handle this is to remove the saved-history file, as demonstrated in JVimes's answer which, however, invariably wipes out the entire history.
      • IF you set up your profile to call Set-PSReadlineOption -HistorySaveStyle SaveAtExit every time a session starts - the setting apparenly does NOT "stick" by itself - you should be able to get away with only calling [Microsoft.PowerShell.PSConsoleReadLine]::ClearHistory() (in addition to Clear-History) without also having to delete the saved-history file, in which case you won't lose your saved history from previous sessions. HOWEVER, AS OF v1.2, SaveAtExit is BROKEN ALTOGETHER - no history is saved at all; see https://github.com/lzybkr/PSReadLine/issues/262

      以下高级功能捆绑了清除doskey -style和PSReadline -module PowerShell控制台窗口中所有用于清除命令历史记录的命令(包括PowerShell本身和控制台):

      The following advanced function bundles all commands necessary to clear the command history (both for PowerShell itself and the console), both for doskey-style and PSReadline-module PowerShell console windows:

      注意:

      • 由于(当前)是唯一安全的选项,PSReadline的保存历史记录文件也将被删除,这意味着整个历史记录(包括以前的会话)也将被清除 .

      • Because it's (currently) the only safe option, PSReadline's saved-history file is deleted as well, which means the entire history, including from previous sessions, is cleared.

      因此,默认情况下会显示确认提示.

      Therefore, a confirmation prompt is shown by default.

      <#
      # .SYNOPSIS
      #  Clears the command history, including the saved-to-file history, if applicable.
      #>
      function Clear-SavedHistory {
        [CmdletBinding(ConfirmImpact='High', SupportsShouldProcess)]
        param(    
        )
      
        # Debugging: For testing you can simulate not having PSReadline loaded with
        #            Remove-Module PSReadline -Force
        $havePSReadline = ($null -ne (Get-Module -EA SilentlyContinue PSReadline))
      
        Write-Verbose "PSReadline present: $havePSReadline"
      
        $target = if ($havePSReadline) { "entire command history, including from previous sessions" } else { "command history" } 
      
        if (-not $pscmdlet.ShouldProcess($target))
        {
              return
        }
      
        if ($havePSReadline) {
      
          Clear-Host
      
          # Remove PSReadline's saved-history file.
          if (Test-Path (Get-PSReadlineOption).HistorySavePath) { 
            # Abort, if the file for some reason cannot be removed.
            Remove-Item -EA Stop (Get-PSReadlineOption).HistorySavePath 
            # To be safe, we recreate the file (empty). 
            $null = New-Item -Type File -Path (Get-PSReadlineOption).HistorySavePath
          }
      
          # Clear PowerShell's own history 
          Clear-History
      
          # Clear PSReadline's *session* history.
          # General caveat (doesn't apply here, because we're removing the saved-history file):
          #   * By default (-HistorySaveStyle SaveIncrementally), if you use
          #    [Microsoft.PowerShell.PSConsoleReadLine]::ClearHistory(), any sensitive
          #    commands *have already been saved to the history*, so they'll *reappear in the next session*. 
          #   * Placing `Set-PSReadlineOption -HistorySaveStyle SaveAtExit` in your profile 
          #     SHOULD help that, but as of PSReadline v1.2, this option is BROKEN (saves nothing). 
          [Microsoft.PowerShell.PSConsoleReadLine]::ClearHistory()
      
        } else { # Without PSReadline, we only have a *session* history.
      
          Clear-Host
      
          # Clear the doskey library's buffer, used pre-PSReadline. 
          # !! Unfortunately, this requires sending key combination Alt+F7.
          # Thanks, https://stackoverflow.com/a/13257933/45375
          $null = [system.reflection.assembly]::loadwithpartialname("System.Windows.Forms")
          [System.Windows.Forms.SendKeys]::Sendwait('%{F7 2}')
      
          # Clear PowerShell's own history 
          Clear-History
      
        }
      
      }
      

      这篇关于PowerShell的清除历史记录不会清除历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆