【发布时间】:2019-02-15 21:48:15
【问题描述】:
问题是我的代码没有覆盖所有文件
我之前使用过这个脚本,它确实会上传文件并覆盖其中一些文件,但不会覆盖所有文件。
# 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()
它不会覆盖所有文件
【问题讨论】:
-
WebClient.UploadFile 方法在内部使用了FTP STOR 命令。因此它会理想地替换文件。您是否可以尝试添加异常处理块或日志,只是为了查看您的 URI 是否正确,因为有时通过组合 BaseAddress 生成的 URI 是无效的,它会引发 WebException。
-
Mohit 我试过了。它会覆盖大部分文件。但很少有文件没有被覆盖
-
另外,它里面有目录,所以它首先通过一个不可用的错误文件创建目录,然后在目录之后,它会抛出登录问题,然后再次登录并上传剩余的文件。
-
问题是那些目录已经存在。所以这就是它抛出异常的原因。
标签: azure powershell teamcity