Need a Powershell script

SR Zak
SR Zak used Ask the Experts™
on
Hello Experts,

I need help to get this script converted to Powershell or VBS script, I have winscp that connects to a remote directory and synchronize files. I don't have to even synchronize but I just want to establish the connection, below is the script.

I downloaded the winscpnet.dll as per the WinSCP site , this will work with powershell. Below is the batchfile that I want to convert to powershell.

option batch abort
option confirm off
open sftp://sftpuser04:password123!@lynn-vs2.cloudapp.net -hostkey="ssh-rsa 2048 d5:71:6a:0d:60:99:23:c4:63:18:55:54:5a:fb:79:05"
synchronize both C:\Temp\Target /home/sftpuser04/Source
exit

Thank you
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dustin SaundersCo-Founder and Chief Architect
Top Expert 2016

Commented:
The website has examples:
https://winscp.net/eng/docs/library_powershell#example

Essentially:
$ErrorActionPreference = 'Stop'
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"

$session = [WinSCP.Session]::new()

$config = New-Object -TypeName WinSCP.SessionOptions -Property @{
    Protocol   = [WinSCP.Protocol]::Sftp
    HostName   = 'host.name'
    UserName   = 'sftpusername'
    Password   = 'password'
    PortNumber = 6969
    SshHostKeyFingerprint = 'ssh-rsa 2048 d5:71:6a:0d:60:99:23:c4:63:18:55:54:5a:fb:79:05'
}

$session.Open($config)

$transferOptions = New-Object -TypeName WinSCP.TransferOptions -Property @{
    TransferMode = [WinSCP.TransferMode]::Binary
}

$session.Dispose()

Open in new window


More on converting batch to Powershell:
https://winscp.net/eng/docs/library_from_script

More Powershell examples:
https://winscp.net/eng/docs/library_examples
SR ZakNetwork Solutions

Author

Commented:
My issue is how to make the script work when my home location and is using UNC path ?
Jose Gabriel Ortega CastroTop Rated Freelancer on MS Technologies
Awarded 2018
Distinguished Expert 2018

Commented:
I don't think that's actually a problem just try it on powershell on your computer and report back if you are having issues.

C:\Temp\Target /home/sftpuser04/Source

C:\Temp\Target  UPN

If that doesn't work you would need to use local addresses.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

SR ZakNetwork Solutions

Author

Commented:
I am getting this error, do you know what I need to change on it.

Method invocation failed because [WinSCP.Session] does not contain a method named 'new'.
At C:\Script\Script3.ps1:4 char:1
+ $session = [WinSCP.Session]::new()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MethodNotFound
SR ZakNetwork Solutions

Author

Commented:
The template provided on winscp do not seem to work.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
You need to run a New-Object for the session object instead of the new() method.
$session = New-Object WinSCP.Session

Open in new window

Co-Founder and Chief Architect
Top Expert 2016
Commented:
What version of the DLL do you have?  Could be older/missing a method.  The sample code works fine for me:

winscp.png
(Ignore the $sessuib part, that was a typo)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial