How to pass a secret type variable to script?

by Cataster   Last Updated June 13, 2019 08:26 AM

I defined some build definition variables, of which some i made as secret type.

I am trying to pass the secret variable $RPASS to an inline powershell script task on TFS, but it appears thats not working.

I looked at this post here: How to add secret variable as task environment variable in VSTS

however , the examples use command line.

is it possible to pass arguments like that in a powershell inline task?

$sec = New-Object -TypeName System.Security.SecureString
"$RPASS".ToCharArray()|%{$sec.AppendChar($_)}
$creds = new-object -typename System.Management.Automation.PSCredential -args "$env:USER", $sec
Send-MailMessage -From "[email protected]" -Subject "YAY!" -To "[email protected]" -Body "$env:DB_NAME" -SmtpServer server.com -Port 25 -Credential $creds

Following the second answer in that post, i tried passing in the arguments

$(RPASS)

arg

and then changed this line $arg[0].ToCharArray()|%{$sec.AppendChar($_)}

but that didnt work either

[error]Cannot index into a null array.

I tried passing it directly into the script as so:

$(RPASS).ToCharArray()|%{$sec.AppendChar($_)}

but that resulted in error:

+ ********.ToCharArray()|%{$sec.AppendChar($_)}
+                                 ~
An expression was expected after '('.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : ExpectedExpression


2019-06-13T00:57:50.7974750Z ##[error]Process completed with exit code 0 and had 1 error(s) written to the error stream.
Tags : powershell tfs


Answers 2


Use ConvertTo-SecureString in your inline script:

$securePassword = ConvertTo-SecureString -String "$(RPASS)" -AsPlainText -Force
$creds = [System.Management.Automation.PSCredential]::new($env:USERNAME, $securePassword)
mhu
mhu
June 13, 2019 07:39 AM

You should pass it in the arguments but you also need to add a param in the script:

Param (
 [string]$RPASS
)
$sec = New-Object -TypeName System.Security.SecureString
$RPASS.ToCharArray()|%{$sec.AppendChar($_)}

And in the "Arguments" field pass the variable:

-RPASS $(RPASS)
Shayki Abramczyk
Shayki Abramczyk
June 13, 2019 08:26 AM

Related Questions


Updated June 22, 2018 01:26 AM

Updated November 28, 2018 14:26 PM

Updated July 08, 2017 02:26 AM

Updated May 14, 2018 03:26 AM