1
votes

When writing an Azure CLI script in a Azure DevOps pipeline you can get access to the the serviceprincpal id, key and tenantid. Is there a way to get this info in a Azure Powershell task?

1
Azure Powershell task doesn't have the feature to access those info directly, so we have to add an Azure Cli task before Azure PS task to pass those values to our own variables, and then share it to following Azure PS task. (task.setvariable variable xxx). If you see the ***, it means the values are correctly passed cause devops only project secret values from displaying in logs... - LoLance

1 Answers

2
votes

The addSpnToEnvironment input which adds service principal id and key of the Azure endpoint you chose to the script's context is one option available only in Azure ClI Task, but not Azure Powershell Task.

Is there a way to get this info in a Azure Powershell task?

As an alternative workaround, we can define job-scoped variables in Azure ClI Task, check this document.

Steps to test:

1.Using latest Azure CLI task 2.0-preview and choose Powershell type. Try inline script like this:

Write-Host "##vso[task.setvariable variable=SpId;]$env:servicePrincipalId"

Write-Host "##vso[task.setvariable variable=SpKey;]$env:servicePrincipalKey"

Write-Host "##vso[task.setvariable variable=TenantId;]$env:tenantId"

Write-Host "##vso[task.setvariable variable=TestVar;]JustForTest"

2.Then add one Azure Powershell task after Azure CLI Task to test:

Write-Host $env:SpId

Write-Host $env:SpKey

Write-Host $env:TenantId

Write-Host $env:TestVar

3.Output:

enter image description here

So if you define the job-scoped variables using Write-Host "##vso[task.setvariable variable=xxx]xxx"(Powershell) or echo "##vso[task.setvariable variable=xxx]xxx"(Batch), you can then use something like $env:VariableName to access the info. The *** in log is because these are secrets projected by Azure Devops, so they're masked.