Friday, 19 October 2012

Install IIS 7.x from Powershell

This post is a follow on from this post. A few days ago I needed to install the whole shebang, rather than just a few features for IIS, so the script below can be used to install IIS as used in our environment.

The list of features includes everything that was installed in our Web Server at the time. I'm fairly certain that we don't need Web-Client-Auth, which relates to client authenticated SSL, but there you go.

The script stops if installation of any feature fails you might not want that, but we do.

The script would be more flexible if it read the features from file but then I would need to distribute two files rather than one. At any rate, you could use $windowsfeatures = Get-Content filename.txt to read from a file instead.
import-module servermanager

$windowsfeatures = @("Web-Server","Web-WebServer","Web-Common-Http","Web-Static-Content","Web-Default-Doc","Web-Http-Errors","Web-App-Dev","Web-Asp-Net","Web-Net-Ext","Web-ISAPI-Ext","Web-ISAPI-Filter","Web-Security","Web-Windows-Auth","Web-Filtering","Web-Performance","Web-Stat-Compression","Web-Dyn-Compression","Web-Mgmt-Tools","Web-Mgmt-Console","Web-Mgmt-Compat","Web-Metabase","Web-Dir-Browsing","Web-IP-Security","Web-Mgmt-Service","Web-Http-Logging","Web-Cert-Auth","Web-Client-Auth")

foreach ($feature in $windowsfeatures)
{

$state = Get-WindowsFeature -Name $feature.trim()

 if ($state.Installed -eq $true)
 {
  Write-Host "$feature is already installed"
 }
 else
 {
  Write-Host "Installing $feature"

  $result = add-windowsfeature $feature.trim()

  if (([System.Convert]::ToBoolean($result.Success)))
  {
   Write-Host "$feature has been installed"
  }
  else
  {
   Write-Host "An error occurred installing $feature"
   exit
  }
 }
}

Write-Host "All Role Services have been Installed"

remove-module servermanager

No comments:

Post a Comment