我定义了一些构建定义变量,其中一些是秘密类型。
我正在尝试将机密变量$RPASS
传递给TFS上的内联Powershell脚本任务,但似乎不起作用。
我在这里看过这篇文章:How to add secret variable as task environment variable in VSTS
但是,示例使用命令行。
是否可以在Powershell内联任务中传递类似的参数?
$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 "tfs@domain.com" -Subject "YAY!" -To "user@domain.com" -Body "$env:DB_NAME" -SmtpServer server.com -Port 25 -Credential $creds
在该帖子的第二个答案之后,我尝试传递参数
$(RPASS)
,然后更改此行$arg[0].ToCharArray()|%{$sec.AppendChar($_)}
但是那也不起作用
[错误]无法索引到空数组。
我尝试这样直接将其传递到脚本中:
$(RPASS).ToCharArray()|%{$sec.AppendChar($_)}
但是导致错误:
+ ********.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.
答案 0 :(得分:1)
在嵌入式脚本中使用ConvertTo-SecureString
:
$securePassword = ConvertTo-SecureString -String "$(RPASS)" -AsPlainText -Force
$creds = [System.Management.Automation.PSCredential]::new($env:USERNAME, $securePassword)
您不需要传递参数,因为TFS会解析内联脚本中的变量
答案 1 :(得分:1)