Powershell中的FTPS上传

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

我正在学习Powershell,我正在开发一个小脚本,每晚将一组文件上传到FTPS服务器。这些文件位于包含名称中日期的子目录中的网络共享上。文件本身都将以相同的字符串开头,让我们说“JONES_”。我有这个脚本适用于FTP,但我不知道我需要做什么才能让它适用于FTPS:

# Set yesterday's date (since uploads will happen at 2am)
$YDate = (Get-Date).AddDays(-1).ToString('MM-dd-yyyy')

#Create Log File
$Logfile = "C:\powershell\$YDate.log"
Function LogWrite
{
    Param ([string]$logstring)

    Add-Content $Logfile -value $logstring
}


# Find Directory w/ Yesterday's Date in name
$YesterdayFolder = Get-ChildItem -Path "\\network\storage\location" | Where-Object {$_.FullName.contains($YDate)}


If ($YesterdayFolder) {

    #we specify the directory where all files that we want to upload are contained 
    $Dir= $YesterdayFolder
    #ftp server
    $ftp = "ftp://ftps.site.com"
    $user = "USERNAME"
    $pass = "PASSWORD"

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


$FilesToUpload = Get-ChildItem -Path (Join-Path $YesterdayFolder.FullName "Report") | Where-Object {$_.Name.StartsWith("JONES","CurrentCultureIgnoreCase")}
foreach($item in ($FilesToUpload))
    { 
        LogWrite "Uploading file:  $YesterdayFolder\Report\$item"
        $uri = New-Object System.Uri($ftp+$item.Name) 
        $webclient.UploadFile($uri, $item.FullName)  
    }
    } Else {
        LogWrite "No files to upload"
    }

如果可能的话,我宁愿不必处理第三方软件解决方案。

powershell ftps
2个回答
1
投票

使用psftp并不适合我。我无法通过SSL连接到FTP。我最后(不情愿地?)使用WinSCP使用此代码:

$PutCommand = '& "C:\Program Files (x86)\WinSCP\winscp.com" /command "open ftp://USER:[email protected]:21/directory/ -explicitssl" "put """"' + $Item.FullName + '""""" "exit"' 
Invoke-Expression $PutCommand 

在foreach循环中。


1
投票

我不确定你是否会将其视为“第三方软件”,但你可以在Powershell中运行PSFTP。这是一个如何做到这一点的例子(source):

$outfile=$YesterdayFolder"\Report\"$item.Name
"rm $outfile`nput $outfile`nbye" | out-file batch.psftp -force -Encoding ASCII

$user = "USERNAME"
$pass = "PASSWORD"

&.\psftp.exe  -l $user -pw $pass  $ftp -b batch.psftp -be
© www.soinside.com 2019 - 2024. All rights reserved.