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

查看:39
本文介绍了使用 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 任务调度程序触发,在特定时间(星期二)运行,该脚本将抓取有问题的文件,将其上传到 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.

谢谢.

推荐答案

目前没有用于执行 SFTP 部分的内置 PowerShell 方法.您必须使用类似 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	est.txt"
$SftpPath = '/Outbox'
$SmbPath = '\filer01Backup'

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

# Load the Posh-SSH module
Import-Module C:TempPosh-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:Usersjon_dechiroDocumentsWindowsPowerShellModules),然后使用名称加载或将其放在任何位置并加载就像我在上面的代码中一样.
  • 如果无法接受脚本中的凭据,则必须使用凭据文件.如果您需要这方面的帮助,我可以更新一些详细信息或为您提供一些链接.
  • 根据需要更改路径、IP 等.

这应该会给你一个不错的起点.

That should give you a decent starting point.

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

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