Last active
December 17, 2015 20:39
-
-
Save lholman/5669329 to your computer and use it in GitHub Desktop.
A powershell module to add an IIS website and app pool to a specified server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Add-IISSite{ | |
<# | |
.SYNOPSIS | |
A powershell module to add an IIS website and app pool to a specified server | |
.DESCRIPTION | |
A powershell module to add an IIS website and app pool to a specified server | |
.NOTES | |
Author: Lloyd Holman | |
DateCreated: 17/01/2013 | |
Requirements: Copy this module to any location found in $env:PSModulePath | |
.PARAMETER serverName | |
Required. The name of the remote server to execute the command against | |
.PARAMETER platformName | |
Required. The name of the platform to use when adding the IIS web site and App Pool | |
.PARAMETER bindings | |
Required. An IIS formatted list of bindings to add to the site on creation. | |
//http/*:80:google.com,http/*:80:,http/*:80:images.google.com,https/*:443: | |
.PARAMETER user | |
Optional. The FQDN of the administrator to run the commands as. | |
.PARAMETER psSession | |
Optional. A valid PSSession object for the supplied serverName, saves us authenticating again | |
.PARAMETER websiteRootPath | |
Optional. The root path on the remote server where the IIS Site should be created. Defaults to c:\inetpub. | |
.PARAMETER appPoolIdentity | |
Optional. The IdentityType to use for creating the appPool, one of the following ApplicationPoolIdentity, LocalService, LocalSystem, NetworkService, SpecificUser. Defaults to NetworkService more detail here http://www.iis.net/configreference/system.applicationhost/applicationpools/add/processmodel | |
.PARAMETER appPoolIdentityUserName | |
Optional. Specifies the identity under which the application pool runs when the appPoolIdentity parameter is set to SpecificUser. | |
.PARAMETER appPoolIdentityPassword | |
Optional. Specifies the password associated with the appPoolIdentityUserName parameter. | |
.EXAMPLE | |
Import-Module Add-IISSiteSpecifcServer | |
Import the module | |
.EXAMPLE | |
Get-Command -Module Add-IISSiteSpecifcServer | |
List available functions | |
.EXAMPLE | |
Add-IISSiteSpecifcServer -serverName -platformName -user | |
Execute the module | |
#> | |
[cmdletbinding()] | |
Param( | |
[Parameter( | |
Position = 0, | |
Mandatory = $True )] | |
[string]$serverName, | |
[Parameter( | |
Position = 1, | |
Mandatory = $True )] | |
[string]$platformName, | |
[Parameter( | |
Position = 2, | |
Mandatory = $True )] | |
[string]$bindings, | |
[Parameter( | |
Position = 3, | |
Mandatory = $False )] | |
[string]$user, | |
[Parameter( | |
Position = 4, | |
Mandatory = $False )] | |
[system.management.automation.runspaces.PSSession]$psSession, | |
[Parameter( | |
Position = 5, | |
Mandatory = $False )] | |
[string]$websiteRootPath, | |
[Parameter( | |
Position = 6, | |
Mandatory = $False )] | |
[string]$appPoolIdentity, | |
[Parameter( | |
Position = 7, | |
Mandatory = $False )] | |
[string]$appPoolIdentityUserName, | |
[Parameter( | |
Position = 8, | |
Mandatory = $False )] | |
[string]$appPoolIdentityPassword | |
) | |
Begin { | |
$DebugPreference = "Continue" | |
} | |
Process { | |
Try | |
{ | |
if ($serverName -eq "") | |
{ | |
Write-Host "Please provide a serverName to execute the command against" | |
break | |
} | |
if ($platformName -eq "") | |
{ | |
Write-Host "Please provide a platformName to execute the command for" | |
break | |
} | |
if ($bindings -eq "") | |
{ | |
Write-Host "Please provide a set of IIS bindings to add to the site" | |
break | |
} | |
if ($user -eq "" -and $psSession -eq $null) | |
{ | |
Write-Host "Please provide a Windows user name via -user or a PSSession object via -psSession" | |
break | |
} | |
if ($psSession -ne $null) | |
{ | |
#Lets check the supplied serverName against the PSSession ComputerName | |
if ($psSession.ComputerName -ne $serverName) | |
{ | |
Write-Host "The supplied serverName doesn't match the ComputerName of the supplied PSSession object." | |
break | |
} | |
} | |
$s = $null | |
if ($psSession -ne $null) | |
{ | |
#We've been passed a PSSession object so lets use that | |
$s = $psSession | |
} | |
else | |
{ | |
#Create a Credential object only if a $user has been supplied | |
if ($user -ne "") { $c = Get-Credential -Credential $user; } | |
if ($c -ne $null) | |
{ | |
#Create a new PSSession with the specific user credential | |
$s = New-PSSession -ComputerName $serverName -Credential $c; | |
} | |
} | |
$appPoolName = "$platformName" | |
Write-Host "Adding IIS app pool: $appPoolName on $serverName"; | |
if ($appPoolIdentity -eq "") | |
{ | |
$appPoolIdentity = "NetworkService" | |
} | |
if ($appPoolIdentity -eq "SpecificUser") | |
{ | |
Invoke-Command -Session $s -ScriptBlock { param($InnerServerName, $InnerAppPoolName, $InnerAppPoolIdentity, $InnerAppPoolIdentityUserName, $InnerAppPoolIdentityPassword) & c:\windows\system32\inetsrv\APPCMD add apppool /name:"$InnerAppPoolName" /enable32BitAppOnWin64:"true" /managedRuntimeVersion:"v4.0" /processModel.identityType:"$InnerAppPoolIdentity" /processModel.userName:"$InnerAppPoolIdentityUserName" /processModel.password:"$InnerAppPoolIdentityPassword" /recycling.periodicRestart.time:"00:00:00"} -ArgumentList $serverName, $appPoolName, $appPoolIdentity, $appPoolIdentityUserName, $appPoolIdentityPassword | Format-List; | |
} | |
else | |
{ | |
Invoke-Command -Session $s -ScriptBlock { param($InnerServerName, $InnerAppPoolName, $InnerAppPoolIdentity) & c:\windows\system32\inetsrv\APPCMD add apppool /name:"$InnerAppPoolName" /enable32BitAppOnWin64:"true" /managedRuntimeVersion:"v4.0" /processModel.identityType:"$InnerAppPoolIdentity" /recycling.periodicRestart.time:"00:00:00"} -ArgumentList $serverName, $appPoolName, $appPoolIdentity | Format-List; | |
} | |
$siteName = "$platformName" | |
$physicalPath = "c:\inetpub" | |
if ($websitePath -ne "") | |
{ | |
$physicalPath = $websiteRootPath | |
} | |
$physicalPath = "$physicalPath\$siteName" | |
Write-Host "Adding IIS site: $siteName on $serverName"; | |
Invoke-Command -Session $s -ScriptBlock { param($InnerServerName, $InnerSiteName, $InnerPhysicalPath, $InnerBindings) & c:\windows\system32\inetsrv\APPCMD add site /name:"$InnerSiteName" /physicalPath:"$InnerPhysicalPath" /bindings:"$InnerBindings"} -ArgumentList $serverName, $siteName, $physicalPath, $bindings | Format-List; | |
Write-Host "Setting IIS site: $siteName to use app pool: $appPool on $serverName"; | |
Invoke-Command -Session $s -ScriptBlock { param($InnerServerName, $InnerSiteName, $InnerAppPoolName) & c:\windows\system32\inetsrv\APPCMD set app $InnerSiteName/ /applicationPool:"$InnerAppPoolName"} -ArgumentList $serverName, $siteName, $appPoolName | Format-List; | |
if ($psSession -eq $null) | |
{ | |
Remove-PSSession -Session $s; | |
} | |
} | |
catch [Exception] { | |
$result = "Woh!, wasn't expecting to get this exception. `r`n $_.Exception.ToString()" | |
} | |
} | |
End { | |
return $result | Format-Table | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment