2
votes

I am writing my own Powershell script to upload some files over sFTP in a Azure Devops release pipeline.

Locally I made the script work but to make it work in the pipeline like a inline script is not going so well.

I build my script up with default params so I can test it locally with input, but when I execute it in Azure I get the error message

param : The term 'param' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Am I not allowed to use param() in Azure?

param (
    $Localpath = $env:System_ArtifactsDirectory,
    $Remotepath = $env:sFTP_Remotepath,
    $Hostname = $env:sFTP_Host,
    $Username = $env:sFTP_Username,
    $Password = $env:sFTP_Password,    
    $HostKeyFingerprint = $env:sFTP_Hostkey,
    $WinSCPnetdllpath =  "$env:System_ArtifactsDirectory\WinSCPnet.dll"
)

try
{
    Write-Host "Hostname:" $Hostname
    Write-Host "Username:" $Username
    Write-Host "Password: ****"
    Write-Host "Remotepath:" $Remotepath
    Write-Host "Localpath:" $Localpath
    Write-Host "HostKeyFingerprint:" $HostKeyFingerprint
    Write-Host "WinSCPnetdllpath:" $WinSCPnetdllpath
    # Load WinSCP .NET assembly
    Add-Type -Path $WinSCPnetdllpath

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

    $session = New-Object WinSCP.Session

    try
    {
        # Connect
        $session.Open($sessionOptions)

        # Upload files, collect results
        $transferResult = $session.PutFiles($Localpath, $Remotepath, $False)

        Write-Host ("Found {0} of files to upload" -f $transferResult.Transfers.Count)

        # Iterate over every transfer
        foreach ($transfer in $transferResult.Transfers)
        {
            # Success or error?
            if ($transfer.Error -eq $Null)
            {
                Write-Host (" - Upload of '{0}' .. Success" -f $transfer.FileName)                              
            }
            else
            {
                Write-Host (" - Upload of '{0}' .. Failed: {1}" -f $transfer.FileName, $transfer.Error.Message)
            }
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }

    exit 0
}
catch [Exception]
{
    Write-Host ("Error: {0}" -f $_.Exception.Message)
    exit 1
}
1
How are you running it? What task? Is it an inline script or a file from source control?Daniel Mann
inline script powershell taskJepzen
What does the configuration of that step look like?Daniel Mann
I added a powershell step and copied the script into the window after choosing inlineJepzen

1 Answers

0
votes

There are some wrapper statements placed around the inline created powershell scripts that will cause errors. It doesn't appear to currently support the param block for inline calls. You could instead just reference your parameters directly instead.

$ErrorActionPreference = 'stop'
...
if (!(Test-Path -LiteralPath variable:\LASTEXITCODE)) {
    Write-Host '##vso[task.debug]$LASTEXITCODE is not set.'
} else {
    Write-Host ('##vso[task.debug]$LASTEXITCODE: {0}' -f $LASTEXITCODE)
    exit $LASTEXITCODE
}

There is some discussion about it on this github issue. You could also glance through the code, since it is open source.