Anijan
Anijan

Reputation: 11

PowerShell - File transfer from windows to unix

I am working on UIpath automation for which I need some files to be transferred back and forth between Windows and Unix machines (only through PowerShell). Kindly provide your inputs as I'm a newbie.

  1. I am using plink in my PowerShell script to connect to a Unix server. Though it works fine, is there any other better way to connect to a Unix server (HP UX) from Windows (through a PowerShell script).

  2. Struggling to find a good module and sample scripts to do a secure copy between the Unix and Windows servers. I came across Posh SSH /WinSCP, sftp etc. but I'm not able to implement any as I do not find the right sample scripts. Also Install-Module does not work (not recognized).

Your help on this would be much appreciated.

Thanks in advance!

Upvotes: 1

Views: 4017

Answers (2)

lit
lit

Reputation: 16266

Once you get Posh-SSH installed, something like this will probably get you down the road. There are ways to keep the password in plain text out of your script.

$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

Upvotes: 0

StefTheo
StefTheo

Reputation: 449

If you want to use SFTP I am using the code below to upload some files automatically to an ftp site:

First of all you have to download the winscp SFTP powershell libraries. https://winscp.net/eng/download.php then extract the contents at the same location the script is located.

Then in your script you must add:

# 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"

Then after the session with those credentials is up you must put your next step of copying the files.

# 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()
}

Probably there is an easier way with Power Shell 6 that can do more with the Unix/Linux operating systems but at this point of answering I haven't used it.

Upvotes: 1

Related Questions