Pass Secret Variable from TFS Build to Powershell script

旧街凉风 提交于 2021-02-07 05:49:06

问题


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

TFS Build Variables

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

Build Step

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

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.


回答1:


Finally I managed to solve it.

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)"

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


来源:https://stackoverflow.com/questions/42822228/pass-secret-variable-from-tfs-build-to-powershell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!