1

I am switching user in powershell script

# password is secret variable from azure devops pipeline 
$username = 'user2'
$password = convertto-securestring $env:password -AsPlainText -Force

Note: it work for normal string and it wont work for secure variable string

I am getting below error:

ConvertTo-SecureString : Cannot bind argument to parameter 'String' because it is null.
At D:\azdo\_work\_temp\cb1ff41e-8df7-48b7-bc48-f23f47d47b8a.ps1:9 char:36
+ $password = convertto-securestring $env:password -AsPlainText ...
+                                    ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [ConvertTo-SecureString], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertToSe 
   cureStringCommand
5
  • There is actually no environment variable named ansible_password in your context and then, it's evaluated as $null Commented Jan 25, 2023 at 6:20
  • @Cid, update variable in error, but it is failing for secure variable and normal string it is working Commented Jan 25, 2023 at 6:23
  • What happens if you explicitly set the environment password: $env:password = 'MyPassword'? Commented Jan 25, 2023 at 6:54
  • @sagar same comment applies with the corrected name : there is no env variable named password, that's why it's null Commented Jan 25, 2023 at 7:11
  • @Iron its in that way Commented Jan 25, 2023 at 12:09

1 Answer 1

1

In Azure DevOps YAML pipelines you need to explicitly map the secret variables to make them available in scripts (this is documented here):

- powershell: |
    Write-Host "Using the mapped env var for this task works and is recommended: $env:MY_MAPPED_ENV_VAR"
  env:
    MY_MAPPED_ENV_VAR: $(mySecret) # the recommended way to map to an env variable
Sign up to request clarification or add additional context in comments.

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.