Azure Devops Pipeline - 如何将变量传递给 Powershell 函数

Azure Devops Pipeline - How to pass variable into Powershell function

我有带有 powershell 任务的 Yaml 管道:

       - task: PowerShell@2
        inputs:
          targetType: filePath
          filePath: $(System.DefaultWorkingDirectory)\folder\script.ps1
          arguments: > 
            -SP_TenantId "$(SP_TenantId)"
            -ProjectName "${{parameters.ProjectName}}"

脚本。ps1 有一个 PS 函数,它以来自 Yaml 参数的强制参数开始

    param (
    [Parameter(Mandatory = $true)][string]$SP_TenantId,
    [Parameter(Mandatory = $true)][string]$ProjectName,

)

在 运行 管道之后,我收到一条错误消息,提示缺少环境:

Get-GraphToken : Cannot process command because of one or more missing mandatory parameters: SP_TenantId 

当我将您的代码片段与我的管道进行比较时,我根据提供的论点发现了一些差异:您可以这样尝试吗?

inputs:
  targetType: filePath
  filePath: $(System.DefaultWorkingDirectory)\folder\script.ps1
  arguments: '-SP_TenantId:"$(SP_TenantId)" -ProjectName:"${{parameters.ProjectName}}"'

Azure Devops Pipeline - How to pass variable into Powershell function

您可以尝试删除 script.ps1:

中的 second comma
param (
[Parameter(Mandatory = $true)][string]$SP_TenantId,
[Parameter(Mandatory = $true)][string]$ProjectName

)

我测试了一下,在我这边效果很好。请查收。