Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在powershell脚本文件中获取历史记录_Powershell - Fatal编程技术网

在powershell脚本文件中获取历史记录

在powershell脚本文件中获取历史记录,powershell,Powershell,我想知道powershell脚本文件中最后执行的命令 例如,我的脚本文件如下所示 echo "hello" $x=(Get-History)[-1] echo $x 在现实中,我有一个更大的脚本文件 这将向我输出“echo'hello'”。但它向我输出在powershell终端中执行的最后一个命令 是否有方法获取powershell脚本文件中最后执行的命令。 实际上,我想使用历史记录的StartExecutionTime属性。获取历史记录显示PSReadLine中交互式PowerShell会话

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

例如,我的脚本文件如下所示

echo "hello"
$x=(Get-History)[-1]
echo $x
在现实中,我有一个更大的脚本文件

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

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

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

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


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

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
对象,该对象包含您希望使用的
totalmillizes
字段。输出如下:

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]
#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    
代码片段也可以修改为与脚本一起使用,因此如果我们想象我们有这样一个脚本:

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]
#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]
它对我有用。“你好”是第一个:

PS C:\Users\admin\foo> .\test
hello

  Id CommandLine
  -- -----------
   3 emacs test.ps1


StartExecutionTime:

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


读取一系列命令的执行时间的非常酷的技巧。谢谢