2013-08-20 61 views
15

我需要編寫一個腳本來將文件從一個文件夾傳輸到另一個服務器(Linux)上,但傳輸文件的腳本在Windows上,我想知道是否有替代scp的PowerShell(或者如果有另一個這樣做的方式)是否存在用於PowerShell的SCP替代方案?

+0

對於任何使用PowerShell版本3或更高版本的人,https://github.com/darkoperator/Posh-SSH是一個選項。 –

回答

7

pscp.exe是一個可行的選擇,但我一直使用庫從Rebex了幾年現在SFTP和FTPS接送我在C#應用程序和PowerShell腳本中取得巨大成功。他們的軟件包還包括an SCP object,但我沒有親自使用它。

它確實需要花費,而pscp是免費的。在選擇Rebex軟件包之前,我考慮過使用PuTTY路線,但是我的團隊決定從長遠來看,有一個我們可以輕鬆放入任何應用/腳本的庫是值得的。

+0

如果你有它的預算,是的:-) –

+0

此博客文章可以幫助您獲得Rebex SFTP在PowerShell中運行http://blog.rebex.net/news/archive/2008/ 9月25日/如何使用的-FTP-或-SFTP功能於PowerShell.aspx –

16

有一個方便的小工具,Putty稱爲pscp.exe這將做到這一點,並可以在PowerShell容易調用。

下面的示例將從Windows複製到CentOS盒子(以用戶代碼「bill」登錄),並使用pscp中的-pw開關傳遞密碼(否則,產生的命令窗口將提示輸入Linux密碼):

Start-Process 'C:\Program Files (x86)\PuTTY\pscp.exe' -ArgumentList ("-scp -pw password C:\Document.rtf [email protected]:/home/bill/") 

PuTTY Secure Copy client 
Release 0.62 
Usage: pscp [options] [[email protected]]host:source target 
     pscp [options] source [source...] [[email protected]]host:target 
     pscp [options] -ls [[email protected]]host:filespec 
Options: 
    -V  print version information and exit 
    -pgpfp print PGP key fingerprints and exit 
    -p  preserve file attributes 
    -q  quiet, don't show statistics 
    -r  copy directories recursively 
    -v  show verbose messages 
    -load sessname Load settings from saved session 
    -P port connect to specified port 
    -l user connect with specified username 
    -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 authentication 
    -noagent disable use of Pageant 
    -agent enable use of Pageant 
    -batch disable all interactive prompts 
    -unsafe allow server-side wildcards (DANGEROUS) 
    -sftp  force use of SFTP protocol 
    -scp  force use of SCP protocol 
+1

使用基於密鑰的身份驗證,而不是將密碼放在腳本中。 – slestak

+0

公平點,這只是一個快速示例,表明PSCP可以完成所要求的工作:-) –

+0

Windows noob here。當我這樣做時,我會得到一個空白的控制檯窗口,大約10秒鐘後消失,並且SCP傳輸似乎沒有發生。關於可能發生什麼的任何想法? – jayhendren

1

還有一個 「.NET友好」 的方式:

可以使用SharpSSH DLL來執行SSH命令,並做SCP/SFTP tranfers。

例如:

[Reflection.Assembly]::LoadFrom((Resolve-Path .\Tamir.SharpSSH.dll)) 

$ssh = New-Object Tamir.SharpSsh.Sftp("server","user","password") 

$ssh.Connect() 

$ssh.Put("C:\localfile","distantfile") 

$ssh.Close() 

還有就是SSH.Net庫中,也近似做同樣的事情。

相關問題