1

Currently having an issue where I am struggling to get the WinSCP PowerShell module to work as expected within an Azure Automation Runbook. Installed via the gallery but the cmdlets were not showing properly. Installed manually at the top of the script as advised on another thread.

Import-Module -Name WinSCP

Then my code to call the module is failing, but its failing silently and after 30 seconds-ish, the whole runbook starts over! I have tried to get the logs to verbose and have what I am getting at the end of the code in the logs.

$SFTPSite = 'ftp IP address'
$SFTPDirectory = '/in/daily'
$LocalDirectory = $encryptedFilePath
$SessionLogFilePath = $encryptedFilePath + '\winscp.log'
$SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx"

$session = New-Object WinSCP.Session
$session.ExecutablePath = "C:\Modules\User\WinSCP\bin\WinSCP.exe"
$session.DebugLogPath = "$SessionLogFilePath"
$session.DebugLogLevel = "2"

$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.HostName = $SFTPSite
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.UserName = "unremoved"
$sessionOptions.SecurePrivateKeyPassphrase = $sftpPassPhrase
$sessionOptions.SshPrivateKeyPath = "$privateKeyFilePath"
$sessionOptions.SshHostKeyFingerprint = $SshHostKeyFingerprint

Write-Output "SFTP session created"

gc $SessionLogFilePath

try {
    $session.Open($sessionOptions) 

    Write-Output "SFTP session established"
        
    $directory = $session.ListDirectory($SFTPDirectory)
 
    foreach ($fileInfo in $directory.Files)
    {
        Write-Host ("$($fileInfo.Name) with size $($fileInfo.Length), " +
            "permissions $($fileInfo.FilePermissions) and " +
            "last modification at $($fileInfo.LastWriteTime)")
    }

    Write-Output "SFTP commands completed"
    $session.Dispose()
    Write-Output "SFTP session removed"

} catch {
    Write-Host "Error: $session.Output"
    gc $SessionLogFilePath
    $session.Dispose()
}

Log file output which I don't get much from...

[2023-08-01 14:04:56.478] [0009] .NET Framework build
[2023-08-01 14:04:56.478] [0009] Executing assembly: WinSCPnet, Version=1.14.0.13736, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf
[2023-08-01 14:04:56.478] [0009] Executing assembly codebase: file:///C:/Modules/User/WinSCP/lib/net40/WinSCPnet.dll
[2023-08-01 14:04:56.478] [0009] Executing assembly location: C:\Modules\User\WinSCP\lib\net40\WinSCPnet.dll
[2023-08-01 14:04:56.478] [0009] Entry Assembly: Orchestrator.Sandbox, Version=7.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
[2023-08-01 14:04:56.478] [0009] Operating system: Microsoft Windows NT 6.2.9200.0
[2023-08-01 14:04:56.478] [0009] Bitness: 64-bit
[2023-08-01 14:04:56.478] [0009] Timezone: 00:00; Coordinated Universal Time
[2023-08-01 14:04:56.478] [0009] User: Administrator@CLIENT@CLIENT; Interactive: True
[2023-08-01 14:04:56.478] [0009] Runtime: 4.0.30319.42000
[2023-08-01 14:04:56.478] [0009] Console encoding: Input: Western European (Windows) (1252); Output: Western European (Windows) (1252)
[2023-08-01 14:04:56.478] [0009] Working directory: C:\Windows\System32
[2023-08-01 14:04:56.494] [0009] Assembly path: C:\Modules\User\WinSCP\lib\net40\WinSCPnet.dll
[2023-08-01 14:04:56.494] [0009] Assembly product version: 6.1.1.0
[2023-08-01 14:04:56.494] [0009] Entry assembly path: C:\Orchestrator\Orchestrator.Sandbox.exe

Any help would be massively appreciated!

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
MKRedacted
  • 11
  • 1

1 Answers1

0

WinSCP .NET assembly uses WinSCP executable (winscp.exe) internally.

Azure Runbooks do not support running executables:
Can custom console application be executed in Azure (PowerShell) Runbook?

So unfortunately, you cannot use WinSCP .NET assembly in Azure Runbook.

You can use WinSCP .NET assembly in Azure Web Jobs though.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992