在脚本块中使用开始、处理、结束

默瓦尔德

是否可以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更直观。

谢谢

G42

这是可能的,如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行不通的这使用一个标准的Verboseswitch 参数,允许您通过$true/ $false(或省略)来控制详细输出。

相关:
about_Functions
about_Functions_Advanced

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在WordPress Gutenberg块中使用(开始和结束索引)范围永久应用格式

如何在Ubuntu中使用终端找到文件的开始和结束块号?

具有开始/处理/结束块的函数中的嵌套函数?

在Google表格中使用Google脚本基于图表“开始日期”和“结束日期”的颜色单元

如何在Java脚本中使用子字符串分隔开始文本和结束文本?

在SQL中使用ORDER BY处理数据块

在处理程序中使用块 - Ansible

使%在SQL块中的“开始”或“结束”上工作

Ruby开始和结束块用法

使用Megaparsec使用符号作为开始和结束来解析块注释

grep或sed使用相同的模式匹配开始和结束之间的块

如何使用熊猫将行的开始和结束日期转换为月度块?

我使用什么命令查看文件系统中文件的开始和结束块?

批处理脚本中途结束

在bash脚本中使用jq处理json

脚本开始/结束范围内的grep

从Powershell在脚本块中使用参数启动CMD.exe?

在这种“开始忙碌”的推测处理方案中使用DispatchSemaphore

在 Shiny 中使用 if/else 块进行警告处理

如何知道开始和结束时如何使用Google Apps脚本获得天数

如何使用SQL在Teradata中的表中使用开始日期和结束日期创建日期列?

如何在icCube中的范围维度II中使用流程管理开始和结束日期

如何在mysql中使用星期数获取开始和结束星期的日期?

如何在excel中使用动态开始和结束单元格制作动态范围函数?

**递归地在Java中使用指定的开始和结束索引查找数组中的最大整数

在R的循环中使用开始和结束值进行采样

在 wordforms 问题中使用开始/结束修饰符搜索

如何在jQuery中使用开始和结束元素包装元素?

在Ruby中使用开始值,结束值和步长为浮点值创建数组