使用PowerShell覆盖ftp文件

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

问题是我的代码没有覆盖所有文件

我之前使用过这个脚本,它会上传文件并覆盖其中一些但不会覆盖所有这些文件。

# Get publishing profile for the web app
$webappname = "sib"
$resourceGroup = "sib2"
$appdirecotry = "c:\temp\sib"
$xml = [xml](Get-AzureRmWebAppPublishingProfile -Name $webappname `
-ResourceGroupName $resourceGroup `
-OutputFile null)
$xml = [xml]$xml
# Extract connection information from publishing profile
$username = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@userName").value
$password = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@userPWD").value
$url = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@publishUrl").value
Write-Host "Set a virtual application"
Set-Location $appdirectory
$webclient = New-Object -TypeName System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($username,$password)
$files = Get-ChildItem -Path $appdirectory -Recurse #| Where-Object{!($_.PSIsContainer)}
foreach ($file in $files)
{
    $relativepath = (Resolve-Path -Path $file.FullName -Relative).Replace('\', '/')
    $uri = New-Object System.Uri("$url/$relativepath")
    if($file.PSIsContainer)
    {
        $uri.AbsolutePath + "is Directory"
        $ftprequest = [System.Net.FtpWebRequest]::Create($uri);
        $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::MakeDirectory
        $ftprequest.UseBinary = $true
        #$ftprequest.UsePassive = $true
        #$ftprequest.KeepAlive = $false

        $ftprequest.Credentials = New-Object System.Net.NetworkCredential($username,$password)

        $response = $ftprequest.GetResponse();
        $response.StatusDescription
        continue
    }
    "Uploading to " + $uri.AbsoluteUri
    $webclient.UploadFile($uri, $file.FullName)
} 
$webclient.Dispose()

它不会覆盖所有文件

azure powershell teamcity
2个回答
0
投票

我必须在CI / CD过程中使用上面的代码。因此,如果它是第一次完成上述脚本是好的,但它在ftp的覆盖场景中无法正常工作。

如果我必须覆盖,我的脚本会被更改。所以我现在有2个脚本。一个用于第一次环境,一个用于覆盖环境

而不是覆盖脚本中的以下内容

Set-Location $appdirectory
$webclient = New-Object -TypeName System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($username,$password)
$files = Get-ChildItem -Path $appdirectory -Recurse #| Where-Object{!($_.PSIsContainer)}
     foreach ($file in $files)
    {
        $relativepath = (Resolve-Path -Path $file.FullName -Relative).Replace('\', '/')
        $uri = New-Object System.Uri("$url/$relativepath")
        if($file.PSIsContainer)
        {
            $uri.AbsolutePath + "is Directory"
            $ftprequest = [System.Net.FtpWebRequest]::Create($uri);
            $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::MakeDirectory
            $ftprequest.UseBinary = $true
            #$ftprequest.UsePassive = $true
            #$ftprequest.KeepAlive = $false

            $ftprequest.Credentials = New-Object System.Net.NetworkCredential($username,$password)

            $response = $ftprequest.GetResponse();
            $response.StatusDescription
            continue
        }
        "Uploading to " + $uri.AbsoluteUri
        $webclient.UploadFile($uri, $file.FullName)
    } 
    $webclient.Dispose()

我在覆盖脚本中使用以下内容

    Set-Location $appdirectory
$webclient = New-Object -TypeName System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($username,$password)
$files = Get-ChildItem -Path $appdirectory -Recurse | Where-Object{!($_.PSIsContainer)}
foreach ($file in $files)
{
    $relativepath = (Resolve-Path -Path $file.FullName -Relative).Replace(".\", "").Replace('\', '/')
    $uri = New-Object System.Uri("$url/$relativepath")
    "Uploading to " + $uri.AbsoluteUri
    $webclient.UploadFile($uri, $file.FullName)
} 
$webclient.Dispose()

以上工作就像覆盖场景的魅力。但是让我确认一下,这在第一次设置时不起作用。如果你第一次尝试这个,它会抛出550错误。


0
投票

这可能会更好。

function DeleteFtpFolder($url, $credentials)
{
    $listRequest = [Net.WebRequest]::Create($url)
    $listRequest.Method = [System.Net.WebRequestMethods+FTP]::ListDirectoryDetails
    $listRequest.Credentials = $credentials

    $lines = New-Object System.Collections.ArrayList

    $listResponse = $listRequest.GetResponse()
    $listStream = $listResponse.GetResponseStream()
    $listReader = New-Object System.IO.StreamReader($listStream)

    while (!$listReader.EndOfStream)
    {
        $line = $listReader.ReadLine()
        $lines.Add($line) | Out-Null
    }

    $listReader.Dispose()
    $listStream.Dispose()
    $listResponse.Dispose()

    foreach ($line in $lines)
    {
        $tokens = $line.Split(" ", 5, [System.StringSplitOptions]::RemoveEmptyEntries)

        $type = $tokens[2]
        $name = $tokens[3]
        $fileUrl = ($url + "/" + $name)

        if ($type -eq "<DIR>")
        {
            Write-Host "Found folder: $name"

            DeleteFtpFolder $fileUrl $credentials

            Write-Host "Deleting folder: $name"
            $deleteRequest = [Net.WebRequest]::Create($fileUrl)
            $deleteRequest.Credentials = $credentials
            $deleteRequest.Method = [System.Net.WebRequestMethods+FTP]::RemoveDirectory
            $deleteRequest.GetResponse() | Out-Null
        }
        else 
        {
            $fileUrl = ($url + "/" + $name)
            Write-Host "Deleting file: $name"

            $deleteRequest = [Net.WebRequest]::Create($fileUrl)
            $deleteRequest.Credentials = $credentials
            $deleteRequest.Method = [System.Net.WebRequestMethods+FTP]::DeleteFile
            $deleteRequest.GetResponse() | Out-Null
        }
    }
}

$credentials = New-Object System.Net.NetworkCredential($username, $password)
DeleteFtpFolder $url $credentials


Set-Location $appdirectory
$webclient = New-Object -TypeName System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($username,$password)
$files = Get-ChildItem -Path $appdirectory -Recurse #| Where-Object{!($_.PSIsContainer)}
foreach ($file in $files)
{
    $relativepath = (Resolve-Path -Path $file.FullName -Relative).Replace('\', '/')
    $uri = New-Object System.Uri("$url/$relativepath")
    if($file.PSIsContainer)
    {
        $uri.AbsolutePath + "is Directory"
        $ftprequest = [System.Net.FtpWebRequest]::Create($uri);
        $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::MakeDirectory
        $ftprequest.UseBinary = $true
        #$ftprequest.UsePassive = $true
        #$ftprequest.KeepAlive = $false

        $ftprequest.Credentials = New-Object System.Net.NetworkCredential($username,$password)

        $response = $ftprequest.GetResponse();
        $response.StatusDescription
        continue
    }
    "Uploading to " + $uri.AbsoluteUri
    $webclient.UploadFile($uri, $file.FullName)
} 
$webclient.Dispose()
© www.soinside.com 2019 - 2024. All rights reserved.