使用PowerShell 自動安裝IIS 及自動部署網站


執行環境:Windows Server 2012 R2

 

安裝iis核心代碼,可自定義安裝項

注意這里不能使用add-windowsfeature  "Web-Filtering","Web-IP-Security"方式自定義安裝,會拋出異常,我猜測是安裝順序的問題

$iisInstallPro = "Web-Filtering","Web-IP-Security","Web-Url-Auth","Web-Windows-Auth","Web-Basic-Auth","Web-Http-Errors","Web-Static-Content","Web-Default-Doc","Web-Dir-Browsing","Web-Stat-Compression","Web-Http-Logging","Web-ODBC-Logging","Web-Mgmt-Console","Web-WHC","Web-Net-Ext","Web-Net-Ext45","Web-ASP","Web-Asp-Net","Web-Asp-Net45","Web-CGI","Web-ISAPI-Ext","Web-ISAPI-Filter","Web-WebSockets","Web-Includes","Web-AppInit";
$features = get-windowsfeature web-*
foreach($item in $features)
{
  if($item.installed -eq $false -and $iisInstallPro -Match $item.Name)
  {
    Write-Host "安裝:" $item.displayname
    $item | add-windowsfeature -WarningAction silentlyContinue
  }
}

 

自動安裝IIS

$features = get-windowsfeature web-*
foreach($item in $features)
{
  if($item.installed -eq $false)
  {
    Write-Host "安裝:$item.displayname"
    $item | add-windowsfeature
  }
}

function RegisterAndEnableIsapi
{
 $isapiPath ="$env:windir\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll"
  
  $isapiConfiguration = get-webconfiguration "/system.webServer/security/isapiCgiRestriction/add[@path='$isapiPath']/@allowed"
  if($null -eq $isapiConfiguration)
  {
    Write-Host "IIS尚未注冊aspnet_isapi.dll"
    $tmpPath=""
   
   
    $tmpPath = "$env:windir\Microsoft.NET\Framework\v4.0.30319\"
    
    set-location $tmpPath
    .\aspnet_regiis.exe -i
    $isapiConfiguration = get-webconfiguration "/system.webServer/security/isapiCgiRestriction/add[@path='$isapiPath']/@allowed"
  }
  if($isapiConfiguration.Value -eq $false)
  {
      Write-Host "IIS已經注冊過aspnet_isapi.dll,但未啟用"
    set-webconfiguration "/system.webServer/security/isapiCgiRestriction/add[@path='$isapiPath']/@allowed" -value true
    if(Is64Bit)
    { 
      set-webconfiguration "/system.webServer/security/isapiCgiRestriction/add[@path='$env:windir\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll']/@allowed" -value true
    }
    Write-Host "isapi已啟用"
  }
  else
  {
      Write-Host "IIS已經注冊過aspnet_isapi.dll,且已啟用"
  }
}

 RegisterAndEnableIsapi
View Code

 自動部署網站     參考鏈接

Import-Module WebAdministration
New-Item –Path IIS:\AppPools\MyAppPool
New-Website –Name MyWebApp –PhysicalPath D:\apidd
New-WebApplication -Name testApp -Site 'MyWebApp' -PhysicalPath D:\apidd -ApplicationPool DefaultAppPool

  

powershell自動安裝iis,點擊打開。打開之后如下圖

 

1.輸入get-windowsfeature web* 查看已經安裝過的IIS 功能(IIS的安裝包全部以web開頭的),結果如下(沒有安裝任何功能,安裝過的前面[ ]會有X)。

2.安裝web-server,就是IIS 服務了。

install-windowsfeature web-server 

 

安裝后會如果提示 Success 就是安裝成功了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM