使用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 就是安装成功了。

猜你喜欢

转载自www.cnblogs.com/GoCircle/p/11226683.html