Check existance of file on sftp through powershell and send email if file exists there.

chetan Vishwakarma 146 Reputation points
2021-09-10T12:59:15.747+00:00

Hello Everyone ,

I am new to PowerShell & I want to create a script in PowerShell which checks the file on sftp server and it will send email if file will be there on sftp.

I have checked but didn't found anything , but have some clue that we can call WINSCP dot net assembly through PowerShell to achieve this. Here are the details below i have received from the business

sftp Server name : sftp.xyz.org
username : Test
password : Test@123
port : 22

Only these details I have received from the business side. Could you please create the script for me if possible ?

Thanks in advance !

Br
Chetan

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,555 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Limitless Technology 39,686 Reputation points
    2021-09-10T16:24:42.61+00:00

    Hello @chetan Vishwakarma

    I would like to suggest you to check below Microsoft thread which used WinSCP assembly to check if file exists or not in SFTP.

    https://social.technet.microsoft.com/Forums/en-US/f043b0b1-1145-40ac-931e-36b512762551/find-files-on-sftp-site-using-winscp-and-process-if-exist?forum=winserverpowershell

    If the reply was helpful, please don’t forget to upvote or accept as answer.

    0 comments No comments

  2. MotoX80 34,431 Reputation points
    2021-09-10T22:29:04.547+00:00

    Download Psftp

    https://www.puttygen.com/psftp

    Use the sample code that I gave you in your first question to call PsFtp.exe instead of Ftp.exe.

    https://learn.microsoft.com/en-us/answers/questions/543962/powershell-to-check-existance-of-file-on-ftp-serve.html

    The command line switches will be different, but it should not be that difficult for you to figure out.

    C:\>psftp -?  
    PuTTY Secure File Transfer (SFTP) client  
    Release 0.74  
    Usage: psftp [options] [user@]host  
    Options:  
      -V        print version information and exit  
      -pgpfp    print PGP key fingerprints and exit  
      -b file   use specified batchfile  
      -bc       output batchfile commands  
      -be       don't stop batchfile processing if errors  
      -v        show verbose messages  
      -load sessname  Load settings from saved session  
      -l user   connect with specified username  
      -P port   connect to specified port  
      -pw passw login with specified password  
      -1 -2     force use of particular SSH protocol version  
      -4 -6     force use of IPv4 or IPv6  
      -C        enable compression  
      -i key    private key file for user authentication  
      -noagent  disable use of Pageant  
      -agent    enable use of Pageant  
      -hostkey aa:bb:cc:...  
                manually specify a host key (may be repeated)  
      -batch    disable all interactive prompts  
      -no-sanitise-stderr  don't strip control chars from standard error  
      -proxycmd command  
                use 'command' as local proxy  
      -sshlog file  
      -sshrawlog file  
                log protocol details to a file  
      
    C:\>  
      
    
    0 comments No comments

  3. Terry N 146 Reputation points
    2022-11-12T15:57:47.28+00:00

    I needed to do the same thing and google got me here but did not find any of the answers helpful at all.

    This worked for me anyway
    Store credentials in Credential Manager as xyz

    $cred = Get-StoredCredential -Target xyz;  
    $SFTPSession = New-SFTPSession -ComputerName sftp.xyz.org -Credential $cred  
    Test-SFTPPath $SFTPSession "/path/filename.txt"  
    Remove-SFTPSession -SessionID 0 -Verbose  
    Get-SFTPSession # just a check to ensure sessions is closed  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.