使用 WinRM IIS Web App Management + AppCmd 管理 IIS 站台

之前有提到使用 IIS WebSite Deployment 來部署 Web 應用程式,不過它不知道在哪個版本就已經列入過時了,取而代之的是 WinRM - IIS Web App Management Task,它不需要依賴 MsDeploy 和 Web Deploy (from Microsoft Web Platform Installer)

 

開發環境

  • IIS 8.5
  • Azure DevOps Server 2019
  • Web Application .Net Framework 4.8

部署目標必要條件

  • 位置:目標機器 FQDN / IP 和埠號,多個位置使用逗號區隔,例如:dbserver.fabrikam.com, dbserver_int.fabrikam.com:5986,192.168.34:5986. 
  • 授權:管理員帳密
  • 防火牆:WinRM 預設 https 是5986 port,http 是 5985 port

WinRM - IIS Web App Management

管理 "AppPool" 以及 "站台",提供以下命令

  • Create Or Update:建立或更新
  • Start:啟動
  • Stop:停止

UI 需要填寫的欄位有點多,不過都還算蠻簡單的

 

若 UI 無法滿足需求時,還可以額外添加 appcmd.exe 的命令

AppCmd

除了 WinRM - IIS Web App Management UI 所提供的功能之外,還可以通過  AppCmd.exe 管理站台

  1. appcmd 的路徑是 64 bit: %windir%\system32\inetsrv\appcmd.exe 32 bit: %windir%\syswow64\inetsrv\appcmd.exe
  2. appcmd 會處理 applicationhost.config,運行命令或腳本之前先備份 applicationhost.config,路徑在 %windir%\system32\inetsrv\config

以下是我在

AppPool AlwaysRunning

set apppool /apppool.name:"$(WebDeploy.WebSite)" /managedRuntimeVersion:v4.0 /autoStart:true /startMode:AlwaysRunning /enable32BitAppOnWin64:false /managedPipelineMode:Integrated

AppPool 停用時間週期回收

set apppool /apppool.name:"$(WebDeploy.WebSite)" /recycling.periodicRestart.time:"00:00:00" /-recycling.periodicRestart.schedule
我嘗試著要移除所有排程時間 /-recycling.periodicRestart.schedule ,結果是失敗,倒是可以移除特定的索引 /-recycling.periodicRestart.schedule.[@0]

AppPool 定時回收

set apppool /apppool.name:"$(WebDeploy.WebSite)" /+recycling.periodicRestart.schedule.[value='07:00:00']

站台預先載入

set site /site.name:"$(WebDeploy.WebSite)" -applicationDefaults.applicationPool:"$(WebDeploy.WebSite)" -applicationDefaults.preloadEnabled:True

站台驗證

set config "$(WebDeploy.WebSite)" /section:anonymousAuthentication /enabled:true /commit:apphost
set config "$(WebDeploy.WebSite)" /section:basicAuthentication /enabled:false /commit:apphost
set config "$(WebDeploy.WebSite)" /section:windowsAuthentication /enabled:false /commit:apphost
set config "$(WebDeploy.WebSite)" /section:clientCertificateMappingAuthentication /enabled:false /commit:apphost
set config "$(WebDeploy.WebSite)" /section:digestAuthentication /enabled:false /commit:apphost
set config "$(WebDeploy.WebSite)" /section:iisClientCertificateMappingAuthentication /enabled:false /commit:apphost

這裡我踩了一些雷

執行以下兩個節點會導致所有 IIS 網站服務崩壞,這兩個命令分別是停用 ASP.NET 模擬、停用表單驗證

set config "$(WebDeploy.WebSite)" /section:identity /impersonate:false /commit:apphost
set config "$(WebDeploy.WebSite)" /section:system.web/authentication /mode:None /commit:apphost

 

最後我把這它們放在 web.config

<system.web>
    <compilation debug="true" targetFramework="4.8" />
    <httpRuntime targetFramework="4.8" />
    <identity impersonate="false" />
    <authentication mode="None" />
</system.web>
WinRM - IIS Web App Management 會把設定好的這些命令打包成 *.ps (動態的檔名),丟到目標電腦 c:\users 的暫存區,再透過目標電腦的 powsershell 執行 *.ps1

 

WinRM - IIS Web App Deployment

發佈站台,需要 .zip 和 SetParametes.xml,這兩個檔案來自於 Build 的成品(Artifact)

在 Build 的時候帶入以下參數,就會產生相關檔案,我的參數如下

/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactstagingdirectory)\\WebPackage" /p:PrecompileBeforePublish=true
跟預設的樣版比,我多了 /p:PrecompileBeforePublish=true


 

ASP.NET build 預設的樣板就已經幫我們把 MsBuild Arguments 準備好了

 

需要更多 Build 的介紹可以參考舊文

[TFS 2015] 實作 Build vNext + Release + MsDeploy 自動部署內部網站

[TFS 2015] 實作 Build vNext + Release + SqlPackage 自動部署測試資料庫

[VSTS][Build Agent] 架設內部 Build Agent 建置、部署 VSTS

[TFS 2015] 實作 Build vNext + Release 自動部署元件到內部 Nuget Server

通過 Azure DevOps Server 2019 Build Pipeline 取代檔案中的某個變數

[ClickOnce][MsBuild] 解決使用 MsBuild 不會產生 Html Page

參考

https://github.com/microsoft/azure-pipelines-extensions/blob/master/Extensions/IISWebAppDeploy/Src/Tasks/IISWebAppMgmt/IISWebAppMgmtV1/README_IISAppMgmt.md#winrm-setup

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo