Powershell script to auto install of IIS 7 and above Powershell script to auto install of IIS 7 and above powershell powershell

Powershell script to auto install of IIS 7 and above


I found the following blog useful , with certain changes from using help i was able to install IIS from power shell with custom role services.I have the code here and the link to the blog is:

http://www.ithassle.nl/2010/09/powershell-script-to-install-and-configure-iis7-5/#codesyntax_1

# --------------------------------------------------------------------# Checking Execution Policy# --------------------------------------------------------------------#$Policy = "Unrestricted"$Policy = "RemoteSigned"If ((get-ExecutionPolicy) -ne $Policy) {  Write-Host "Script Execution is disabled. Enabling it now"  Set-ExecutionPolicy $Policy -Force  Write-Host "Please Re-Run this script in a new powershell enviroment"  Exit}# --------------------------------------------------------------------# Define the variables.# --------------------------------------------------------------------$InetPubRoot = "D:\Inetpub"$InetPubLog = "D:\Inetpub\Log"$InetPubWWWRoot = "D:\Inetpub\WWWRoot"# --------------------------------------------------------------------# Loading Feature Installation Modules# --------------------------------------------------------------------Import-Module ServerManager # --------------------------------------------------------------------# Installing IIS# --------------------------------------------------------------------Add-WindowsFeature -Name Web-Common-Http,Web-Asp-Net,Web-Net-Ext,Web-ISAPI-Ext,Web-ISAPI-Filter,Web-Http-Logging,Web-Request-Monitor,Web-Basic-Auth,Web-Windows-Auth,Web-Filtering,Web-Performance,Web-Mgmt-Console,Web-Mgmt-Compat,RSAT-Web-Server,WAS -IncludeAllSubFeature# --------------------------------------------------------------------# Loading IIS Modules# --------------------------------------------------------------------Import-Module WebAdministration# --------------------------------------------------------------------# Creating IIS Folder Structure# --------------------------------------------------------------------New-Item -Path $InetPubRoot -type directory -Force -ErrorAction SilentlyContinueNew-Item -Path $InetPubLog -type directory -Force -ErrorAction SilentlyContinueNew-Item -Path $InetPubWWWRoot -type directory -Force -ErrorAction SilentlyContinue# --------------------------------------------------------------------# Copying old WWW Root data to new folder# --------------------------------------------------------------------$InetPubOldLocation = @(get-website)[0].physicalPath.ToString()$InetPubOldLocation =  $InetPubOldLocation.Replace("%SystemDrive%",$env:SystemDrive)Copy-Item -Path $InetPubOldLocation -Destination $InetPubRoot -Force -Recurse# --------------------------------------------------------------------# Setting directory access# --------------------------------------------------------------------$Command = "icacls $InetPubWWWRoot /grant BUILTIN\IIS_IUSRS:(OI)(CI)(RX) BUILTIN\Users:(OI)(CI)(RX)"cmd.exe /c $Command$Command = "icacls $InetPubLog /grant ""NT SERVICE\TrustedInstaller"":(OI)(CI)(F)"cmd.exe /c $Command# --------------------------------------------------------------------# Setting IIS Variables# --------------------------------------------------------------------#Changing Log Location$Command = "%windir%\system32\inetsrv\appcmd set config -section:system.applicationHost/sites -siteDefaults.logfile.directory:$InetPubLog"cmd.exe /c $Command$Command = "%windir%\system32\inetsrv\appcmd set config -section:system.applicationHost/log -centralBinaryLogFile.directory:$InetPubLog"cmd.exe /c $Command$Command = "%windir%\system32\inetsrv\appcmd set config -section:system.applicationHost/log -centralW3CLogFile.directory:$InetPubLog"cmd.exe /c $Command#Changing the Default Website locationSet-ItemProperty 'IIS:\Sites\Default Web Site' -name physicalPath -value $InetPubWWWRoot# --------------------------------------------------------------------# Checking to prevent common errors# --------------------------------------------------------------------If (!(Test-Path "C:\inetpub\temp\apppools")) {  New-Item -Path "C:\inetpub\temp\apppools" -type directory -Force -ErrorAction SilentlyContinue}# --------------------------------------------------------------------# Deleting Old WWWRoot# --------------------------------------------------------------------Remove-Item $InetPubOldLocation -Recurse -Force# --------------------------------------------------------------------# Resetting IIS# --------------------------------------------------------------------$Command = "IISRESET"Invoke-Expression -Command $Command


I can able to install IIS using powershell script by following below step:

1) Create batch File (Setup_IIS_win10.bat) with following script in it

@ECHO OFFSET ThisScriptsDirectory=%~dp0SET PowerShellScriptPath="%ThisScriptsDirectory%SetupIISPowerShellScript_win10.ps1"PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%PowerShellScriptPath%""' -Verb RunAs}";

2) Create powershell script file (SetupIISPowerShellScript_win10.ps1) with following script in it

# This script installs IIS and the features required to run asp.net applications# * Make sure you run this script from an Admin Prompt!# * Make sure Powershell Execution Policy is bypassed to run these scripts:# * YOU MAY HAVE TO RUN THIS COMMAND PRIOR TO RUNNING THIS SCRIPT!Set-ExecutionPolicy Bypass -Scope ProcessEnable-WindowsOptionalFeature -Online -FeatureName IIS-WebServerRoleEnable-WindowsOptionalFeature -Online -FeatureName IIS-WebServerEnable-WindowsOptionalFeature -Online -FeatureName IIS-CommonHttpFeaturesEnable-WindowsOptionalFeature -Online -FeatureName IIS-HttpErrorsEnable-WindowsOptionalFeature -Online -FeatureName IIS-HttpRedirectEnable-WindowsOptionalFeature -Online -FeatureName IIS-ApplicationDevelopmentEnable-WindowsOptionalFeature -Online -FeatureName IIS-NetFxExtensibility45Enable-WindowsOptionalFeature -Online -FeatureName IIS-HealthAndDiagnosticsEnable-WindowsOptionalFeature -Online -FeatureName IIS-HttpLoggingEnable-WindowsOptionalFeature -Online -FeatureName IIS-LoggingLibrariesEnable-WindowsOptionalFeature -Online -FeatureName IIS-RequestMonitorEnable-WindowsOptionalFeature -Online -FeatureName IIS-HttpTracingEnable-WindowsOptionalFeature -Online -FeatureName IIS-SecurityEnable-WindowsOptionalFeature -Online -FeatureName IIS-RequestFilteringEnable-WindowsOptionalFeature -Online -FeatureName IIS-PerformanceEnable-WindowsOptionalFeature -Online -FeatureName IIS-WebServerManagementToolsEnable-WindowsOptionalFeature -Online -FeatureName IIS-IIS6ManagementCompatibilityEnable-WindowsOptionalFeature -Online -FeatureName IIS-MetabaseEnable-WindowsOptionalFeature -Online -FeatureName IIS-ManagementConsoleEnable-WindowsOptionalFeature -Online -FeatureName IIS-BasicAuthenticationEnable-WindowsOptionalFeature -Online -FeatureName IIS-WindowsAuthenticationEnable-WindowsOptionalFeature -Online -FeatureName IIS-StaticContentEnable-WindowsOptionalFeature -Online -FeatureName IIS-DefaultDocumentEnable-WindowsOptionalFeature -Online -FeatureName IIS-WebSocketsEnable-WindowsOptionalFeature -Online -FeatureName IIS-ApplicationInitEnable-WindowsOptionalFeature -Online -FeatureName IIS-NetFxExtensibility45Enable-WindowsOptionalFeature -Online -FeatureName IIS-ASPNET45Enable-WindowsOptionalFeature -Online -FeatureName IIS-ISAPIExtensionsEnable-WindowsOptionalFeature -Online -FeatureName IIS-ISAPIFilterEnable-WindowsOptionalFeature -Online -FeatureName IIS-HttpCompressionStaticEnable-WindowsOptionalFeature -Online -FeatureName IIS-ASPEnable-WindowsOptionalFeature -Online -FeatureName IIS-CGIEnable-WindowsOptionalFeature -Online -FeatureName IIS-ASPNET35Enable-WindowsOptionalFeature -Online -FeatureName IIS-ASPNET47#Enable-WindowsOptionalFeature -Online -FeatureName IIS-ServerSideIncludesEnable-WindowsOptionalFeature -Online -FeatureName WCF-Services45Enable-WindowsOptionalFeature -Online -FeatureName WCF-Http-Activation45Enable-WindowsOptionalFeature -Online -FeatureName WCF-TCP-PortSharing45# If running in the console, wait for input before closing.if ($Host.Name -eq "ConsoleHost"){     Write-Host "Press any key to close..."    $Host.UI.RawUI.FlushInputBuffer()   # Make sure buffered input doesn't "press a key" and skip the ReadKey().    $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null}

3) Run the batch file(Setup_IIS_win10.bat) with Adminitrator privilege

Hope it will be useful for someone.


It's just a matter of calling pkgmgr with the correct packages:

$packages = "IIS-WebServerRole;" +    "IIS-WebServer;" +    "IIS-CommonHttpFeatures;" +    "IIS-StaticContent;" +    "IIS-DefaultDocument;" +#       ... some other packages here     "IIS-ManagementConsole;" +    "IIS-ManagementService;" +    "IIS-LegacySnapIn;" +    "IIS-FTPManagement;" +     "WAS-NetFxEnvironment;" +    "WAS-ConfigurationAPI"Start-Process "pkgmgr" "/iu:$packages"

Depending on your platform, and on IIS version, there are some subtle differences. You can find more information here, here, and here.