0

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"
}```
Jeganaak
  • 313
  • 1
  • 3
  • 23
  • Does this answer your question? [WinSCP .NET library: Connect to SFTP server without specifying SSH host key fingerprint](https://stackoverflow.com/questions/17938825/winscp-net-library-connect-to-sftp-server-without-specifying-ssh-host-key-fing) + One problem per question only please. – Martin Prikryl Jul 21 '23 at 15:15

1 Answers1

-1

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"  <=
    }
  • Perfect. It works with this property. Is there a way to upload the file to SFTP directly without downloading to Local path. Since I have several blobs with Json file every minute I would like to upload all at once . Is this possible. Thanks. – Jeganaak Jul 19 '23 at 16:22
  • 1
    Please do not suggest anyone to skip SSH host key verification without explaining the security consequences, + `GiveUpSecurityAndAcceptAnySshHostKey` is obsolete. – Martin Prikryl Jul 21 '23 at 15:12