Skip to content

Instantly share code, notes, and snippets.

@rwetzeler
Created April 25, 2012 13:01
Show Gist options
  • Save rwetzeler/2489561 to your computer and use it in GitHub Desktop.
Save rwetzeler/2489561 to your computer and use it in GitHub Desktop.
Import-Module WebAdministration
$port = 80
$ssl = $false
#App Start
SetupOptionalVariables
SetupAppPools
SetupWebSite
SetupWebApplication
SetupAuthentication
SetupElmahSubject
SetupACLForPulseDB
function SetupVariables {
if($siteName -eq $null)
{
$siteName = $OctopusWebSiteName
}
if($webApplicationAppPool -eq $null)
{
$webApplicationAppPool = $appPoolName
}
}
function SetupAppPool {
param ($poolName)
try {
$ap = Get-WebAppPoolState | Where {$_.Name -eq $poolName}
if($ap -eq $null) {
write-host "no App Pool setup, creating... $poolName"
New-WebAppPool $poolName
Set-ItemProperty IIS:\AppPools\$poolName managedRuntimeVersion v4.0
}
}
catch {
throw "Error while attempting to create a new app pool called $poolName"
}
}
function SetupAppPools {
SetupAppPool $appPoolName
if($appPoolName -ne $webApplicationPoolName) {
SetupAppPool $webApplicationPoolName
}
}
function SetupWebSite {
try {
$ws = Get-Website | Where {$_.Name -eq $siteName}
if($ws -eq $null) {
write-host "no web site setup, creating..."
$webApp = New-Website -Name $siteName -Port $port -HostHeader $hostHeader -ApplicationPool $appPoolName -Ssl:$ssl -PhysicalPath c:\inetpub\wwwroot
if($hostHeaderAlt -ne $null) {
write-host "adding alt host header"
New-WebBinding -Name $siteName -IPAddress "*" -Port $port -HostHeader $hostHeaderAlt
}
}
else {
write-host "Web Site Already setup, skipping..."
}
}
catch {
throw "Error while creating web site: $siteName"
}
}
function SetupWebApplication {
try {
if($webApplication -ne $null) {
write-host "Web Application defined. Checking if it already exists."
$app = Get-WebApplication -Site $OctopusWebSiteName -Name $webApplication
if ($app -eq $null)
{
write-host "Web Application does not exist. Creating ..."
New-WebApplication -Site $OctopusWebSiteName -Name $webApplication -PhysicalPath "c:\inetpub\wwwroot" -ApplicationPool $webApplicationAppPool
}
else {
write-host "Web Application already exist. Skipping..."
}
}
}
catch {
throw "Error while creating the web application: $webApplication"
}
}
function SetupAuthentication {
try {
if($allowWindowsAuth) {
$windowsAuthServer = Get-WebConfigurationLock -PSPath "IIS:\" -Filter //windowsAuthentication #-Name enabled
Write-Host "value: " $windowsAuthServer.Value
if($windowsAuthServer.Value -ne $null) {
Write-Host "Unlocking Windows Auth Override on Server"
Remove-WebConfigurationLock -PSPath "IIS:\" -Filter //windowsAuthentication
}
else {
Write-Host "Windows Auth Override on Server already set to Allow, skipping..."
}
$webAppSetForWindowAuth = Get-WebConfigurationProperty -filter /system.WebServer/security/authentication/windowsAuthentication -name enabled -location $OctopusWebSiteName
if(!$webAppSetForWindowAuth.Value) {
Write-Host "Windows Auth not set for this web app, enabling"
Set-WebConfigurationProperty -filter /system.WebServer/security/authentication/windowsAuthentication -name enabled -value true -location $OctopusWebSiteName
}
else {
Write-Host "Windows Auth is already enabled for the web app $OctopusWebsiteName , skipping..."
}
}
else {
Write-Host "Use WindowsAuth Not configured, moving on"
}
}
catch {
throw "Error while setting up Authentication"
}
}
function SetupElmahSubject {
$original_file = 'Web.config'
$destination_file = 'Web.config'
$searchText = 'ELMAH_EMAIL_SUBJECT'
$newText = $OctopusProjectName + ' - Env:' + $OctopusEnvironmentName + ' Server: ' + $OctopusMachineName + ' Ver: ' + $OctopusPackageVersion
Write-Host "Setting Elmah Email Subject to: " + $newText
(Get-Content $original_file) | Foreach-Object {
$_ -replace $searchText, $newText
} | Set-Content $destination_file
}
function SetupACLForPulseDB {
Write-Host "Setting ACLs for App_Data folder to let Pulse log correctly"
$directory = "D:\Octopus\Tentacle\Applications\" + $OctopusPackageNameAndVersion + "\App_Data\Pulse.sdf"
Write-Host "checking ACL on: " $directory
if (Test-Path $directory)
{
$acl = Get-Acl $directory
$permission = "IIS AppPool\$appPoolName","Read,ExecuteFile,Write","Allow"
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
Write-Host "Setting ACL on: " $directory
$acl.SetAccessRule($accessRule)
$acl | Set-Acl $directory
}
else
{
throw "The dir/file $directory can't be found"
}
}
@x-a-n-d-e-r-k
Copy link

need to change line 7:
$ap = Get-WebAppPoolState | Where {$_.Name -eq $appPoolName}
to
$ap = Get-WebAppPoolState -Name "$appPoolName"

@rwetzeler
Copy link
Author

fixed via bkeele

@x-a-n-d-e-r-k
Copy link

line 102:
throw "The dir/file $directory can't be found."

The concatenation is messing it up.

line 80 typo: Change Elamh to Elmah.

@x-a-n-d-e-r-k
Copy link

Ok, made some changes. Please look at : https://gist.github.com/2491944/6ac02982629b724d32df900236d571aed3c36eee and bring in at least everything betweek line 34 and 50, inclusive.

This includes handling webapplications instead of only sites.

@rwetzeler
Copy link
Author

rwetzeler commented Apr 25, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment