PowerShell SFTP上传到特定端口号

问题描述 投票:3回答:1

我需要一个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 = "[email protected]" 
$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) 
 } 
powershell ftp sftp
1个回答
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   = '[email protected]'
    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('[email protected]', 'plaintextpw')
foreach ($file in (Get-ChildItem -Path $path -Filter *.trc))
{
    "Uploading $file"
    $webClient.UploadFile("ftp://ftp.dlptest.com:6969/$file", $file.FullName)
}
© www.soinside.com 2019 - 2024. All rights reserved.