Search code examples
azurepowershellazure-powershell

Read Json files from Azure Blob and upload to a SFTP end point


I am using the following script to read the Json files from Azure Blob and download to a local path, then the script uploads the files to SFTP end point using WinSCP module. I am using WinSCP module to solve the discrepancy between the SSH version in the script and the SFTP server.

During the upload it asks for a SSH assembly private key . Is there a way to upload the files without the SSH key as this is part of automation. Also is it possible to upload the files directly to SFTP instead of transfer from local folder. I could also change the scripto use any other module if it is less complicated . Thanks.

$containerName = ""
$localPath = ""
$sftpRemotePath = ""
$connectionString = ""
$storageContext = New-AzStorageContext -ConnectionString $connectionString
$container = Get-AzStorageContainer -Name $containerName -Context $storageContext
$blobs = Get-AzStorageBlob -Container $containerName -Context $storageContext -Prefix $folderPath
$jsonFiles = $blobs | Where-Object { $_.Name -like "*.json" }
$jsonFiles | ForEach-Object {
    $blobName = $_.Name
    $localFilePath = $blobFolder
    try {
        Write-Host "Downloading: $blobName"
        $fullLocalPath = Join-Path -Path $localPath -ChildPath $localFilePath
        Get-AzStorageBlobContent -Container $containerName -Blob $blobName -Context $storageContext -Destination $fullLocalPath -ErrorAction Stop
        Write-Host "Downloaded: $blobName"
Write-Host "Uploading to SFTP: $blobName"
try {
    Add-Type -Path "\WinSCPnet.dll"
    $winscpPath = "\winSCP.exe"
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = sftpHost
        UserName = sftpUser
        Password = sftpPassword
        PortNumber = sftpPort
    }
    $session = New-Object WinSCP.Session
    $Session.ExecutablePath = $winscpPath
    $session.Open($sessionOptions)
    $remotePath = $sftpRemotePath
    $session.PutFiles($fullLocalPath, $remotePath).Check()
    $session.Dispose()
    Write-Host "Uploaded to SFTP: $blobName"
}```

Solution

  • You can add this to the list of options: GiveUpSecurityAndAcceptAnySshHostKey = "true"

    Property @{
            Protocol = [WinSCP.Protocol]::Sftp
            HostName = sftpHost
            UserName = sftpUser
            Password = sftpPassword
            PortNumber = sftpPort
            GiveUpSecurityAndAcceptAnySshHostKey = "true"  <=
        }