在发布管道中使用ftp连接到Webapp以下载文件

时间:2020-10-27 08:08:06

标签: powershell ftp azure-powershell

我尝试使用以下powershell脚本连接到ftp:

#FTP Server Information - SET VARIABLES
$ftp = "ftp://XXX.com/" 
$user = 'UserName' 
$pass = 'Password'
$folder = 'FTP_Folder'
$target = "C:\Folder\Folder1\"

#SET CREDENTIALS
$credentials = new-object System.Net.NetworkCredential($user, $pass)

function Get-FtpDir ($url,$credentials) {
    $request = [Net.WebRequest]::Create($url)
    $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
    if ($credentials) { $request.Credentials = $credentials }
    $response = $request.GetResponse()
    $reader = New-Object IO.StreamReader $response.GetResponseStream() 
    while(-not $reader.EndOfStream) {
        $reader.ReadLine()
    }
    #$reader.ReadToEnd()
    $reader.Close()
    $response.Close()
}

#SET FOLDER PATH
$folderPath= $ftp + "/" + $folder + "/"

$files = Get-FTPDir -url $folderPath -credentials $credentials

$files 

$webclient = New-Object System.Net.WebClient 
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) 
$counter = 0
foreach ($file in ($files | where {$_ -like "*.txt"})){
    $source=$folderPath + $file  
    $destination = $target + $file 
    $webclient.DownloadFile($source, $target+$file)

    #PRINT FILE NAME AND COUNTER
    $counter++
    $counter
    $source
}

但是我在连接时不断出现530错误(身份验证错误)。我知道用户名和密码有效,因为我已经在其他ftp客户端中对其进行了测试。 因此,我认为这可能是一个问题,因为Web应用程序需要某种协议,而该脚本中未使用该协议。

我一直在做一些研究,发现可能叫做Posh-SSH的东西。但是有办法代替我的脚本吗?当我与winscp连接时,我将TLS / SSL隐式加密的FTP协议用于端口990。

更新:工作 我做了以下工作:

#FTP Server Information - SET VARIABLES
$ftp = "ftp://waws-prod-xxx.ftp.azurewebsites.windows.net" 
$user = 'xxxxx\xxxxx@email.com' 
$pass = '$FRnqxxpxxxxxxx'
$folder = 'site/wwwroot/wwwroot/images/uploaded'
$target = "C:\Folder\Folder1\"

#SET CREDENTIALS
$credentials = new-object System.Net.NetworkCredential($user, $pass)

function Get-FtpDir ($url,$credentials) {
    $request = [Net.WebRequest]::Create($url)
    $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
    if ($credentials) { $request.Credentials = $credentials }
    $response = $request.GetResponse()
    $reader = New-Object IO.StreamReader $response.GetResponseStream() 
    while(-not $reader.EndOfStream) {
        $reader.ReadLine()
    }
    #$reader.ReadToEnd()
    $reader.Close()
    $response.Close()
}

#SET FOLDER PATH
$folderPath= $ftp + "/" + $folder + "/"

$files = Get-FTPDir -url $folderPath -credentials $credentials

Write-Host($files)


$files 

$webclient = New-Object System.Net.WebClient 
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) 
$counter = 0
foreach ($file in ($files)){
    $source=$folderPath + $file  
    $destination = $target + $file 
    $webclient.DownloadFile($source, $target+$file)

    #PRINT FILE NAME AND COUNTER
    $counter++
    $counter
    $source
}

我最终向Web应用程序添加了新凭据,并将主机名中的sftp更改为ftp,现在可以使用了。也可以使用webpublish文件中的凭据。 enter image description here

我也使WinSCP正常工作,并且无法下载带有孩子的完整文件夹。

#FTP Server Information - SET VARIABLES
$ftp = "waws-prod-xxx-xxx.ftp.azurewebsites.windows.net" 
$user = 'xxxxxx\xxxx@xxxxx.no' 
$pass = '$xxxxxxxxx'
$folder = 'site/wwwroot/wwwroot/images/uploaded/*'
$target = "C:\Folder\Folder1\*"

# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Ftp
    HostName = $ftp
    UserName = $user
    Password = $pass 
}

$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.Open($sessionOptions)

    # Download files
    $session.GetFiles($folder, $target).Check()
}
finally
{
    # Disconnect, clean up
    $session.Dispose()
}    

1 个答案:

答案 0 :(得分:0)

FtpWebRequest(也不包含任何其他内置的.NET FTP API)不支持隐式TLS / SSL加密。
参见Does .NET FtpWebRequest Support both Implicit (FTPS) and explicit (FTPES)?


Posh-SSH是SSH / SFTP库。与FTP无关。


随着您收到“身份验证错误” ,我相信您的问题实际上有所不同。仔细检查您的凭据。您可能在代码中使用了错误的代码。

此外,如果WinSCP为您工作,则可以使用WinSCP .NET assembly from the PowerShell。根据您的工作GUI会话,WinSCP GUI甚至可以generate a code template for you