【问题标题】:Upload/Publish WebApp Files to Azure via PowerShell通过 PowerShell 将 WebApp 文件上传/发布到 Azure
【发布时间】:2016-06-13 10:43:48
【问题描述】:

您好,我目前在更新用于 Azure 的旧 PowerShell 脚本时遇到问题。它最初是为了在询问用户几个问题后部署网站而编写的。这很简单,您可以通过 New-AzureWebsite 创建一个新网站,然后 Publish-AzureWebsite 上传文件。

我现在正在使用 New-AzureRmWebApp 但不知道如何上传文件,没有为此的 Publish 命令,并且 Set-AzureRmWebApp 命令没有涵盖此问题的参数。

有人知道这是否还能做到吗?

谢谢,

【问题讨论】:

    标签: powershell azure


    【解决方案1】:

    使用资源管理器似乎没有那么简单,但是您可以使用 VS 生成的 Publish-AzureWebsite.ps1。在此处查看详细信息: https://azure.microsoft.com/en-us/documentation/articles/vs-azure-tools-publishing-using-powershell-scripts/

    关于自动配置和部署的更深入的文章: http://www.asp.net/aspnet/overview/developing-apps-with-windows-azure/building-real-world-cloud-apps-with-windows-azure/automate-everything#resources

    【讨论】:

    • 那太好了,如果他们提供与他们习惯相同的命令会更好。
    【解决方案2】:

    我知道这是一个老问题,但想分享一下我的经验。

    在资源管理器中肯定没有那么简单 - 常规方法(例如上传 zip 文件)已被自动化和与 GitHub 等其他服务的集成甚至与 Dropbox 同步所取代 - 您还可以与本地Git 版本,从技术上讲,您可以从您的计算机执行 git push 到“本地”(即站点/应用程序服务的本地)Git 存储库。然后,该 Git 存储库会从您的推送中部署该站点。

    如果您想要简单,也可以使用 FTP 解决方案,例如 curl 和 wget。

    Web Deploy (msdeploy.exe) 等其他解决方案需要安装软件,特别是 Visual Studio。

    所有上述部署方法的信息都在这里列出:https://docs.microsoft.com/en-us/azure/app-service-web/web-sites-deploy

    我最终使用的解决方案是一个折中方案——Azure 应用服务的部署引擎叫做 Kudu。正是这项服务实现了本地 Git 存储库推送,以便从您的 Git 推送部署您的站点,但它可以做更多的事情。

    Kudu has an API 我偶然发现了一个 PowerShell 脚本,它允许直接从您的计算机上传目录,以便使用 Kudu 部署到站点。该脚本最初是为与 ASM 一起使用而编写的,因此我进行了一些小改动以使其能够与 Resource Manager Web 应用程序集成。

    为确保原作者得到认可,要点在这里:https://gist.github.com/lwsrbrts/a2c9bfe1949ea0ebe34b6c6d5c0b11b6

    那些讨厌发布链接的人的代码:

    Param(
    [Parameter(Mandatory = $true)]
    [string]$websiteName,
    [Parameter(Mandatory = $true)]
    [string]$resourceGroupName,
    [Parameter(Mandatory = $true)]
    [string]$sourceDir,
    [string]$destinationPath = "/site/wwwroot"
    )
    
    # Usage: .\kuduSiteUpload.ps1 -websiteName mySite -sourceDir C:\Temp\mydir -resourceGroupName myResourceGroup
    
    Function d3-KuduUploadDirectory
    {
    param( 
        [string]$siteName = $( throw "Missing required parameter siteName"),
        [string]$sourcePath = $( throw "Missing required parameter sourcePath"),
        [string]$destinationPath = $( throw "Missing required parameter destinationPath"),
        [string]$resourceGroupName = $( throw "Missing required parameter resourceGroupName")
    )
    
    $zipFile = [System.IO.Path]::GetTempFileName() + ".zip"
    
    d3-ZipFiles -zipfilename $zipFile -sourcedir $sourcePath
    
    d3-KuduUploadZip -siteName $siteName -sourceZipFile $zipFile -destinationPath $destinationPath -resourceGroupName $resourceGroupName
    }
    
    Function d3-KuduUploadZip
    {
    param( 
        [string]$siteName = $( throw "Missing required parameter siteName"),
        [string]$sourceZipFile = $( throw "Missing required parameter sourceZipFile"),
        [string]$destinationPath = $( throw "Missing required parameter destinationPath"),
        [string]$resourceGroupName = $( throw "Missing required parameter resourceGroupName")
    
    )
    
    [xml]$publishSettings = Get-AzureRmWebAppPublishingProfile -Format WebDeploy -OutputFile .\Temp.publishsettings -ResourceGroupName $resourceGroupName -Name $siteName
    $website = $publishSettings.SelectSingleNode("//publishData/publishProfile[@publishMethod='MSDeploy']")
    
    $timeOutSec = 900
    
    $username = $webSite.userName
    $password = $webSite.userPWD
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
    
    $baseUrl = "https://" + $siteName + ".scm.azurewebsites.net"
    $apiUrl = d3-JoinParts ($baseUrl, "api/zip", $destinationPath) '/'
    
    Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method PUT -InFile $sourceZipFile -ContentType "multipart/form-data" -TimeoutSec $timeOutSec
    }
    
    Function d3-JoinParts {
    param ([string[]] $Parts, [string] $Separator = '/')
    
    # example:
    #  d3-JoinParts ('http://mysite','sub/subsub','/one/two/three') '/'
    
    $search = '(?<!:)' + [regex]::Escape($Separator) + '+'  #Replace multiples except in front of a colon for URLs.
    $replace = $Separator
    ($Parts | ? {$_ -and $_.Trim().Length}) -join $Separator -replace $search, $replace
    }
    
    Function d3-ZipFiles
    {
    Param(
        [Parameter(Mandatory = $true)]
        [String]$zipfilename,
        [Parameter(Mandatory = $true)]
        [String]$sourcedir
    )
    
    Add-Type -Assembly System.IO.Compression.FileSystem
    $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
    [System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir, $zipfilename, $compressionLevel, $false)
    }
    
    $startTime = Get-Date
    d3-KuduUploadDirectory -siteName $websiteName -sourcePath $sourceDir -destinationPath $destinationPath -resourceGroupName $resourceGroupName
    $finishTime = Get-Date
    Write-Host (" Total time used (minutes): {0}" -f ($finishTime -$startTime).TotalMinutes)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-30
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-21
      • 1970-01-01
      相关资源
      最近更新 更多