Welcome Forums General PowerShell Q&A Copying files to sFTP using Posh-SSH module. (only transfering single file)

Viewing 6 reply threads
  • Author
    Posts
    • #172402
      Participant
      Topics: 3
      Replies: 9
      Points: 59
      Rank: Member

      Hi

      I want to use the script pasted below to transfer files from a local folder to an sFTP folder.
      It works as intended, the only problem is that only copies one file, and I want to copy all the .csv files from the folder.

      # Set the credentials
      $Password = ConvertTo-SecureString 'Password' -AsPlainText -Force
      $Credential = New-Object System.Management.Automation.PSCredential ('Username', $Password)
      
      # Set local file path, SFTP path, and the backup location SMB path
      $FilePath = "D:\Folder\*.csv"
      $SftpPath = '/sFTP Folder'
      $SmbPath = 'D:\Backup'
      
      # Set the IP of the SFTP server
      $SftpIp = 'ServerIP'
      
      # Load the Posh-SSH module
      Import-Module C:\Temp\Posh-SSH
      
      # Establish the SFTP connection
      $ThisSession = New-SFTPSession -ComputerName $SftpIp -Credential $Credential
      
      # Upload the file to the SFTP path
      Set-SFTPFile -SessionId ($ThisSession).SessionId -Localfile $FilePath -RemotePath $SftpPath
      
      #Disconnect all SFTP Sessions
      Get-SFTPSession | % { Remove-SFTPSession -SessionId ($_.SessionId) }
      
      # Copy the file to the SMB location
      Copy-Item -Path $FilePath -Destination $SmbPath
    • #172414
      Participant
      Topics: 0
      Replies: 115
      Points: 433
      Helping Hand
      Rank: Contributor

      Hello Danky,

      Any particular reason you are not querying the child items of the path? Based on the examples for the command, it supports string arrays, but the way you are trying to pull the items is the culprit I suspect. Give this a go instead and let me know if that fixes the issue.

      $FilePath = Get-ChildItem -Path D:\Folder -Name *.csv | Select-Object -ExpandProperty FullName
    • #172417
      Participant
      Topics: 3
      Replies: 9
      Points: 59
      Rank: Member

      Yes that was the Solution 🙂
      Many thx Jason

      Here is the script copying all files instead of a single file.

      # Set the credentials
      $Password = ConvertTo-SecureString 'Password' -AsPlainText -Force
      $Credential = New-Object System.Management.Automation.PSCredential ('Username', $Password)
      
      # Set local file path, SFTP path, and the backup location SMB path
      $FilePath = Get-ChildItem -Path D:\Folder -Name *.csv | Select-Object -ExpandProperty FullName
      $SftpPath = '/sFTP Folder'
      $SmbPath = 'D:\Backup'
      
      # Set the IP of the SFTP server
      $SftpIp = 'ServerIP'
      
      # Load the Posh-SSH module
      Import-Module C:\Temp\Posh-SSH
      
      # Establish the SFTP connection
      $ThisSession = New-SFTPSession -ComputerName $SftpIp -Credential $Credential
      
      # Upload the file to the SFTP path
      Set-SFTPFile -SessionId ($ThisSession).SessionId -Localfile $FilePath -RemotePath $SftpPath
      
      #Disconnect all SFTP Sessions
      Get-SFTPSession | % { Remove-SFTPSession -SessionId ($_.SessionId) }
      
      # Copy the file to the SMB location
      Copy-Item -Path $FilePath -Destination $SmbPath
    • #172420
      Participant
      Topics: 0
      Replies: 115
      Points: 433
      Helping Hand
      Rank: Contributor

      Awesome! Happy to help.

      • #172435
        Participant
        Topics: 3
        Replies: 9
        Points: 59
        Rank: Member

        Another problem has now appeared, I have no idea why. It was working. And now I get an error.

        # Set local file path, SFTP path, and the backup location path. 
        $FilePath = Get-ChildItem -Path "D:\Routines\Tester\" -Name "*.csv" | Select-Object -ExpandProperty FullName
        $SftpPath = '/test'
        $BackupPath = 'D:\Routines\Backup'

        And now I am getting error

        Select-Object : Property "FullName" cannot be found.
        At D:\Routines\Move_item_to_sFTP_Mobistyle.ps1:9 char:85
        + ... obistyle test" -Name "*.csv" | Select-Object -ExpandProperty FullName
        + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo : InvalidArgument: (0012121915_0001...080141_2105.csv:PSObject) [Select-Object], PSArgumentException
        + FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand
        Select-Object : Property "FullName" cannot be found.
        At D:\Routines\Move_item_to_sFTP_Mobistyle.ps1:9 char:85
        + ... obistyle test" -Name "*.csv" | Select-Object -ExpandProperty FullName
        + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo : InvalidArgument: (0012121915_0001...080141_2105.csv:PSObject) [Select-Object], PSArgumentException
        + FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand
        
        Set-SFTPFile : Cannot bind argument to parameter 'LocalFile' because it is null.
        At D:\Routines\Move_item_to_sFTP s1:23 char:61
        + ... File -SessionId ($ThisSession).SessionId -Localfile $FilePath -Remote ...
    • #172555
      Participant
      Topics: 0
      Replies: 115
      Points: 433
      Helping Hand
      Rank: Contributor

      Danky,

      Do you still have files in the location? I would recommend creating some error handling in your script as well. For example.

      IF ($FilePath -gt 0){
         Set-SFTPFile-SessionId ($ThisSession).SessionId -Localfile $FilePath-RemotePath $SftpPath
      }
      else{
         Write-Warning"No Files found in the directory. Please verify content."
      }
      • #172811
        Participant
        Topics: 3
        Replies: 9
        Points: 59
        Rank: Member

        Hi

        Yes, there are files in the folder, it runs the same type of error for each file.

        Right now there are 40 .csv files located in the folder, all with data.

        Filename with variation is.  001212915_00013318_valuereport_20190826104555.csv

        Select-Object : Property "FullName" cannot be found.
        At D:\Routines\Move_item_to_sFTP_Mobistyle.ps1:9 char:85
        + ... obistyle test" -Name "*.csv" | Select-Object -ExpandProperty FullName
        + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo : InvalidArgument: (0012121915_0001...080141_2105.csv:PSObject) [Select-Object], PSArgumentException
        + FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand

        It runs the error message 40 times, and after implementing the IF clause as you suggested, then it still ends with the warning that no files were found.

        Very strange

    • #173062
      Participant
      Topics: 0
      Replies: 115
      Points: 433
      Helping Hand
      Rank: Contributor

      Danky,

      Interesting, and this was working previously. Can you confirm the folder location you have the files located in? In the script, it was hardcoded to D:\Routines\Tester\. The error code states D:\Routines. Please confirm the location and try this variant of Get-ChildItem.

      Get-ChildItem -Path D:\Routines\Tester -Filter *.csv
      PS C:\> Get-ChildItem -Path C:\Folder\ -Filter *.csv | Select-Object -ExpandProperty FullName
      C:\Folder\File1.csv
      C:\Folder\File10.csv
      C:\Folder\File2.csv
      C:\Folder\File3.csv
      C:\Folder\File4.csv
      C:\Folder\File5.csv
      C:\Folder\File6.csv
      C:\Folder\File7.csv
      C:\Folder\File8.csv
      C:\Folder\File9.csv
      • #173341
        Participant
        Topics: 3
        Replies: 9
        Points: 59
        Rank: Member

        Hi Jason

        Many thanks for your patience, and help 🙂

        It is working now after I used -Filter instead of -Name

        Not working (-Name *.csv)

        # Set local file path, SFTP path, and the backup location path. 
        $FilePath = Get-ChildItem -Path "D:\Routines\Tester\" -Name *.csv | Select-Object -ExpandProperty FullName
        $SftpPath = '/test'
        $BackupPath = 'D:\Routines\Backup'

        Working (-Filter *.csv)

        # Set local file path, SFTP path, and the backup location path. 
        $FilePath = Get-ChildItem -Path "D:\Routines\Tester\" -Filter *.csv | Select-Object -ExpandProperty FullName
        $SftpPath = '/test'
        $BackupPath = 'D:\Routines\Backup'

         

    • #173467
      Participant
      Topics: 0
      Replies: 115
      Points: 433
      Helping Hand
      Rank: Contributor

      Glad it’s working now. Let us know if you need any further assistance!

Viewing 6 reply threads
  • The topic ‘Copying files to sFTP using Posh-SSH module. (only transfering single file)’ is closed to new replies.