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
}