title: Posh-SSH PowerShell How to get filename and status, and add line to log file
body: <p>Im trying copy file to SFTP server i use posh shh and if it was successfully moved then I get the filename and add new line in Log file, otherwise i add line that file wasnt move or file not in source dir.
  I understand how to copy and add line into the log file but i cant bring the  right logic in to this script.</p>
  <p>Pls see my code below:</p>
  <pre><code># SFTP Upload of Inventory From CSV files to WPEngine SFTP. Requires installation of Posh-SSH 
  # Install-Module -Name Posh-SSH (https://github.com/darkoperator/Posh-SSH)
   
  # Set the credentials
  $Password = ConvertTo-SecureString 'almighty1992' -AsPlainText -Force
  $Credential = New-Object System.Management.Automation.PSCredential ('sftpuser', $Password)
   
  # Set local file path and SFTP path
  $FilePath1 = &quot;D:\TEST.txt&quot;
  
  $SftpPath = '/shared/'
   
  # Set the Hostname of the SFTP server
  $SftpServer = '192.168.1.215'
   
  # Load the Posh-SSH module
  #Import-Module Posh-SSH
   
  # Establish the SFTP connection
  $ThisSession = New-SFTPSession -ComputerName $SftpServer -Credential $Credential -AcceptKey -Port 22
   
  # Upload the files to the SFTP path
  Set-SFTPFile -SessionId ($ThisSession).SessionId -Localfile $FilePath1 -RemotePath $SftpPath -Overwrite
  
   
  #Disconnect all SFTP Sessions
  Get-SFTPSession | % { Remove-SFTPSession -SessionId ($_.SessionId) }
  
  Add-Content D:\LOG.TXT &quot;Test&quot; 
  </code></pre>
  <p><strong>Code which i dont know how to add.
  If im right understand i need to check that file exist in source directory.</strong></p>
  <blockquote>
  <p>#check source file exist   Test-Path -Path D:\TEST.txt -PathType Leaf</p>
  <h1>lists sftp directory files after file was copied   $FilePath = Get-SFTPChildItem -sessionID $SFTPSession.SessionID -path $SftpPath</h1>
  <p>#For each file  ForEach ($LocalFile in $FilePath) {</p>
  <pre><code>if($LocalFile.name -eq &quot;.&quot; -Or $LocalFile.name -eq &quot;..&quot; )
  {
        Write-Host &quot;Files Ignored!&quot;
  }
  else
  {
      Write-Host $LocalFile
      Get-SFTPFile -SessionId $SFTPSession.SessionID -LocalPath $LocalPath -RemoteFile $localfile.fullname 
  </code></pre>
  <p>Force
  }</p>
  <p>}</p>
  </blockquote>