【发布时间】:2019-04-17 23:14:33
【问题描述】:
我想编写一个函数,它接受一个脚本块作为参数,并在调用它的范围内执行该脚本块。
Measure-Command 是我想要的行为示例。脚本块在与 Measure-Command 本身相同的范围内运行。如果脚本块引用此范围内的变量,则脚本可以更改它。
附加的是增加 $a 变量的示例脚本块。当被 Measure-Command 调用时,变量会递增。但是当被 Wrapper 函数调用时,该变量不会增加 - 除非我对 Wrapper 函数的调用和 Wrapper 函数本身都使用点源进行点源。
function Wrapper1
{
param( $scriptBlock )
$startTime = Get-Date
Write-Output ( "{0:HH:mm:ss} Start script" -f $startTime )
& $scriptBlock
$endTime = Get-Date
Write-Output ( "{0:HH:mm:ss} End script - {1:c} seconds elapsed" -f $endTime, ( $endTime - $StartTime ) )
}
function Wrapper2
{
param( $scriptBlock )
$startTime = Get-Date
Write-Output ( "{0:HH:mm:ss} Start script" -f $startTime )
. $scriptBlock
$endTime = Get-Date
Write-Output ( "{0:HH:mm:ss} End script - {1:c} seconds elapsed" -f $endTime, ( $endTime - $StartTime ) )
}
$a = 1
Write-Output "Initial state: `$a = $a"
Measure-Command { $a++ } | Out-Null
Write-Output "Measure-Command results: `$a = $a"
Wrapper1 { $a++ }
Write-Output "Wrapper1 results: `$a = $a"
. Wrapper1 { $a++ }
Write-Output "dot-sourced Wrapper1 results: `$a = $a"
Wrapper2 { $a++ }
Write-Output "Wrapper2 results: `$a = $a"
. Wrapper2 { $a++ }
Write-Output "dot-sourced Wrapper2 results: `$a = $a"
运行这段代码的结果是:
Initial state: $a = 1
Measure-Command results: $a = 2
13:44:49 Start script
13:44:49 End script - 00:00:00 seconds elapsed
Wrapper1 results: $a = 2
13:44:49 Start script
13:44:49 End script - 00:00:00.0157407 seconds elapsed
dot-sourced Wrapper1 results: $a = 2
13:44:49 Start script
13:44:49 End script - 00:00:00 seconds elapsed
Wrapper2 results: $a = 2
13:44:49 Start script
13:44:49 End script - 00:00:00 seconds elapsed
dot-sourced Wrapper2 results: $a = 3
虽然最后一个选项有效,但我想避免调用 Wrapper2 的点源语法。这可能吗? Measure-Command 不使用 dot-source 语法,所以它似乎是可能的。
【问题讨论】:
-
将包装函数放入模块中。
标签: function powershell scope