3

I need a PowerShell script to upload to an SFTP site to a specific port number. It is currently working with FTP and when not connecting on a specific port but how would I edit my script to let it connect to a specific port and use SFTP? See my script below:

#we specify the directory where files are located to upload to Jevon FTP
$Dir="E:\CMBPAID\BPAID_JM_1360493_customer01_20180803_011700.csv"    

#ftp server for Nest
$ftp = "sftp://ftp.dlptest.com/" 
$user = "dlpuser@dlptest.com" 
$pass = "e73jzTRTNqCN9PYAAjjn"  

$webclient = New-Object System.Net.WebClient 

$webclient.Credentials = New-Object 
System.Net.NetworkCredential($user,$pass)  

#list sql server trace file 
foreach($item in (dir $Dir "*.trc")) { 
    "Uploading $item..." 
    $uri = New-Object System.Uri($ftp+$item.Name) 
    $webclient.UploadFile($uri, $item.FullName) 
 } 
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
AJ26
  • 501
  • 4
  • 15
  • I add it by doing this: `$ftp = "sftp://psftp-nesam.ms.gxs.com:22/"` but I get the error `"Exception calling UploadFIle with 2 arguments An exception occurred during a WebClient request"` – AJ26 Aug 31 '18 at 00:58

1 Answers1

3

Longer example following this article from WinSCP:

$ErrorActionPreference = 'Stop'
Add-Type -Path "$path\WinSCPnet.dll"

$session = [WinSCP.Session]::new()
$session.Open(New-Object -TypeName WinSCP.SessionOptions -Property @{
    Protocol   = [WinSCP.Protocol]::Sftp
    HostName   = 'dlptest.com'
    UserName   = 'dlpuser@dlptest.com'
    Password   = 'plaintextpw'
    PortNumber = 6969
})

$transferOptions = New-Object -TypeName WinSCP.TransferOptions -Property @{
    TransferMode = [WinSCP.TransferMode]::Binary
}
foreach ($file in (Get-ChildItem -Path $path -Filter *.trc))
{
    "Uploading $file"

    $result = $session.PutFiles($file.FullName, '/', $false, $transferOptions)

    try
    {
        $result.Check()
    }
    catch
    {
        "Failed to upload file: $PSItem"
    }
}

$session.Dispose()

Your URI scheme needs to include the port number to change it from the default, i.e., ftp://address:port/

Here is your example condensed:

#requires -Version 5

$webClient = [System.Net.WebClient]::new()
$webClient.Credentials = [System.Net.NetworkCredential]::new('dlpuser@dlptest.com', 'plaintextpw')
foreach ($file in (Get-ChildItem -Path $path -Filter *.trc))
{
    "Uploading $file"
    $webClient.UploadFile("ftp://ftp.dlptest.com:6969/$file", $file.FullName)
}
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
  • Thank you but I realized I left out something. It should actually be `sftp` and not `ftp` Does that make any difference? I was reading somewhere that `sftp` does not work with `webclient` – AJ26 Aug 31 '18 at 01:06
  • 1
    @AdamNewman You would be correct. I highly recommend looking into the WinSCP tool. [Here's a direct link to the download](https://winscp.net/download/WinSCP-5.13.3-Automation.zip) that you can import into a powershell session using `Add-Type -Path` – Maximilian Burszley Aug 31 '18 at 01:07
  • 1
    @AdamNewman See my update. I've written the code necessary for your question using the previously linked library. – Maximilian Burszley Aug 31 '18 at 01:34