是否可以Begin, Process, End
在脚本块中使用高级功能特性?
例如,我有以下脚本块:
$startStopService = {
Param(
[bool] $startService)
if ($startService){
...
Start-Service "My-Service"
}
else {
Stop-Service "My-Service"
}
}
由于我希望能够控制脚本块的详细输出,我想将块更改为:
$startStopService = {
Param(
[bool] $startService)
Begin {
$oldPreference = $VerbosePreference
$VerbosePreference = $Using:VerbosePreference
}
Process {
if ($startService){
...
Start-Service "My-Service"
}
else {
Stop-Service "My-Service"
}
}
End {
# Restore the old preference
$VerbosePreference = $oldPreference
}
}
Begin, Process, End
尽管脚本块不是 cmdlet,但是否可以在此处使用?我只是希望VerbosePreference
恢复到旧值,无论是否发生错误。当然,我可以使用try{}finally{}
作为替代,但我发现这Begin, Process, End
更直观。
谢谢
这是可能的,如about_script_blocks 中所述:
与函数一样,脚本块可以包含 DynamicParam、Begin、Process 和 End 关键字。有关更多信息,请参阅 about_Functions 和 about_Functions_Advanced。
为了测试这一点,我修改了你的脚本块并运行了这个:
$startStopService = {
Param(
# a bool needs $true or $false passed AFAIK
# A switch is $true if specified, $false if not included
[switch] $startService
)
Begin {
$oldPreference = $VerbosePreference
Write-Output "Setting VerbosePreference to Continue"
# $Using:VerbosePreference gave me an error
$VerbosePreference = "Continue"
}
Process {
if ($startService){
Write-Verbose "Service was started"
}
else {
Write-Verbose "Service was not started"
}
}
End {
# Restore the old preference
Write-Output "Setting VerbosePreference back to $oldPreference"
$VerbosePreference = $oldPreference
}
}
Write-Verbose "This message will not print if VerbosePreference is the default SilentlyContinue"
. $startStopService -startService
Write-Verbose "This message will not print if VerbosePreference is the default SilentlyContinue"
你追求什么功能?如果您想在运行$VerbosePreference
脚本块时打印详细消息但不更改脚本的其余部分,请考虑使用[CmdletBinding()]
和-Verbose
标志:
$startStopService = {
[CmdLetBinding()]
Param(
[switch] $startService
)
Write-Verbose "This is a verbose message"
}
Write-Verbose "This message will not print if VerbosePreference is the default SilentlyContinue"
. $startStopService -verbose
Write-Verbose "This message will not print if VerbosePreference is the default SilentlyContinue"
编辑 - 调用命令
在您发表评论后,我研究了Invoke-Command
. 并发现了很多行不通的事情。
我认为对您最有用的简短版本:您可以$VerbosePreference = "Continue"
在 scriptblock 内声明,这将仅限于 scriptblock 的范围。之后不需要改回来。
$startStopService = {
[CmdLetBinding()]
Param(
[parameter(Position=0)]
[switch]$startStopService,
[parameter(Position=1)]
[switch]$Verbose
)
if($Verbose){
$VerbosePreference = "Continue"
}
Write-Verbose "This is a verbose message"
}
Write-output "VerbosePreference: $VerbosePreference"
Write-Verbose "This message will not print if VerbosePreference is the default SilentlyContinue"
Invoke-Command -Scriptblock $startStopService -ArgumentList ($true,$true)
Write-output "VerbosePreference: $VerbosePreference"
Write-Verbose "This message will not print if VerbosePreference is the default SilentlyContinue"
尝试将-Verbose
开关CommonParameter传递给Invoke-Command
是行不通的。这使用一个标准的Verbose
switch 参数,允许您通过$true
/ $false
(或省略)来控制详细输出。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句