如何通过代码安装IIS 8

对于我的Windows 2012 R2学校作业,我需要在我的虚拟机上自动安装IIS 8 。 我想最好的方法是用脚本。 我已经find了许多用户界面的解决scheme,但没有为CMD。 有没有)一个方法来自动安装在每台机器上的用户界面? 或者有b)在Windows上安装IIS 8的脚本?

先谢谢你。

在server 2012 R2上,可以使用Enable-WindowsOptionalFeature cmdlet来安装默认的IIS使用:

Enable-WindowsOptionalFeature -Online -FeatureName "IIS-WebserverRole" 

要选择可选功能,请使用以下某些方法构建自定义脚本:

 # helper function Function InstallIISFeature([string]$name) { & Enable-WindowsOptionalFeature -Online -FeatureName $name } InstallIISFeature "IIS-WebserverRole" # this installs: #IIS-ApplicationDevelopment #IIS-CommonHttpFeatures #IIS-DefaultDocument #IIS-DirectoryBrowsing #IIS-HealthAndDiagnostics #IIS-HttpCompressionStatic #IIS-HttpErrors #IIS-HttpLogging #IIS-ManagementConsole #IIS-Performance #IIS-RequestFiltering #IIS-RequestMonitor #IIS-Security #IIS-StaticContent #IIS-Webserver #IIS-WebserverManagementTools #IIS-WebserverRole # AspNetPrerequisites() InstallIISFeature "IIS-ISAPIFilter" InstallIISFeature "IIS-ISAPIExtensions" # ASP.NET InstallIISFeature "NetFx4Extended-ASPNET45" InstallIISFeature "IIS-NetFxExtensibility45" InstallIISFeature "IIS-ASPNET45" InstallIISFeature "IIS-NetFxExtensibility" InstallIISFeature "IIS-ASPNET" # Classic ASP InstallIISFeature "IIS-ASP" # more optional features InstallIISFeature "IIS-FTPserver" InstallIISFeature "IIS-FTPSvc" InstallIISFeature "IIS-ManagementScriptingTools" InstallIISFeature "IIS-HttpCompressionDynamic" InstallIISFeature "IIS-IISCertificateMappingAuthentication" InstallIISFeature "IIS-HttpTracing" InstallIISFeature "IIS-HttpRedirect" InstallIISFeature "IIS-WindowsAuthentication" InstallIISFeature "IIS-IPSecurity" InstallIISFeature "IIS-WebSockets" InstallIISFeature "IIS-LoggingLibraries" InstallIISFeature "IIS-RequestMonitor" InstallIISFeature "IIS-ManagementService" # show installed features Get-WindowsOptionalFeature –Online | Where {$_.FeatureName -match "^IIS-" -and $_.State -eq "Enabled"} | Sort FeatureName | Select FeatureName 

您可以通过将所有必需的功能作为数组而不是字符串传递来加速启用功能所需的时间。

您也可以使用“全部”参数。 它“启用指定功能的所有父功能。如果要在图像中启用指定功能需要父功能,则全部将启用父功能及其所有的默认相关功能。 资源

 # Also helpful reference: https://peter.hahndorf.eu/blog/WindowsFeatureViaCmd # Start with some basics [System.Collections.ArrayList]$features = "IIS-WebserverRole","IIS-ISAPIFilter","IIS-ISAPIExtensions" # add roles for ASP.NET specifically $features.Add("NetFx4Extended-ASPNET45") $features.Add("IIS-NetFxExtensibility45") $features.Add("IIS-ASPNET45") $features.Add("NetFx3") $features.Add("NetFx3serverFeatures") $features.Add("IIS-NetFxExtensibility") $features.Add("IIS-ASPNET") # classic ASP $features.Add("IIS-ASP") # more optional features $features.Add("IIS-FTPserver") $features.Add("IIS-FTPSvc") $features.Add("IIS-ManagementScriptingTools") $features.Add("IIS-HttpCompressionDynamic") $features.Add("IIS-IISCertificateMappingAuthentication") $features.Add("IIS-HttpTracing") $features.Add("IIS-HttpRedirect") $features.Add("IIS-WindowsAuthentication") $features.Add("IIS-IPSecurity") $features.Add("IIS-WebSockets") $features.Add("IIS-LoggingLibraries") $features.Add("IIS-RequestMonitor") $features.Add("IIS-ManagementService") # now we'll enable the features all in one shot (quite a bit faster than one at a time) Enable-WindowsOptionalFeature -Online -FeatureName $features -All # show installed features # Get-WindowsOptionalFeature –Online | Where {$_.FeatureName -match "^IIS-" -and $_.State -eq "Enabled"} | Sort FeatureName | Select FeatureName # show features NOT installed # Get-WindowsOptionalFeature –Online | Where {$_.FeatureName -match "^IIS-" -and $_.State -ne "Enabled"} | Sort FeatureName | Select FeatureName 

请注意:* -WindowsOptionalFeature cmdlet集可用于服务器或非服务器Windows,并启用/禁用已经在OS中的功能。 这是DISM,因为你可以使用在线图像与-online开关或离线图像

服务器使用* -WindowsFeature cmdlet添加或删除功能