PowerShell SFTP上传到特定端口号

时间:2018-08-31 00:43:49

标签: powershell ftp sftp

我需要PowerShell脚本才能将SFTP网站上载到特定端口号。它目前可以在FTP上使用,并且当未在特定端口上连接时,但是如何编辑脚本以使其连接到特定端口并使用SFTP?参见下面的脚本:

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

1 个答案:

答案 0 :(得分:3)

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

您的URI方案需要包含端口号,才能将其更改为默认值,即ftp://address:port/

以下是您的示例摘要:

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