Automatically pass string to powershell user input

Viewed 323

In my powershell script, I call the following azure function:

az repos import create --git-source-url https://incommunities@dev.azure.com/my-organisation/Templates/_git/$($Framework) --detect true --project $ProjectAlias --repository $ProjectAlias --requires-authorization

When running, it prompts the user for a Password/PAT token, e.g:

Git Password / PAT:

Is there a way to automatically pass the password/token to the user input without having to enter manually?

I attempted to pipe the value through, however this does not seem to work e.g

my-pat-token | az repos import create --git-source-url https://incommunities@dev.azure.com/my-organisation/Templates/_git/$($Framework) --detect true --project $ProjectAlias --repository $ProjectAlias --requires-authorization

Is this both a) possible and then if so b) how can I do this?

1 Answers

There are two approaches you can use, both come courtesy from this nice blog post which you'll probably want to read, as it talks about a bunch of Azure Devops tasks.

  1. Use an environmental variable

These commands will check for the presence of an environmental variable and will use it instead of prompting.

To do this, set an environment variable called AZURE_DEVOPS_EXT_PAT to the value of your PAT. (More info on how these tokens work here from the Microsoft Docs)

# set environment variable for current process
$env:AZURE_DEVOPS_EXT_PAT = 'xxxxxxxxxx'

When you're automating things, just set this variable before running the Azure commands.

  1. Pipe the value in

I am not as big of a fan of this sort of approach but you can "echo out" the PAT value and pipe that into a command, which might work. IMHO this is more fragile and frunky and I wouldn't advise it.

$pat | az devops login
Related