如何在 FTP 中使用 VSTS 变量,包括安全变量

How to use VSTS Variables in FTP, including Secure Variables

我需要执行以下脚本:

Param(
   [string]$HostName,
   [string]$UserName,
   [string]$Password,
   [string]$SshHostKeyFingerprint,
   [string]$RemoteFTPFolder,
   [string]$GitUserEmail,
   [string]$GitUserName,
   [string]$PersonalAccessToken,
   [string]$VSTSProjectName
)


try
{
    # Load WinSCP .NET assembly
   Add-Type -Path  "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"

    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = $HostName
        UserName = $UserName
        Password = $Password
        SshHostKeyFingerprint = $SshHostKeyFingerprint
    }

    $session = New-Object WinSCP.Session

    try
    {

        # Connect
        $session.Open($sessionOptions)

        # Download files
        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary

        $transferResult =
            $session.GetFiles($RemoteFTPFolder+"/*", "D:\a\s\", $False, $transferOptions)

        # Throw on any error
        $transferResult.Check()

        # Print results
        foreach ($transfer in $transferResult.Transfers)
        {
            Write-Host "Download of $($transfer.FileName) succeeded"
        }

        Set-Location -Path "D:\a\s\"

        git config --global user.email $GitUserEmail
        git config --global user.name $GitUserName
        git checkout master
        git pull --rebase origin master
        git add .
        git commit -m "Changes done"       
        git push https://Personal%20Access%20Token:$PersonalAccessToken@mycompany.visualstudio.com/_git/$VSTSProjectName 

    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }

    exit 0
}
catch [Exception]
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}

我也创建了变量,有些是明文,有些像密码

但是我收到这个错误:

2018-05-29T14:25:41.4804626Z Error: The value supplied is not valid, or the property is read-only. Change the value, and then try again.
2018-05-29T14:25:41.5591148Z ##[error]Process completed with exit code 1.

甚至不清楚它失败的地方

您需要将参数传递给脚本,例如 -Hostname $HostName

您可以尝试使用单引号 来转义PowerShell 中包含特殊字符的密码的异常。 (此处为 ({)。

所以,只需尝试像这样设置 Password 变量的值:'b(!d_@{xxx'

然后再试一次。