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

标签 powershell azure msbuild azure-web-app-service credentials

我在 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 在 .Net 项目上调用 MSBuild 以发布到 Azure Web 应用?

最佳答案

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

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="userPWD 的值"

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

有多种方法可以实现您构建基础架构,我提出的方法可能不是最适合您的解决方案,但您可以尝试并决定最适合您的解决方案。

一些资源:

Automate Everything (Building Real-World Cloud Apps with Azure) - 这个使用 ASM 而不是资源管理器,但您可以轻松调整它以使用 ARM Using Windows PowerShell scripts to publish to dev and test environments

希望这有帮助。

关于powershell - 如何使用 Powershell 将 .Net Web 应用程序部署到 Azure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40685994/

相关文章:

powershell - 通过 PowerShell 远程处理访问 Azure 云服务 PaaS 实例

c# - 跨多个 SP 的 SAML 身份验证

msbuild - 如何从 MSBuild 中的 ItemGroup 构建 PropertyGroup 条目?

visual-studio-2008 - MSBuild,使用不同的配置构建依赖项目

powershell - Powershell输出格式?

loops - 脚本的循环部分

powershell - 调用 WebRequest : Accept a cookie

c# - 如何在 Azure Function App 中运行时更新应用程序设置中的单个值

azure - 如何在 Azure Devops 中为多模块 Maven 创建构建管道

visual-studio - 即使发布成功,发布配置文件中的构建目标也不会执行