【问题标题】:Can't run PowerShell script in Azure DevOps Pipeline 'parse error'无法在 Azure DevOps Pipeline“解析错误”中运行 PowerShell 脚本
【发布时间】:2021-03-16 15:28:09
【问题描述】:

所以,我是 Azure Devops 的新手(但不是 Azure 或 PowerShell)。我的管道中有两个脚本作为任务,第一个运行完美(注意:没有 az module 命令)。第二个失败(它有一个 Az 调用)。我在管道中遇到的错误是:

ParserError: /home/vsts/work/_temp/3f7b3ce1-6afb-46f3-b00d-1efe35fbac71.ps1:5
Line |
   5 |  } else {
     |  ~
     | Unexpected token '}' in expression or statement.

事情是这样的......我的脚本中没有'} else {',或者至少,我删除了它并得到了同样的错误。

所以无论是什么原因造成的,都比我的脚本更根本。我认为 '/home/vsts/work/_temp/3f7b3ce1-6afb-46f3-b00d-1efe35fbac71.ps1' 是我复制到远程系统的脚本,但事实并非如此。

有什么办法,我可以找出那个文件是什么?

任务是设置了“pwsh: true”的“PowerShell@2”。 谢谢!

YAML:

trigger:
- main

pool:
  vmImage: ubuntu-latest

steps:
- task: AzureResourceManagerTemplateDeployment@3
  inputs:
    deploymentScope: 'Resource Group'
    azureResourceManagerConnection: 'Pay-As-You-Go(<guid>)'
    subscriptionId: '<guid>'
    action: 'Create Or Update Resource Group'
    resourceGroupName: 'project-Test'
    location: 'East US 2'
    templateLocation: 'Linked artifact'
    csmFile: 'deploy.template.json'
    csmParametersFile: 'deploy.parameters.json'
    deploymentMode: 'Incremental'
    deploymentOutputs: 'DeploymentOutput'

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: $(System.DefaultWorkingDirectory)/Get-ArmDeploymentOutput.ps1 -ArmOutputString '$(DeploymentOutput)' -MakeOutput -ErrorAction Stop
    pwsh: true
  displayName: Get-ArmDeploymentOutput

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: $(System.DefaultWorkingDirectory)/Set-AzWebAppIPRestriction.ps1 -Priority 100 -Action 'Allow' -WebAppId '$(WebAppId)' -PipId '$(gwpipId)'' -ErrorAction Stop
    pwsh: true
  displayName: Set-AzWebAppIPRestriction

【问题讨论】:

  • 您需要提供 YAML,&lt;guid&gt;.ps1 文件名只是为您的内联脚本创建的文件。
  • 我已经添加了 YAML。
  • 那些不是内联脚本。那些是文件。尝试将targetType 更改为filePath。如果遇到路径错误,请插入 - pwsh: Get-ChildItem $(System.DefaultWorkingDirectory) -Recurse 以获取目录输出并帮助解决路径问题。

标签: azure azure-devops azure-devops-pipelines


【解决方案1】:

我发现在您的第二个 powershell 任务的脚本中,-PipId '$(gwpipId)'' 中有一个额外的 '。也许它导致了错误。

script: $(System.DefaultWorkingDirectory)/Set-AzWebAppIPRestriction.ps1 -Priority 100 -Action 'Allow' -WebAppId '$(WebAppId)' -PipId '$(gwpipId)'' -ErrorAction Stop

因为您在 .ps1 文件中运行脚本。您应该将powershell tasktargetType 参数更改为filePath。并在filePath 参数中设置.ps1 文件。见下例:

steps:
- task: PowerShell@2
  displayName: 'PowerShell Script'
  inputs:
    targetType: filePath
    filePath: '$(System.DefaultWorkingDirectory)/Set-AzWebAppIPRestriction.ps1'
    arguments: '-Priority 100 -Action "Allow" -WebAppId "$(WebAppId)" -PipId "$(gwpipId)" -ErrorAction Stop'
    pwsh: true

如果您在脚本中使用 azure powershell 模块。您可以使用 Azure powershell task 代替 powershell 任务..

【讨论】:

    猜你喜欢
    • 2019-03-07
    • 2023-03-16
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 2021-12-02
    • 1970-01-01
    相关资源
    最近更新 更多