Search code examples
powershelltfsbuildcontinuous-deployment

Pass Secret Variable from TFS Build to Powershell script


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.


Solution

  • 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