如何使用Powershell将.Net Web应用程序部署到Azure

3

我在Azure上有一个“Web应用程序”,我使用Visual Studio部署/发布.Net Web应用程序(构建 -> 发布),并且它可以正常工作。

我希望能够使用Powershell脚本部署/发布我的应用程序。我已经编写了以下脚本来实现构建部分:

CMD> "c:\Program Files (x86)\MSBuild\14.0\bin\msbuild.exe" WebApplication1.sln

为了使其也能部署,我需要添加一些参数:
CMD> "c:\Program Files (x86)\MSBuild\14.0\bin\msbuild.exe" WebApplication1.sln /p:DeployOnBuild=true /p:PublishProfile="C:\Users\jgodse\Documents\Visual Studio 2015\Projects\WebApplication1\WebApplication1\Properties\PublishProfiles\jg-7-web-app-1 - Web Deploy.pubxml" /p:Configuration=Release

我遇到了一个错误:
Build FAILED.

"c:\Users\jgodse\Documents\Visual Studio 2015\Projects\WebApplication1\WebApplication1.sln" (default target) (1) ->
"c:\Users\jgodse\Documents\Visual Studio 2015\Projects\WebApplication1\WebApplication1\WebApplication1.csproj" (default target) (2) ->
(MSDeployPublish target) ->
  C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.Publishing.targets(4295,5): msdeploy error ERROR_USER_UNAUTHORIZED: Web deployment task failed. (Connected to the remote computer ("jg-7-web-app-1.scm.azurewebsites.net") using the Web Management Service, but could not authorize. Make sure that you are using the correct user name and password, that the site you are connecting to exists, and that the credentials represent a user who has permissions to access the site.  Learn more at: http://go.microsoft.com/fwlink/?LinkId=221672#ERROR_USER_UNAUTHORIZED.) [c:\Users\jgodse\Documents\Visual Studio 2015\Projects\WebApplication1\WebApplication1\WebApplication1.csproj]

    0 Warning(s)
    1 Error(s)

很显然我缺少Azure凭据(由于Visual Studio能够捕获它们),并且我也没有在Powershell中运行。

因此,我将整个命令放入名为deploy.bat的文件中,打开了一个Powershell窗口,并执行了以下操作:

PS>  Login-AzureRmAccount

我在GUI弹出窗口中输入了我的用户名/密码。

PS> cmd /c .\deploy.bat

构建过程顺利完成,但在尝试发布时出现了相同的错误。我猜测,在调用CMD程序时,Azure凭据并未传递。

我该如何使用PowerShell调用MSBuild来发布我的.NET项目到Azure Web应用程序?

1个回答

5
你可以使用现有的.pubxml文件,但需要密码才能进行部署。有几种方法可以获取密码。最明显的方法是通过导航到 Web 应用程序的刀片,然后单击“更多”,最后单击“获取发布配置文件”从门户获取它。

enter image description here

这个文件包含各种数据,但你需要的是userPWD,这是你需要使用的密码。复制密码并将其添加到你的MsBuild命令中:

CMD> "c:\Program Files (x86)\MSBuild\14.0\bin\msbuild.exe" WebApplication1.sln /p:DeployOnBuild=true /p:PublishProfile="C:\Users\jgodse\Documents\Visual Studio 2015\Projects\WebApplication1\WebApplication1\Properties\PublishProfiles\jg-7-web-app-1 - Web Deploy.pubxml" /p:Configuration=Release /p:Password="Value of userPWD"

显然,在构建脚本中存储此值并不推荐。你可以使用Powershell(Get-AzurePublishSettingsFile)下载发布设置,提取userPWD值,将其传递给MsBuild以发布你的应用程序,最后清理所有内容。

有各种实现构建基础设施的方法,我提出的可能不是最好的解决方案,但你可以尝试并决定哪种方法最适合你。

一些资源:

自动化一切(使用Azure构建实际应用程序) - 这个使用ASM而非资源管理器,但你可以轻松调整它,使其使用ARM。 使用Windows PowerShell脚本发布到开发和测试环境

希望这可以帮助您。


1
谢谢。那正是我所需要的。 - Jay Godse

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接