PowerShell-从windows到unix的文件传输
我正在进行UIpath自动化,为此我需要在Windows和Unix机器之间来回传输一些文件(仅通过PowerShell)。请提供您的意见,因为我是新手PowerShell-从windows到unix的文件传输,powershell,file-transfer,Powershell,File Transfer,我正在进行UIpath自动化,为此我需要在Windows和Unix机器之间来回传输一些文件(仅通过PowerShell)。请提供您的意见,因为我是新手 我正在PowerShell脚本中使用plink连接到Unix服务器。尽管它工作正常,但有没有其他更好的方法从Windows(通过PowerShell脚本)连接到Unix服务器(HP UX)呢 正在努力寻找一个好的模块和示例脚本,以便在Unix和Windows服务器之间进行安全复制。我遇到了漂亮的SSH/WinSCP、sftp等,但由于没有找到正确
plink
连接到Unix服务器。尽管它工作正常,但有没有其他更好的方法从Windows(通过PowerShell脚本)连接到Unix服务器(HP UX)呢安装模块
不工作(无法识别)提前谢谢 如果您想使用SFTP,我将使用下面的代码将一些文件自动上载到ftp站点: 首先,您必须下载winscp SFTP powershell库。 然后在脚本所在的同一位置提取内容 然后,您必须在脚本中添加:
# Load WinSCP .NET assembly
# Give the path the dll file is located so powershell can call it.
Add-Type -Path "C:\Path where the dll file is located\WinSCPnet.dll"
# Setup session options
# Add all the properties the session needs to be established such as username password hostname and fingerprint.
# The username and password must be in plaintext.
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "HostName"
UserName = "UserName"
Password = "Password"
SshHostKeyFingerprint = "SSH fingerprint"
然后,在使用这些凭据的会话结束后,您必须执行复制文件的下一步
# Open a session to the Host
# Try to connect to the Host with the credentials and information provided previously.
# Upload the file from a specific path to the path on the host.
# And then close the session and clean up the session trace data.
# $session.Dispose() -> If session was opened, closes it, terminates underlying WinSCP process, deletes XML log file and disposes object.
$session = New-Object WinSCP.Session
Try
{
# Connect to the SFTP site
$session.Open($sessionOptions)
# Upload the files from local disk to the destination
$session.PutFiles("Path of the file you want to upload", "/import/").Check()
}
Finally
{
# Disconnect, clean up
$session.Dispose()
}
使用Power Shell 6可能有一种更简单的方法,可以在Unix/Linux操作系统上做更多的工作,但在回答这个问题时,我还没有使用它。一旦安装了漂亮的SSH,类似的东西可能会让您走上正轨。有几种方法可以将明文密码保留在脚本之外
$SecurePassword = ConvertTo-SecureString -AsPlainText 'thepassword' -Force
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'me',$SecurePassword
$Session = New-SFTPSession -ComputerName 'zenith' -Credential $Credentials -ConnectionTimeout 30
$r = Set-SFTPFile -Session $Session. -LocalFile 'C:\Users\me\t.txt' -RemotePath '/home/me' -Overwrite
Remove-SFTPSession -SFTPSession $session | Out-Null
PowerShell 5提供
安装模块
。升级是可能的。另请参见中的答案