1. 程式人生 > >批處理命令完成應用池和網站的新增、刪除、啟動和停止

批處理命令完成應用池和網站的新增、刪除、啟動和停止

最近接到一個任務:使用批處理命令完成以下幾個功能。

  1. Stop IIS Service
  2. Start IIS Service
  3. Stop AppPool
  4. Start AppPool
  5. Create AppPool
  6. Create Application Path

經過一番搜尋也有些頭緒,便開始了批處理檔案的編寫……

@echo off&cls

:: For AppPool
@set apppoolname="New AppPool"
:: true/false
@set loaduserprofile="false"
:: LocalSystem/LocalService/NetworkService/ApplicationPoolIdentity/SpecificUser
@set
processmodelindentytype="NetworkService" :: v4.0/v2.0 @set runtimeversion="v4.0" :: Integrated/Classic @set pipelinemode="Integrated" :: For WebSite @set websitename="New WebSite" @set ipaddress="*" @set port="8087" @set sitePath="C:\website" :: For Add Application Bindings @set addbindingwebsitename="New WebSite" :: http/https/net
.tcp/net.pipe/met.msmq/msmq.formatname @set addbindingsprotocol="https" @set addbindingsipaddress="192.168.238.1" @set addbindingsport="8089" @set addsslcertificate="IIS Express Development Certificate" :: For Delete @set deleteapppoolname="New AppPool" @set deletewebsitename="New WebSite" GOTO MENU :MENU echo
. echo ############################# echo 1 Stop IIS Service echo. echo 2 Start IIS Service echo. echo 3 Stop AppPool echo. echo 4 Start AppPool echo. echo 5 Create AppPool echo. echo 6 Delete AppPool echo. echo 7 Create Application Path echo. echo 8 Add Application Bindings echo. echo 9 Delete Application Path echo ############################# echo. set/p a=Please select a function : echo. if %a%==1 goto cmd1 if %a%==2 goto cmd2 if %a%==3 goto cmd3 if %a%==4 goto cmd4 if %a%==5 goto cmd5 if %a%==6 goto cmd6 if %a%==7 goto cmd7 if %a%==8 goto cmd8 if %a%==9 goto cmd9 :cmd1 echo You choosed : Stop IIS Service iisreset /STOP goto MENU :cmd2 echo You choosed : Start IIS Service net start w3svc goto MENU :cmd3 echo You choosed : Stop AppPool c:\windows\system32\inetsrv\AppCmd.exe stop apppool /apppool.name:%apppoolname% goto MENU :cmd4 echo You choosed : Start AppPool c:\windows\system32\inetsrv\AppCmd.exe start apppool /apppool.name:%apppoolname% goto MENU :cmd5 echo You choosed : Create AppPool C:\Windows\System32\inetsrv\appcmd.exe add apppool /name:%apppoolname% -processModel.loadUserProfile:%loaduserprofile% -processModel.identityType:%processmodelindentytype% /managedRuntimeVersion:%runtimeversion% /managedPipelineMode:%pipelinemode% goto MENU :cmd6 echo You choosed : Delete AppPool C:\Windows\System32\inetsrv\appcmd.exe delete apppool /apppool.name:%deleteapppoolname% goto MENU :cmd7 echo You choosed : Create Application Path C:\Windows\System32\inetsrv\appcmd.exe add site /name:%websitename% /bindings:http/%ipaddress%:%port%: /applicationDefaults.applicationPool:%apppoolname% /physicalPath:%sitePath% goto MENU :cmd8 echo You choosed : Add Application Bindings C:\Windows\System32\inetsrv\appcmd.exe set site /site.name:%addbindingwebsitename% /+bindings.[protocol='%addbindingsprotocol%',bindingInformation='%addbindingsipaddress%:%addbindingsport%:%addsslcertificate%'] goto MENU :cmd9 echo You choosed : Delete Application Path C:\Windows\System32\inetsrv\appcmd.exe delete site /site.name:%deletewebsitename% goto MENU Pause goto menu

其中的Delete AppPool,Add Application Bindings,Delete Application Path是根據需求新增的功能。
注意
1. 跑之前需要先在檔案裡把應用池和網站資訊配置好(區分大小寫,要注意);
2. 因為涉及許可權問題,需要以“管理員身份”執行此檔案。
3. Add Application Bindings這個功能裡要注意單引號不能丟,bindingInformation和前面的逗號不能有空格
4. 有什麼問題請留言,共同討論。