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

时间:2018-05-29 14:30:31

标签: powershell tfs azure-devops tfsbuild azure-pipelines

我需要执行以下脚本:

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\1\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\1\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
}

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

enter image description here

但是我收到了这个错误:

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.

它甚至不清楚失败的地方

1 个答案:

答案 0 :(得分:1)

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

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

因此,只需尝试设置Password变量的值,如下所示:'b(!d_@{xxx'

然后再试一次。