使用PowerShell将文件上传到SFTP

问题描述:

我们被要求设置从我们的一台服务器到SFTP站点的自动上传。每个星期一早上将有一个文件从数据库导出到文件管理器,他们希望文件在星期二上传到SFTP。我们正在使用的当前身份验证方法是用户名和密码(我相信有一个选项也有密钥文件,但用户名/密码选项被选中)。使用PowerShell将文件上传到SFTP

我设想的方式是让一个脚本坐在服务器上,由Windows任务计划程序在特定时间(星期二)运行,以便抓取相关文件将其上传到SFTP,然后将其移至其他位置以备用途。

例如:

  • 本地目录:C:\FileDump

  • SFTP目录:/Outbox/

  • 备份目录:C:\Backup

我试过几件事情在这个点WinSCP是其中之一,以及SFTP PowerShell Snap-In,但到目前为止没有为我工作。

这将在Windows Server 2012R2上运行。
当我运行Get-Host我的控制台主机版本是4.0。

谢谢。

+0

我有一个FTP脚本使用System.Net.WebRequestMethods + FTP 我用 http://www.thomasmaurer.ch/2010/11/powershell-ftp-upload-and-download/ 作为基地。如果您需要更多帮助,我可以清理我的代码以供公开发布。 –

+0

@GlenBuktenica问题是关于SFTP,而不是FTP。 –

当前没有用于执行SFTP部分的内置PowerShell方法。您必须使用像psftp.exe或PowerShell模块(如Posh-SSH)。

这里使用一个例子Posh-SSH

# Set the credentials 
$Password = ConvertTo-SecureString 'Password1' -AsPlainText -Force 
$Credential = New-Object System.Management.Automation.PSCredential ('root', $Password) 

# Set local file path, SFTP path, and the backup location path which I assume is an SMB path 
$FilePath = "C:\FileDump\test.txt" 
$SftpPath = '/Outbox' 
$SmbPath = '\\filer01\Backup' 

# Set the IP of the SFTP server 
$SftpIp = '10.209.26.105' 

# Load the Posh-SSH module 
Import-Module C:\Temp\Posh-SSH 

# Establish the SFTP connection 
New-SFTPSession -ComputerName $SftpIp -Credential $Credential 

# Upload the file to the SFTP path 
Set-SFTPFile -SessionId 0 -LocalFile $FilePath -RemotePath $SftpPath 

# Disconnect SFTP session 
(Get-SFTPSession -SessionId 0).Disconnect() 

# Copy the file to the SMB location 
Copy-Item -Path $FilePath -Destination $SmbPath 

一些其他注意事项:

  • 你必须下载辣妹-SSH模块,可以安装到你的用户模块目录(如C:\ Users \ jon_dechiro \ Documents \ WindowsPowerShell \ Modules),只需使用该名称进行加载或将其放在任何位置并像我在上面的代码中那样加载它。
  • 如果在脚本中拥有凭据是不可接受的,则必须使用凭证文件。如果您需要帮助,我可以更新一些细节或指向一些链接。
  • 根据需要更改路径,IP等。

这应该给你一个体面的起点。

使用PuTTY's pscp.exe(我有一个$env:path目录):

pscp -sftp -pw passwd c:\filedump\* [email protected]:/Outbox/ 
mv c:\filedump\* c:\backup\* 

你没告诉我们你有什么特别的问题与WinSCP赋予,所以我真的只重复什么是WinSCP赋予文档。

  • Download WinSCP .NET assembly
    截至目前最新的包装是WinSCP-5.13-Automation.zip;
  • Extract.zip沿您的脚本存档;
  • 使用这样的代码(根据官方公布的PowerShell upload example):

    # Load WinSCP .NET assembly 
    Add-Type -Path "WinSCPnet.dll" 
    
    # Setup session options 
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ 
        Protocol = [WinSCP.Protocol]::Sftp 
        HostName = "example.com" 
        UserName = "user" 
        Password = "mypassword" 
        SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" 
    } 
    
    $session = New-Object WinSCP.Session 
    
    try 
    { 
        # Connect 
        $session.Open($sessionOptions) 
    
        # Upload 
        $session.PutFiles("C:\FileDump\export.txt", "/Outbox/").Check() 
    } 
    finally 
    { 
        # Disconnect, clean up 
        $session.Dispose() 
    } 
    

你可以有WinSCP赋予生成上传你的PowerShell脚本:

  • 用WinSCP GUI登录到你的服务器;
  • 导航到远程文件面板中的目标目录;
  • 在本地文件面板中选择要上传的文件;
  • 调用上传命令;
  • Transfer options dialog,去传输设置>生成代码;
  • 在Generate transfer code对话框中,选择.NET assembly code tab;
  • 选择PowerShell的语言。

你会得到一个类似的代码上面填写所有的会话和传送设置。

Generate transfer code dialog

(我的WinSCP的作者)

+1

昨天我试图找出这些东西的问题。然后我找到了你的帖子。这有助于很多! 我在短短几分钟内就完成了我的测试工作,而且这一切似乎都能奏效。那是一个超级漂亮的功能,先生。 有时你需要的只是一个很好的例子。 –

+0

哇,很好的信息,我没有意识到WinSCP有这个功能!我爱你的软件:)一种唠叨的问题,如果WinSCP可以允许更多的连接过程自定义,那将是美好的。我可以使用PLINK将SCP文件发送到我的思科设备,因为直接SCP只需要进行一次认证,但如果不使用只需要密码作为响应的“En”就无法连接并获得DIR,而且我不能弄清楚如何设置WinSCP来做到这一点。也许我们有一天可以更好地控制使用XMLS的连接过程? –

+0

WinSCP就像我的sftp问题的冠军一样工作 – digitalslavery