-1

I have the below powershell script to do a simple file upload from a windows machine to a remote Linux server.

$DestServerIP = 'xx.xx.xx.xx'
$user = "user"
$pass = "xxxx"
$LocalPath = "\\windows_IP\d$\file.txt"
$RemotePath = '/tmp'

try
{
   $secpasswd = ConvertTo-SecureString $pass -AsPlainText -Force
   $credential = New-Object System.Management.Automation.PSCredential ($user, $secpasswd)
   # Opening a new SFTP session
   $session = New-SFTPSession -ComputerName $DestServerIP -Credential $credential -AcceptKey
   If (($session.Host -ne $DestServerIP) -or !($session.Connected)){
      Write-Host "SFTP server Connectivity failed..!" -ForegroundColor Red
      exit 1
   }
   Write-Host "Session established successfully" -ForegroundColor Green
   Write-Host "Started uploading: $LocalPath files to $RemotePath"

   # uploading all the .txt files to remote server:
   #Set-SFTPItem -SessionId $session.SessionId -Destination $RemotePath -Path ./file.txt
   #Set-SFTPItem -SessionId $session.SessionId -LocalFile $LocalPath -RemotePath $RemotePath -Overwrite -Force
   #Set-SFTPFile -SessionId $session.SessionId -Localfile "D:\file.txt" -RemotePath $RemotePath
   Set-SFTPFile -SessionId ($session).SessionId -LocalFile $LocalPath -RemotePath $RemotePath
   Write-Host "Files successfully uploaded"
} finally {
   Write-Host "Closing the connection" -ForegroundColor Green
   #Disconnect, clean up
   Remove-SFTPSession -SessionId $session.SessionId -Verbose | out-null
   exit 0
} catch {
   Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red
   exit 1
}

when I run the script using powershell -F scriptname.ps1 I get the following error

Set-SFTPFile : /tmp does not exist.
At D:\scriptname.ps1:76 char:4
+    Set-SFTPFile -SessionId ($session).SessionId -LocalFile $LocalPath ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (SSH.SftpSession:SftpSession) [Set-SFTPFile], SftpPathNotFoundExceptio
   n
    + FullyQualifiedErrorId : /tmp does not exist.,SSH.SetSftpFile

Files successfully uploaded
Closing the connection
VERBOSE: 0
VERBOSE: Removing session 0
VERBOSE: Session 0 Removed

I replaced it with Set-SFTPItem that also gives the same error

Ibrahim Quraish
  • 3,889
  • 2
  • 31
  • 39

1 Answers1

-1

The /tmp directory not available is due to the chroot jail user and when I given the available path, the code started working.

Ibrahim Quraish
  • 3,889
  • 2
  • 31
  • 39