Get Multiple Files Using Posh-SSH SFTP

Tagged: , , ,

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Devin Garrett Devin Garrett 3 weeks, 1 day ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #38957
    Profile photo of Devin Garrett
    Devin Garrett
    Participant

    Hi,
    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 https://gist.github.com/darkoperator/6152630/raw/c67de4f7cd780ba367cccbc2593f38d18ce6df89/instposhsshdev | iex
    # Set various variables
    $sftphost = "sftp.host.com"
    # 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
    Profile photo of Alex
    Alex
    Participant

    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
    Profile photo of Devin Garrett
    Devin Garrett
    Participant

    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
    Profile photo of jaygriffin4
    jaygriffin4
    Participant

    Devin,
    did you find a solution to this?

    #53948
    Profile photo of Devin Garrett
    Devin Garrett
    Participant

    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 https://gist.github.com/darkoperator/6152630/raw/c67de4f7cd780ba367cccbc2593f38d18ce6df89/instposhsshdev | iex
    
    
    
    # Set various variables
    $ErrorActionPreference = "SilentlyContinue"
    $sftphost = "sftp.host.org"
    $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.

    • This reply was modified 3 weeks, 1 day ago by Profile photo of Devin Garrett Devin Garrett. Reason: added archive steps in the script
    #53950
    Profile photo of Devin Garrett
    Devin Garrett
    Participant

    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.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.