当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] Measure-Command (Microsoft.PowerShell.Utility)

作者:精品下载站 日期:2024-12-14 02:05:25 浏览:15 分类:玩电脑

Measure-Command (Microsoft.PowerShell.Utility)


Measure-Command

模块 :Microsoft.PowerShell.Utility

测量运行脚本块和 cmdlet 所需的时间。

句法

Measure-Command
       [-InputObject <PSObject>]
       [-Expression] <ScriptBlock>
       [<CommonParameters>]

描述

Measure-Command cmdlet 在内部运行脚本块或 cmdlet,对操作的执行进行计时,并返回执行时间。

笔记

Measure-Command 运行的脚本块在当前作用域中运行,而不是在子作用域中运行。

示例

示例 1:测量命令

此示例测量运行获取 Windows PowerShell 事件日志中的事件的 Get-EventLog 命令所需的时间。

Measure-Command { Get-EventLog "windows powershell" }

示例 2:比较 Measure-Command 的两个输出

第一个命令测量处理递归 Get-ChildItem 命令所需的时间,该命令使用 Path 参数仅获取 .txt 文件C:\Windows 目录及其子目录。

第二个命令测量处理使用特定于提供程序的 Filter 参数的递归 Get-ChildItem 命令所需的时间。

这些命令显示了在 PowerShell 命令中使用特定于提供程序的筛选器的价值。

Measure-Command { Get-ChildItem -Path C:\Windows\*.txt -Recurse }

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 8
Milliseconds      : 618
Ticks             : 86182763
TotalDays         : 9.9748568287037E-05
TotalHours        : 0.00239396563888889
TotalMinutes      : 0.143637938333333
TotalSeconds      : 8.6182763
TotalMilliseconds : 8618.2763

Measure-Command {Get-ChildItem C:\Windows -Filter "*.txt" -Recurse}

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 1
Milliseconds      : 140
Ticks             : 11409189
TotalDays         : 1.32050798611111E-05
TotalHours        : 0.000316921916666667
TotalMinutes      : 0.019015315
TotalSeconds      : 1.1409189
TotalMilliseconds : 1140.9189

示例 3:通过管道输入到 Measure-Command

通过管道传输到 Measure-Command 的对象可用于传递给 Expression 参数的脚本块。对于管道上的每个对象,脚本块都会执行一次。

# Perform a simple operation to demonstrate the InputObject parameter
# Note that no output is displayed.
10, 20, 50 | Measure-Command -Expression { for ($i=0; $i -lt $_; $i++) {$i} }

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 12
Ticks             : 122672
TotalDays         : 1.41981481481481E-07
TotalHours        : 3.40755555555556E-06
TotalMinutes      : 0.000204453333333333
TotalSeconds      : 0.0122672
TotalMilliseconds : 12.2672

示例 4:显示测量命令的输出

要在 Measure-Command 中显示表达式的输出,您可以使用管道到 Out-Default

# Perform the same operation as above adding Out-Default to every execution.
# This will show that the ScriptBlock is in fact executing for every item.
10, 20, 50 | Measure-Command -Expression {for ($i=0; $i -lt $_; $i++) {$i}; "$($_)" | Out-Default }

10
20
50


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 11
Ticks             : 113745
TotalDays         : 1.31649305555556E-07
TotalHours        : 3.15958333333333E-06
TotalMinutes      : 0.000189575
TotalSeconds      : 0.0113745
TotalMilliseconds : 11.3745

示例 5:测量子作用域中的执行情况

Measure-Command 在当前范围内运行脚本块,因此脚本块可以修改当前范围内的值。为了避免更改当前作用域,必须将脚本块括在大括号 ({}) 中,并使用调用运算符 (&) 在子作用域中执行该块。

$foo = 'Value 1'
$null = Measure-Command { $foo = 'Value 2' }
$foo
$null = Measure-Command { & { $foo = 'Value 3' } }
$foo

Value 2
Value 2

有关调用运算符的更多信息,请参阅 about_Operators。

参数

-Expression

指定正在计时的表达式。将表达式括在大括号 ({}) 中。

类型 :

ScriptBlock

位置:

0

默认值:

None

必需的:

True

接受管道输入:

False

接受通配符:

False

-InputObject

绑定到 InputObject 参数的对象是传递给 Expression 参数的脚本块的可选输入。在脚本块内,$_ 可用于引用管道中的当前对象。

类型 :

PS对象

位置:

命名

默认值:

None

必需的:

False

接受管道输入:

True

接受通配符:

False

输入

PSObject

您可以通过管道将对象传递给此 cmdlet。

输出

时间跨度

此 cmdlet 返回表示结果的时间跨度对象。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯