IT Hassle

10Sep/1010

Powershell script to install and configure IIS7.5

Normally when we deploy an IIS server we want all websites installed on the D drive. Same goes for the log file location. In IIS 6.0 we could easily do this with a batch command, this would completely install IIS on the D drive.

I have created the following script that will install IIS 7.5 with the default website and all logging on the D drive. Some snippets where gathered from IIS.Net

 PowerShell |  copy code |? 
01
# --------------------------------------------------------------------
02
# Checking Execution Policy
03
# --------------------------------------------------------------------
04
#$Policy = "Unrestricted"
05
$Policy = "RemoteSigned"
06
If ((get-ExecutionPolicy) -ne $Policy) {
07
  Write-Host "Script Execution is disabled. Enabling it now"
08
  Set-ExecutionPolicy $Policy -Force
09
  Write-Host "Please Re-Run this script in a new powershell enviroment"
10
  Exit
11
}
12
 
13
# --------------------------------------------------------------------
14
# Define the variables.
15
# --------------------------------------------------------------------
16
$InetPubRoot = "D:\Inetpub"
17
$InetPubLog = "D:\Inetpub\Log"
18
$InetPubWWWRoot = "D:\Inetpub\WWWRoot"
19
 
20
# --------------------------------------------------------------------
21
# Loading Feature Installation Modules
22
# --------------------------------------------------------------------
23
Import-Module ServerManager 
24
 
25
# --------------------------------------------------------------------
26
# Installing IIS
27
# --------------------------------------------------------------------
28
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
29
 
30
# --------------------------------------------------------------------
31
# Loading IIS Modules
32
# --------------------------------------------------------------------
33
Import-Module WebAdministration
34
 
35
# --------------------------------------------------------------------
36
# Creating IIS Folder Structure
37
# --------------------------------------------------------------------
38
New-Item -Path $InetPubRoot -type directory -Force -ErrorAction SilentlyContinue
39
New-Item -Path $InetPubLog -type directory -Force -ErrorAction SilentlyContinue
40
New-Item -Path $InetPubWWWRoot -type directory -Force -ErrorAction SilentlyContinue
41
 
42
# --------------------------------------------------------------------
43
# Copying old WWW Root data to new folder
44
# --------------------------------------------------------------------
45
$InetPubOldLocation = @(get-website)[0].physicalPath.ToString()
46
$InetPubOldLocation =  $InetPubOldLocation.Replace("%SystemDrive%",$env:SystemDrive)
47
Copy-Item -Path $InetPubOldLocation -Destination $InetPubRoot -Force -Recurse
48
 
49
# --------------------------------------------------------------------
50
# Setting directory access
51
# --------------------------------------------------------------------
52
$Command = "icacls $InetPubWWWRoot /grant BUILTIN\IIS_IUSRS:(OI)(CI)(RX) BUILTIN\Users:(OI)(CI)(RX)"
53
cmd.exe /c $Command
54
$Command = "icacls $InetPubLog /grant ""NT SERVICE\TrustedInstaller"":(OI)(CI)(F)"
55
cmd.exe /c $Command
56
 
57
# --------------------------------------------------------------------
58
# Setting IIS Variables
59
# --------------------------------------------------------------------
60
#Changing Log Location
61
$Command = "%windir%\system32\inetsrv\appcmd set config -section:system.applicationHost/sites -siteDefaults.logfile.directory:$InetPubLog"
62
cmd.exe /c $Command
63
$Command = "%windir%\system32\inetsrv\appcmd set config -section:system.applicationHost/log -centralBinaryLogFile.directory:$InetPubLog"
64
cmd.exe /c $Command
65
$Command = "%windir%\system32\inetsrv\appcmd set config -section:system.applicationHost/log -centralW3CLogFile.directory:$InetPubLog"
66
cmd.exe /c $Command
67
 
68
#Changing the Default Website location
69
Set-ItemProperty 'IIS:\Sites\Default Web Site' -name physicalPath -value $InetPubWWWRoot
70
 
71
# --------------------------------------------------------------------
72
# Checking to prevent common errors
73
# --------------------------------------------------------------------
74
If (!(Test-Path "C:\inetpub\temp\apppools")) {
75
  New-Item -Path "C:\inetpub\temp\apppools" -type directory -Force -ErrorAction SilentlyContinue
76
}
77
 
78
# --------------------------------------------------------------------
79
# Deleting Old WWWRoot
80
# --------------------------------------------------------------------
81
Remove-Item $InetPubOldLocation -Recurse -Force
82
 
83
# --------------------------------------------------------------------
84
# Resetting IIS
85
# --------------------------------------------------------------------
86
$Command = "IISRESET"
87
Invoke-Expression -Command $Command

Comments (10) Trackbacks (0)
  1. Looks nice, I’m guessing using the built in 2008 R2 installer doesn’t have the ability to change default install location and that’s why you need powershell?

  2. @Chase: Correct. In 2003 you can change the default installation path by providing a INI file but that is not possible with 2008 (R2).

  3. I have a similar script but I also create some ftp sites and want to change the logfile location for the ftp sites to D:\Inetpub\Log as well.
    Can you help me out please ?

  4. fixed it with
    appcmd set config -section:system.ftpServer/log /centralLogFileMode:Central /centralLogFile.directory:”D:\log\LogFiles\ftp” /commit:apphost

  5. @Dennis: Sweet, thanks for sharing. I hadn’t found any time yet to answer your question and I’m glad to see you found one.

  6. Greetings,
    I was in charge of implementing IIS 7.5 at the firm where I work. One piece of that was moving Inetpub to a different drive, including removing Inetpub from the C: drive. It appears the script above does the same with “Remove-Item $InetPubOldLocation -Recurse -Force”.

    At some point our Microsoft rep became aware that we were deleting Inetpub from C: and warned us this was not supported by Microsoft. He pointed me to a blog posting that describes how to move Inetpub, but also includes the warning about deleting Inetpub from C:. This was confirmed in a conference call with the IIS product manager. You may want to see http://blogs.iis.net/thomad/archive/2008/02/10/moving-the-iis7-inetpub-directory-to-a-different-drive.aspx.

    We made MS aware we are not happy that the only announcement about this is in a blog, and not in some “offical” MS site like TechNet, MSDN, etc. We are also not happy about having to maintain Inetpub in two locations now.
    cheers,
    ~dean~

  7. When I try to run this script on Windows Server 2012, I get the error:

    The request to add or remove features on the specified server failed.
    Installation of one or more roles, role services, or features failed.
    The source files could not be downloaded.
    Use the “source” option to specify the location of the files that are required to restore the feature. For more information on specifying a source location, see
    http://go.microsoft.com/fwlink/?LinkId=243077. Error: 0x800f0906

    After looking up the error, I found that it the command “Add-WindowsFeature” was attempting to install .Net Framework 3.5 features, which is a Feature on Demand in Windows Server 2012. The workaround is to change the configuration in the Group Policy settings in this link. http://support.microsoft.com/kb/2734782

    Since the purpose of this script is meant to automate IIS installation, is there a way to change the above setting through PowerShell cmdlet? Or avoid installing .Net 3.5 and use .Net 4.5 (which comes preinstalled with Windows Server 2012) using the “Add-WindowsFeature”?

  8. Hi Mark,

    The post was made before Windows 2012 was published, that at least explains why I didn’t run into this problem.

    There is however a simple solution without a GPO. The help of “Install-Windowsfeature” also explains this in there examples. You point to the needed binaries with the “-source” parameter.
    Example: Install-WindowsFeature -Name Web-Server -Source \\server2\winsxs

    Just add this to the script and make sure that the path specified is reachable. Everything should work out then.

  9. Thanks, this was very helpful!

    Is there some reason you used appcmd instead of the powershell cmdlets in the WebAdministration module? For example:

    # Configure logfile path
    @(‘system.applicationHost/sites/siteDefaults/logFile’
    ‘system.applicationHost/sites/siteDefaults/ftpServer/logFile’
    ‘system.applicationHost/log/centralBinaryLogFile’
    ‘system.applicationHost/log/centralW3CLogFile’
    ) |
    %{
    Set-WebConfigurationProperty -Filter $_ -Name directory -Value $InetPubLog
    }

  10. This is my first time go to see att here and i am truly happoy to read all at alone place.


Leave a comment

No trackbacks yet.