【问题标题】:Get-History inside a powershell script filePowerShell脚本文件中的Get-History
【发布时间】:2020-06-04 22:30:41
【问题描述】:

我想知道我的 powershell 脚本文件中最后执行的命令。

例如我的脚本文件看起来像

echo "hello"
$x=(Get-History)[-1]
echo $x

实际上我有一个更大的脚本文件

这应该输出“echo 'hello'”。但它输出了我在 powershell 终端中执行的最后一个命令。

有没有办法在 powershell 脚本文件中获取最后执行的命令。 我实际上想使用历史记录的 StartExecutionTime 属性。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    PowerShell 本身并不支持这一点,但如果您需要跟踪多个命令以查看脚本中哪个最慢,您可以使用如下构造:

    $commands = @(
        'write-host 123',
        'write-host 234',
        'set-location c:\git',
        'write-host 123',
        'set-location c:\temp'    
    )
    
    forEach ($c in $commands){
        $stopWatch = Measure-command -Expression {Invoke-Expression $c}
        Write-host "The last command [$c] was executed in [$($stopWatch.TotalMilliseconds)]"
    }
    
    

    这将一一执行数组$commands 中的所有命令并在它们上运行Measure-Command,这将返回一个丰富的TimeSpan 对象,其中包含您要使用的TotalMilliseconds 字段。输出如下:

    The last command [write-host 123] was executed in [2.3979]
    The last command [write-host 234] was executed in [0.031]
    The last command [set-location c:\git] was executed in [0.0236]
    The last command [write-host 123] was executed in [0.021]
    The last command [set-location c:\temp] was executed in [0.0171]
    

    代码的 sn-p 也可以修改为与脚本一起使用,所以如果我们想象我们有一个这样的脚本:

    #myCoolScript.ps1
    write-host 123
    start-sleep -Seconds 2
    write-host 234
    start-sleep -Seconds 1
    set-location c:\git
    write-host 123
    set-location c:\temp    
    

    您可以像这样修改代码以测量每一行:

    
    $commands = get-content .\MyCoolScript.ps1
    forEach ($c in $commands){
        $stopWatch = Measure-command -Expression {invoke-expression $c}
        Write-host "The last command [$c] was executed in [$($stopWatch.TotalMilliseconds)]"
    }
    

    这会给出这个输出:

    The last command [write-host 123] was executed in [10.1866]
    The last command [start-sleep -Seconds 2] was executed in [2000.178]
    The last command [write-host 234] was executed in [1.1301]
    The last command [start-sleep -Seconds 1] was executed in [999.5883]
    The last command [set-location c:\git] was executed in [0.5302]
    The last command [write-host 123] was executed in [0.9388]
    The last command [set-location c:\temp    ] was executed in [0.3985]
    

    【讨论】:

    • 读取一系列命令的执行时间的非常酷的技巧。谢谢
    【解决方案2】:

    Get-History 显示来自 PSReadLine 的交互式 PowerShell 会话的命令历史记录。但是当您将其作为脚本执行时,您将看不到这一点。

    如果您想在脚本中获取最后一个运行命令,您应该能够使用自动变量$$,它将显示最后一个命令的结果。

    还有用于调试的Get-PSCallStack,它显示了正在运行的会话中的调用堆栈。

    【讨论】:

      【解决方案3】:

      它对我有用。 “你好”是第一位的:

      PS C:\Users\admin\foo> .\test
      hello
      
        Id CommandLine
        -- -----------
         3 emacs test.ps1
      
      
      

      开始执行时间:

      PS C:\Users\admin\foo> (get-history)[-1] | % startexecutiontime
      
      Thursday, June 4, 2020 10:26:14 AM
      

      你是说你想要回声的开始时间吗?

      $a = get-date
      echo "hello"
      $a
      
      Thursday, June 4, 2020 10:30:19 AM
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-21
        相关资源
        最近更新 更多