使用PowerShell将文件上传到SFTP [英] Upload file to SFTP using PowerShell

查看:1075
本文介绍了使用PowerShell将文件上传到SFTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We were asked to set up an automated upload from one of our servers to an SFTP site. There will be a file that is exported from a database to a filer every Monday morning and they want the file to be uploaded to SFTP on Tuesday. The current authentication method we are using is username and password (I believe there was an option to have key file as well but username/password option was chosen).

我设想的方式是让脚本坐在服务器上,该脚本将由Windows Task Scheduler触发,以在特定时间(星期二)运行,该脚本将抓取有问题的文件上传到SFTP,然后移动备份到其他位置.

The way I am envisioning this is to have a script sitting on a server that will be triggered by Windows Task scheduler to run at a specific time (Tuesday) that will grab the file in question upload it to the SFTP and then move it to a different location for backup purposes.

例如:

  • 本地目录:C:\FileDump

SFTP目录:/Outbox/

备份目录:C:\Backup

在这一点上,我尝试了几件事WinSCP成为其中之一,并且 SFTP PowerShell管理单元,但到目前为止,对我来说没有任何帮助.

I tried few things at this point WinSCP being one of them as well as SFTP PowerShell Snap-In but nothing has worked for me so far.

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

This will be running on Windows Server 2012R2.
When I run Get-Host my console host version is 4.0.

谢谢.

推荐答案

当前尚无内置的PowerShell方法用于执行SFTP部分.您将必须使用诸如psftp.exe之类的东西或诸如Posh-SSH之类的PowerShell模块.

There isn't currently a built-in PowerShell method for doing the SFTP part. You'll have to use something like psftp.exe or a PowerShell module like Posh-SSH.

以下是使用豪华的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
$ThisSession = New-SFTPSession -ComputerName $SftpIp -Credential $Credential

# Upload the file to the SFTP path
Set-SFTPFile -SessionId ($ThisSession).SessionId -LocalFile $FilePath -RemotePath $SftpPath

#Disconnect all SFTP Sessions
Get-SFTPSession | % { Remove-SFTPSession -SessionId ($_.SessionId) }

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

一些附加说明:

  • 您必须下载Posh-SSH模块,您可以将其安装到用户模块目录(例如C:\ Users \ jon_dechiro \ Documents \ WindowsPowerShell \ Modules)中,然后使用名称进行加载或将其放置在任何位置进行加载就像我在上面的代码中一样.
  • 如果脚本中的凭据不可接受,则必须使用凭据文件.如果您需要帮助,我可以提供一些详细信息或为您提供一些链接.
  • 根据需要更改路径,IP等.

这应该为您提供一个不错的起点.

That should give you a decent starting point.

这篇关于使用PowerShell将文件上传到SFTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆