我们被要求设置从我们的一台服务器到SFTP站点的自动上传。每个星期一早上都会有一个文件从数据库导出到文件管理器,他们希望文件在星期二上载到SFTP。我们当前使用的身份验证方法是用户名和密码(我相信也可以选择拥有密钥文件,但是选择了用户名/密码选项)。
我设想的方法是让脚本坐在服务器上,该脚本将由Windows Task Scheduler触发,以在特定时间(星期二)运行,该脚本将抓取有问题的文件上传到SFTP,然后将其移动到用于备份的不同位置。
例如:
本地目录: C:\FileDump
SFTP目录: /Outbox/
备份目录: C:\Backup
在这一点上,我尝试了几件事,但WinSCP和SFTP PowerShell Snap-In都是其中之一,但是到目前为止,对我来说没有任何作用。
这将在Windows Server 2012R2上运行。
当我运行Get-Host
控制台时,主机版本为4.0。
谢谢。
当前没有内置的PowerShell方法可以执行SFTP部分。您将必须使用诸如psftp.exe之类的东西或诸如Posh-SSH之类的PowerShell模块。
这是使用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
$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
一些其他注意事项:
那应该给您一个体面的起点。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句