Get Multiple Files Using Posh-SSH SFTP

Welcome Forums General PowerShell Q&A Get Multiple Files Using Posh-SSH SFTP

This topic contains 5 replies, has 3 voices, and was last updated by

2 years, 5 months ago.

  • Author
  • #38957

    Points: 0
    Rank: Member

    I have a scheduled task running that gets posh and attempts to start a session and get files from an sftp server. The filenames can be random and there are usually more than one at a time. If this were command I would be attempting something like mget or get-childitem. Posh has a get-sftpchilditem but I'm not sure if it's applicable. Any guideance would be appreciated. In case I'm ranting a bit here's my question:

    Can i use the following code to get multiple files using posh sftp?

    wget | iex
    # Set various variables
    $sftphost = ""
    # Yes I know passwords as plain text is yucky...
    $PWord = ConvertTo-SecureString –AsPlainText 'Password' -Force
    $Credential = New-Object System.Management.Automation.PSCredential ('user', $PWord)
    $Session = New-SFTPSession -ComputerName $sftphost -Credential $Credential 
    # End any sessions that are currently running
    Get-SFTPSession | Remove-SFTPSession
    #Download Files
    Get-SFTPFile -SFTPSession $Session -RemoteFile "/response/*" -LocalPath "C:\temp\SFTPImport\"
  • #38984

    Points: 0
    Rank: Member

    It depends if the parameter in Get-SFTPFile -remotefile supports wildcards.

    It might just be looking for a file called "*"

    I downloaded the module and checked its help file and its lacking.

    If there is a cmdlet in that module to show all files in the directory you could do a foreach loop on them.

    I don't have a SFTP setup so I can't even test this.

  • #39158

    Points: 0
    Rank: Member

    I believe I'm getting closer with the line Get-SFTPChildItem -SessionId 0 -Recursive | %{ "{1,-10}{1,-10}{2,-60}" -f $_.Filename,$_.Name,$_.Definition } -Outvariable responsefiles

    but I'm having trouble setting it up in a for each statement. Here's what I've got:

     New-SFTPSession -ComputerName $sftphost -Credential $Credential
    Set-SFTPLocation "/response/" -sessionID 0
    Get-SFTPChildItem -SessionId 0 -Recursive | %{ "{1,-10}{1,-10}{2,-60}" -f $_.Filename,$_.Name,$_.Definition } -Outvariable responsefiles
    Get-SFTPFile -SFTPSession $Session -RemoteFile $responsefiles -LocalPath "c:\temp\SFTPImport\"
    • #53935

      Points: 1
      Rank: Member

      did you find a solution to this?

  • #53948

    Points: 0
    Rank: Member

    We'll for the posting of the files I was able to setup something. The following script will allow you to post files from the $exportfolder paths you define in the $folders array. I have it set to look for any *.XML files but you can easily change the extension or create a variable that identifies the files in particular you're looking for.

    #to get the PoshSFTP package uncomment or remove the hashtag on the next line
    #wget | iex
    # Set various variables
    $ErrorActionPreference = "SilentlyContinue"
    $sftphost = ""
    $PWord = ConvertTo-SecureString –AsPlainText 'P@$$w0rd' -Force
    $Credential = New-Object System.Management.Automation.PSCredential ('username', $PWord)
    $Session = New-SFTPSession -ComputerName $sftphost -Credential $Credential
    $exportfolder = "C:\Export"
    $archive = "C:\Export\Archive"
    $processed = "C:\Temp\ExportSFTP\processed"
    $folders = @(
    Join-Path $exportfolder "\Folder1\*"
    Join-Path $exportfolder "\Folder2\*"
    Join-Path $exportfolder "\Folder3\*"
    #move files from the export folders and wait for it to complete
    Get-ChildItem -path $folders -Filter *.XML -Recurse | ForEach-Object { Move-Item $_.FullName -Destination $processed}
    Start-Sleep -Seconds 3
    #Set variable for temporary files
    $LocalFiles = Get-ChildItem -path $processed -Filter *.XML -Recurse
    #End any sessions that are currently running and start a new one
    Get-SFTPSession | Remove-SFTPSession
    Start-Sleep -Seconds 2
    New-SFTPSession -ComputerName $sftphost -Credential $Credential
    #Set your working directory
    Set-Location $processed
    #Now copy the files up
    ForEach ($LocalFile in $LocalFiles)
    Set-SFTPFile -SessionId 0 -LocalFile "$LocalFile" -RemotePath "/request"
    #Rename and archive the local files
    Get-ChildItem -path $processed -Filter *.XML -Recurse | foreach-object {
    rename-item $_ -newname ($_.Fullname -replace '\.xml','.bak') -force 
    Get-ChildItem -path $processed -Filter *.bak -Recurse | foreach-object {
    Move-Item $_.FullName -Destination $Archive} 

    Some of this may be superfluous and it was a little trial and error to get it to work (like the set-location piece I played with a hard coded path). I'm still looking into pulling the files though.

  • #53950

    Points: 0
    Rank: Member

    Also I should note that I had a #requires piece for the posh-ssh package but removed it as it needs a little more work to function properly. I set this up as a scheduled job and input my applicable values and haven't had any issues with it.

The topic ‘Get Multiple Files Using Posh-SSH SFTP’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort