title: How to Deploy a .Net web app to Azure using Powershell
body: <p>I have a "Web app" in Azure to which I deploy/publish a .Net web application using Visual Studio. (Build --> Publish), and it works. </p>
  
  <p>I want to be able to deploy/publish my application using a Powershell script. I got the following script to work for the build portion:</p>
  
  <pre><code>CMD&gt; "c:\Program Files (x86)\MSBuild\14.0\bin\msbuild.exe" WebApplication1.sln
  </code></pre>
  
  <p>To make it also deploy, I need to add a few parameters:</p>
  
  <pre><code>CMD&gt; "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
  </code></pre>
  
  <p>I got an error:</p>
  
  <pre><code>Build FAILED.
  
  "c:\Users\jgodse\Documents\Visual Studio 2015\Projects\WebApplication1\WebApplication1.sln" (default target) (1) -&gt;
  "c:\Users\jgodse\Documents\Visual Studio 2015\Projects\WebApplication1\WebApplication1\WebApplication1.csproj" (default target) (2) -&gt;
  (MSDeployPublish target) -&gt;
    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)
  </code></pre>
  
  <p>I am obviously missing my Azure credentials (seeing as Visual Studio was able to capture them), and I am also not running in Powershell. </p>
  
  <p>So I took the entire command and put it into a file called <strong>deploy.bat</strong>, opened up a Powershell window and did the following:</p>
  
  <pre><code>PS&gt;  Login-AzureRmAccount
  </code></pre>
  
  <p>(I typed in my user/password in the GUI popup).</p>
  
  <pre><code>PS&gt; cmd /c .\deploy.bat
  </code></pre>
  
  <p>The build was fine, but I got the same error when trying to publish. I guess that the Azure credentials did not carry through when shelling out to the CMD program. </p>
  
  <p>How do I use Powershell to call MSBuild on my .Net project to publish to an Azure web app?</p>