将秘密变量从 TFS 构建传递到 Powershell 脚本

Pass Secret Variable from TFS Build to Powershell script

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

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

我的 PowerShell 脚本如下所示

 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 脚本。所以唯一的选择是将密码作为输入传递给脚本。

终于解决了

我在通过 powershell 脚本参数发送密码时用双引号引起来。繁荣!!它开始工作了。它发送解密的密码。

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

我的力量 shell 脚本与上面相同。

 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