You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

PowerShell实现Windows与HP-Unix文件传输及UiPath自动化技术问询

PowerShell实现Windows与HP-Unix双向文件传输的最佳实践

作为UiPath自动化场景下的新手,你当前用plink的思路是对的,但确实有更适配PowerShell生态、更易维护的方案,下面分模块给你讲解:

一、更优的连接方式选择

相比单独使用plink,更推荐以下两种方式:

  • OpenSSH for Windows:微软官方推出的开源工具,完全集成PowerShell,支持标准的scpsftp命令,和Unix系统的SSH生态无缝兼容,稳定性和安全性更有保障。
  • Posh-SSH模块:专为PowerShell打造的SSH/SFTP管理模块,封装了很多自动化友好的API,不用拼接复杂的命令行参数,适合UiPath这类自动化流程调用。

二、推荐的PowerShell模块及安装方法

1. OpenSSH for Windows

这是首选方案,步骤如下:

  • 打开PowerShell(管理员权限),运行以下命令安装:
    Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
    
  • 安装完成后,直接就能使用scpsftp命令,和Unix端的用法几乎一致。

2. Posh-SSH模块

适合需要更灵活编程控制的场景,安装方式:

Install-Module -Name Posh-SSH -Force -AllowClobber

(注意:首次安装需要允许从PSGallery下载模块,按提示输入Y确认即可)

三、安全拷贝示例脚本

1. 使用OpenSSH的scp命令(双向传输)

Windows → HP-Unix上传文件

# 单个文件上传
scp "C:\local\path\file.txt" username@hp-unix-host:/remote/path/

# 整个文件夹上传(递归)
scp -r "C:\local\folder" username@hp-unix-host:/remote/directory/

HP-Unix → Windows下载文件

# 单个文件下载
scp username@hp-unix-host:/remote/path/file.txt "C:\local\destination\"

# 整个文件夹下载(递归)
scp -r username@hp-unix-host:/remote/directory/ "C:\local\destination\"

安全提示:自动化场景下不要用密码登录,推荐配置SSH密钥认证:

  1. 在Windows生成密钥对:ssh-keygen -t rsa(一路回车默认设置)
  2. 将生成的id_rsa.pub内容复制到HP-Unix服务器的~/.ssh/authorized_keys文件中
  3. 之后就可以免密执行scp命令了

2. 使用Posh-SSH模块的SFTP示例(更适合自动化)

初始化SSH连接

# 导入模块
Import-Module Posh-SSH

# 用密钥认证创建连接(推荐)
$sshKey = Get-Content "C:\Users\YourUser\.ssh\id_rsa" | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential("username", $sshKey)
$sftpSession = New-SFTPSession -ComputerName "hp-unix-host" -Credential $credential

Windows → HP-Unix上传文件

# 单个文件上传
Set-SFTPFile -SessionId $sftpSession.SessionId -LocalFile "C:\local\path\file.txt" -RemotePath "/remote/path/"

# 递归上传文件夹
Set-SFTPFile -SessionId $sftpSession.SessionId -LocalFile "C:\local\folder" -RemotePath "/remote/directory/" -Recurse

HP-Unix → Windows下载文件

# 单个文件下载
Get-SFTPFile -SessionId $sftpSession.SessionId -RemoteFile "/remote/path/file.txt" -LocalPath "C:\local\destination\"

# 递归下载文件夹
Get-SFTPFile -SessionId $sftpSession.SessionId -RemoteFile "/remote/directory/" -LocalPath "C:\local\destination\" -Recurse

关闭连接

Remove-SFTPSession -SessionId $sftpSession.SessionId

四、UiPath集成提示

在UiPath中调用这些脚本时:

  • 可以用PowerShell活动直接执行脚本片段
  • 注意处理会话的生命周期(比如用完及时关闭SFTP连接)
  • 密钥文件路径尽量用绝对路径,避免UiPath运行时的路径问题

内容的提问来源于stack exchange,提问作者Anijan

火山引擎 最新活动