如果条件,如何编写Inline PowerShell@2任务脚本?

发布于 2025-02-01 20:28:45 字数 486 浏览 7 评论 0原文

我有一个具有这样的任务的YAML管道:

- task: PowerShell@2
      displayName: Restart my service
      inputs:
        targetType: 'inline'
        script: |
          $service = Get-Service | Where-Object{$_.DisplayName -like "My Service"}
          if($service)
          {
            Write-host $service.Name " service is restarting."
            Restart-Service $service
          }

但是,最后一个“}”被标记为“无法读取块映射条目”的错误。我认为它试图将某些脚本读为管道内容,而不会将其视为脚本的一部分。我如何将其作为整个内联脚本接受?

I've got a yaml pipeline with a task like this:

- task: PowerShell@2
      displayName: Restart my service
      inputs:
        targetType: 'inline'
        script: |
          $service = Get-Service | Where-Object{$_.DisplayName -like "My Service"}
          if($service)
          {
            Write-host $service.Name " service is restarting."
            Restart-Service $service
          }

However, the last '}' is being flagged as an error with "cannot read a block mapping entry". I assume that it's trying to read some of the script as pipeline stuff and doesn't consider it part of the script. How do I get this to be accepted as the whole inline script?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

涫野音 2025-02-08 20:28:45

看起来这是一个缩进的问题。我知道它可以处理以下内容:

if($service) {
  Write-host $service.Name " service is restarting."
  Restart-Service $service
  }

Looks like this was an indenting issue. I got it to work with the following:

if($service) {
  Write-host $service.Name " service is restarting."
  Restart-Service $service
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文