将秘密变量从TFS Build传递到Powershell脚本 [英] Pass Secret Variable from TFS Build to Powershell script

查看:56
本文介绍了将秘密变量从TFS Build传递到Powershell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在构建定义中添加了一个名为Password的秘密变量,如下图所示:

I have added secret variable called Password in my build definition as shown in this image:

我要在构建步骤之一中将密码传递给PowerShell脚本,如下图所示:

I want to pass the Password to the PowerShell script in one of my build steps as shown in this image:

我的PowerShell脚本如下

My PowerShell script looks like this

 Param
(
    [Parameter(Mandatory=$true)]
    [string]$UserName,
    [Parameter(Mandatory=$true)]
    [string]$Password
)

$appPool = New-WebAppPool -Name "test"
$appPool.processModel.userName = $userName
$appPool.processModel.password = $password
$appPool.processModel.identityType = "SpecificUser"
$appPool | Set-Item

但是看起来密码的类型不是字符串.我尝试了 PSCredential ,但没有成功.有人可以帮我吗?如何将密码从构建步骤传递到PowerShell脚本以及安全变量的类型?我无法直接读取环境变量,因为我正在目标计算机上运行PowerShell脚本.因此,唯一的选择是将密码作为输入传递给脚本.

But it looks like the type of the Password is not a string. I tried PSCredential but didn't work. Can somebody help me? How do I pass the password from build step to the PowerShell script and the type of the secure variable? I can't read the environment variable directly because I am running the PowerShell script on a target machine. So only option is to pass Password to the script as input.

推荐答案

最后,我设法解决了这个问题.

Finally I managed to solve it.

通过powershell脚本参数发送密码时,我在密码周围加上了双引号.繁荣!!它开始工作.它发送解密的密码.

I have put double quotes around my Password when sending it via the powershell script arguments. Boom!! it started working. It sends the decrypted password.

-用户名$(用户名)-密码"$(密码)"

-UserName $(Username) -Password "$(Password)"

我的Power Shell脚本与上面相同.

My power shell script stays the same as above.

 Param
(
    [Parameter(Mandatory=$true)]
    [string]$UserName,
    [Parameter(Mandatory=$true)]
    [string]$Password
)

$appPool = New-WebAppPool -Name "test"
$appPool.processModel.userName = $userName
$appPool.processModel.password = $password
$appPool.processModel.identityType = "SpecificUser"
$appPool | Set-Item

这篇关于将秘密变量从TFS Build传递到Powershell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆