[英]Windows PowerShell Posh-SSH module script SFTP file-upload to Linux server giving error
我有下面的 powershell 腳本來執行從 windows 機器到遠程 Linux 服務器的簡單文件上傳。
$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
}
當我使用powershell -F scriptname.ps1運行腳本時,出現以下錯誤
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
我用Set-SFTPItem替換了它,它也給出了同樣的錯誤
/tmp目錄不可用是由於chroot jail用戶,當我給出可用路徑時,代碼開始工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.