-
-
Save matt2005/8c4bb2341dcc19352759 to your computer and use it in GitHub Desktop.
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
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" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment