Demo entry 6761891

Test

   

Submitted by Valahna on Oct 07, 2018 at 17:06
Language: PowerShell. Code size: 3.0 kB.

param([Parameter(Mandatory = $True )][array]$files,
      [Parameter(Mandatory = $True )][String]$rpath,
      [Parameter(Mandatory = $True )][String]$uname,
      [Parameter(Mandatory = $True )][String]$pwpath,
      [Parameter(Mandatory = $True )][String]$server,
      [Parameter(Mandatory = $False)][String]$keyfile,
      [Parameter(Mandatory = $True )][String]$lpath,
      [Parameter(Mandatory = $False)][Switch]$clean)

$cred = @() 
$cred += [PSObject]@{ Login = $uname
                    Password = (Get-Content $pwpath | ConvertTo-SecureString)
                        Server = $server
                        Key    = $keyfile }               

$conn = { $sc = New-Object System.Management.Automation.PSCredential $_.Login, $_.Password
            Try
            {   #if keyfile arg is passed, use it; else if $null, don't
                if($cred.key) { New-SFTPSession -ComputerName $_.Server -KeyFile $_.key -Credential $sc -AcceptKey -Verbose -ErrorVariable ProcessError -ErrorAction SilentlyContinue }
                else          { New-SFTPSession -ComputerName $_.Server -Credential $sc -AcceptKey -Verbose -ErrorVariable ProcessError -ErrorAction SilentlyContinue }
            }
            Catch
            { 
                $ErrorMessage = $_.Exception.Message
                Write-output "Error in connecting to $server"
                Write-Output "Caught Error: $ErrorMessage"
                Break 0 
            }
            Finally{ if($ProcessError){ Write-warning "New-SFTPSession error: $($ProcessError.Exception.Message)"; exit 0 } }
        }
$sessions = $cred | ForEach-Object -Process $conn -ErrorAction SilentlyContinue -ErrorVariable ProcErr  
if($ProcErr){ Write-Warning "SFTP error connecting to $server : $($ProcErr.Exception.Message)" } 

$process = { if((Get-SFTPSession).Count -eq 0) {break 0}
                $list = Get-SFTPChildItem -SessionId $_.SessionID -path $rpath 
                $expanded = $files | Select-Object @{ Name="ExpandedItem"; Expression={ $list.Name -Like $_ }} |
                                    Select-Object -ExpandProperty ExpandedItem -Unique    
                    
                $list = $list | where-object {$_.Name -in $expanded }                                                    
                if($list.count -eq 0)
			    { write-output "No New file(s) to download"; Exit 0 }
				Else
				{
				    ForEach ($f in $list)
	                            { Get-SFTPFile $_.SessionID -RemoteFile $f.fullname -LocalPath $lpath -Overwrite -Verbose 
                                      if($clean -eq $true) {Remove-SFTPItem -SessionId $_.SessionID -Path $f.fullname -Verbose }   
                                    }
				}
				0..(Get-SFTPSession).Count | Foreach-Object { Remove-SFTPSession -sessionid $_ -Verbose } | out-null       #drop session/cleanup any hanging
            }
$sessions | ForEach-Object -Process $process -Verbose                                               #do stuffs

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).