2016-11-18 232 views
2

我在Azure中使用Visual Studio部署/發佈.Net Web應用程序的「Web應用程序」。 (生成 - >發佈),它的工作原理。如何使用Powershell將.Net Web應用程序部署到Azure

我希望能夠使用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 

構建沒問題,但嘗試發佈時出現同樣的錯誤。我猜這些Azure證書在CMD程序中退出時並未實現。

如何使用Powershell在我的.Net項目上調用MSBuild以發佈到Azure Web應用程序?

回答

4

您可以使用現有的.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:密碼=不推薦「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

希望這有助於。

+1

謝謝。那正是我需要的。 –

相關問題