问题是我的代码没有覆盖所有文件
我以前使用过此脚本,它确实上传了文件并覆盖了其中的一些文件,但并未覆盖所有文件。
# 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()
它不会覆盖所有文件
答案 0 :(得分: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错误。
答案 1 :(得分: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()