Mski
Mski

Reputation: 13

Azure Devops Pipeline - How to pass variable into Powershell function

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 

Upvotes: 1

Views: 7332

Answers (3)

Umesh Torawane
Umesh Torawane

Reputation: 539

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,
)

Upvotes: 0

Leo Liu
Leo Liu

Reputation: 76760

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.

Upvotes: 1

bianconero
bianconero

Reputation: 225

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}}"'

Upvotes: 0

Related Questions