1

I have Yaml pipeline with powershell task:

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

The script.ps1 has a PS Function which starts with mandatory parameters from Yaml arguments

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

)

After running the pipeline I've got an error telling that env are missing:

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

3 Answers 3

1

Azure Devops Pipeline - How to pass variable into Powershell function

You could try to remove the second comma in the script.ps1:

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

)

I test it, it works fine on my side. Please check it.

Sign up to request clarification or add additional context in comments.

Comments

0

when I compare your snippet with a pipeline of mine, I've recognized some differences according to providing the arguments: can you try it that way?

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

Comments

0

First, ensure your Azure pipeline has the correct variable group under which the particular variable is.

Passing variable as part of arguments (pipeline to PowerShell script)

task: AzureCLI@2 
displayName: My PowerShell script 
inputs: 
azureSubscription: $(serviceConnection) 
scriptType: ps 
scriptLocation: scriptPath 
scriptPath: $(Build.SourcesDirectory)/path/mypowershellscript.ps1 
arguments: "-variable1 ${{ parameters.variable }} -Force"

In the script accept this parameter in the 'Param' section

[CmdletBinding()]

Param
(
    [Parameter(Position = 0, Mandatory)]
    [string]$variable1,
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.