Home > Mobile >  Powershell script Cannot bind argument to parameter 'String' because it is null
Powershell script Cannot bind argument to parameter 'String' because it is null

Time:01-26

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

CodePudding user response:

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
  • Related