【问题标题】:How can I see the command history across all PowerShell sessions in Windows Server 2016?如何查看 Windows Server 2016 中所有 PowerShell 会话的命令历史记录?
【发布时间】:2017-10-21 14:10:27
【问题描述】:

在哪里可以查看 Windows Server 2016 中所有会话的完整历史记录?

以下 PowerShell 命令仅包含当前会话中的命令:

Get-History

【问题讨论】:

    标签: powershell windows-server-2016


    【解决方案1】:

    在 PowerShell 中输入以下命令:

    (Get-PSReadlineOption).HistorySavePath
    

    这为您提供了保存所有历史记录的路径。然后在文本编辑器中打开路径。

    尝试cat (Get-PSReadlineOption).HistorySavePath 列出 PowerShell 中的历史记录。

    【讨论】:

    • 实际上,它在全新安装的 Windows Server 2016 中默认存在。
    • 它在我的 Windows 10 Pro (1703) 中。但我不知道它是否默认存在,或者是否有 Visual Studio 或其他类似工具安装的某些模块。不过,在我最近的蓝屏之后,它对我有帮助。
    • 它在 Windows 10 Enterprise 64 位上默认存在。
    • 基本上只使用cat (Get-PSReadlineOption).HistorySavePath 来获取整个历史记录。谢谢@DanielLeach
    • 使用 cat (Get-PSReadlineOption).HistorySavePath | select -Last 10 获取最后 10 行。如果您的历史记录中有许多命令,这很有用。
    【解决方案2】:

    为了从 PowerShell 获取完整历史记录并将输出保存到文件,我使用以下命令:

    Get-Content (Get-PSReadlineOption).HistorySavePath > D:\PowerShellHistory.txt
    

    【讨论】:

      【解决方案3】:

      由于你是在windows上,你也可以用它来打开“记事本”。

      notepad (Get-PSReadlineOption).HistorySavePath
      

      【讨论】:

        【解决方案4】:

        Powershell 库上的 Psreadline 模块 2.1 beta1(仅限 Powershell 7)https://www.powershellgallery.com/packages/PSReadLine/2.1.0-beta1 使用保存的历史记录在命令行上进行智能感知:https://github.com/PowerShell/PSReadLine/issues/1468 它已经开始出现在 Vscode 中。 https://www.reddit.com/r/PowerShell/comments/g33503/completion_on_history_in_vscode/

        同样在 Psreadline 中,您可以使用 f8(在命令行上键入内容后)或 control-R 向后搜索保存的历史记录。 get-psreadlinekeyhandler 列出键绑定。

        get-psreadlinekeyhandler -bound -unbound | ? function -match history
        

        【讨论】:

          【解决方案5】:

          提到了 Windows Server/Enterprise 版本,但作为 Pro(标准零售版)用户 HistorySavePath 也可供我使用。我需要查看最近在旧会话中安装了哪些 python 包,并想在此处为寻找历史中特定事物的人们添加答案。

          # if you like file names and line numbers included in your output
          Select-String "<search pattern>" (Get-PSReadlineOption).HistorySavePath
          
          # if you Just want the text without any of the other information
          Get-Content (Get-PSReadlineOption).HistorySavePath | Select-String "<search pattern>" 
          

          就我而言,我跑了

          Select-String 'pip install' (Get-PSReadlineOption).HistorySavePath
          

          这给了我从我之前的会话中运行的 pip install 命令列表

          ...
          [Path/To/File]:10401:pip install dash
          [Path/To/File]:10824:pip install -r .\requirements.txt
          [Path/To/File]:11296:pip install flask-mysqldb
          [Path/To/File]:11480:pip install Flask-Markdown
          [Path/To/File]:11486:pip install pygments
          [Path/To/File]:11487:pip install py-gfm
          [Path/To/File]:11540:pip install bs4
          

          【讨论】:

            猜你喜欢
            • 2016-02-15
            • 1970-01-01
            • 2013-08-17
            • 2013-02-13
            • 1970-01-01
            • 2016-08-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多