1

I have a requirement to upload multiple files to SFTP server from my machine using powershell.i have created the following scripts for the same . the script is working fine . but need to check file existence before upload to SFTP server .

the following are the script i used for upload

$username = "UserName"

$password = "Password" | ConvertTo-SecureString -AsPlainText -Force

$server = "xxxxx"

$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList  
  $username,$password

Get-SFTPSession | Remove-SFTPSession

$SFTPSession=New-SFTPSession -ComputerName $server -Credential $credential -Port xxxx

$FilePath = get-childitem "D:\Test\xx*"

$SftpPath = '/D:/xxx/xxx/Test'

 ForEach ($LocalFile in $FilePath)
{
Set-SFTPFile -SessionId $SFTPSession.SessionID -LocalFile $LocalFile.fullname -RemotePath $SftpPath -Overwrite
}

Please any body advice me on to check multiple files existence before uploading and only upload files which is not exist in server .

Thanks in advance

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992

1 Answers1

1

Assuming you are using Posh-SSH, you want to use the Test-SFTPPath cmdlet:

if (Test-SFTPPath -SFTPSession $session -Path $Path)
{
   ...
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992