powershell上传文件到服务器,使用PowerShell将文件上传到SFTP

当前没有用于执行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等。

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值