Skip to content

Instantly share code, notes, and snippets.

@meehanman
Created May 14, 2019 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meehanman/b00d5d91e9d076ebe02cc9b15aca6d2c to your computer and use it in GitHub Desktop.
Save meehanman/b00d5d91e9d076ebe02cc9b15aca6d2c to your computer and use it in GitHub Desktop.
DevOps | IIS | Creates a new IIS Pool containing a website and that contains a single application
Import-Module ServerManager
Import-Module WebAdministration
######################################################################################################################################################################################
# Functions
######################################################################################################################################################################################
function Refresh-Directory {
Param (
[Parameter(Position=0,mandatory=$true)]
[String]$DirectoryPath
)
#If it exists, then remove it
if(Test-Path $DirectoryPath){
#If we make it this far, the folder is not locked
Remove-Item "${DirectoryPath}" -Force -Recurse
Write-Host "Removed Directory ${DirectoryPath} as there was already a folder."
}
#Create a new Directory
New-Item -ItemType directory -Path "${DirectoryPath}"
}
#Used to transform the configurations in the package that follow the structure
# web.devonline.config => web.config (dev)
# web.proddmz.config => web.config (uat dmz etc.)
function Transform-Configurations {
$configs = Get-ChildItem -Path "$($OctopusParameters['Octopus.Action.Package.InstallationDirectoryPath'])\*" -Include *.config,*.json
$environment = $OctopusParameters["Octopus.Environment.Name"]
$roles = $OctopusParameters["Octopus.Machine.Roles"]
#Check to see if the current machine is in the DMZ, then ensure we update
#the environment string to be uatdmz instead of uat
if($roles -like '*DMZ*'){
$environment = "$($environment)dmz".ToLower()
}else{
$environment = "$($environment)".ToLower()
}
Write-Output $roles
Write-Output "Environment set to $($environment)"
#Rename all the default configurations first
foreach($configFile in $configs)
{
#Advice.Web.DevOnline.Config
$currentFile = $($configFile.FullName)
#[Advice, Web, Config]
$currentFileSplit = $($configFile.Name.ToLower()).Split(".")
#Advice.Web
$currentFileName = "$($($currentFileSplit)[0..($($currentFileSplit).Count-3)] -join ".")"
#Config
$currentFileExt = "$($currentFileSplit[$($currentFileSplit).Length-1])"
#DevOnline
$currentFileEnvironment = "$($currentFileSplit[$($currentFileSplit).Length-2])"
#If it's web.config (without the .devonline) then lets rename it to keep it
if(!($currentFileEnvironment.Contains("online") -or $currentFileEnvironment.Contains("offline"))){
Write-Output "\> $currentFile => $($currentFile).default"
Rename-Item -Path "$currentFile" -NewName "$($currentFile).default"
continue
}
}
#Rename all the Environment Specific ones
$configs = Get-ChildItem -Path "$($OctopusParameters['Octopus.Action.Package.InstallationDirectoryPath'])\*" -Include *.config,*.json
foreach($configFile in $configs)
{
#Advice.Web.DevOnline.Config
$currentFile = $($configFile.FullName)
#[Advice, Web, Config]
$currentFileSplit = $($configFile.Name.ToLower()).Split(".")
#Advice.Web
$currentFileName = "$($($currentFileSplit)[0..($($currentFileSplit).Count-3)] -join ".")"
#Config
$currentFileExt = "$($currentFileSplit[$($currentFileSplit).Length-1])"
#DevOnline
$currentFileEnvironment = "$($currentFileSplit[$($currentFileSplit).Length-2])"
#If the current config file currentFileEnvironment is equal to currentFileEnvironment'Online'; rename it to applicationName.Config
if($currentFileEnvironment -eq "$($environment.ToLower())online"){
Write-Output "$> $currentFile => $($currentFileName).$($currentFileExt)"
Rename-Item -Path "$currentFile" -NewName "$($currentFileName).$($currentFileExt)"
}else{
Remove-Item $configFile
}
}
}
######################################################################################################################################################################################
# Script
######################################################################################################################################################################################
#Variables
$ProjectName=$OctopusParameters['ProjectName']
$IISPortHTTP=$OctopusParameters['IISPortHTTP']
$IISPortHTTPS=$OctopusParameters['IISPortHTTPS']
$OctopusPackageInstalledDirectoryPath=$OctopusParameters['Octopus.Action.Package.InstallationDirectoryPath']
$MachineName=$OctopusParameters['Octopus.Machine.Name']
Write-Host "::Project Path: ${OctopusPackageInstalledDirectoryPath}"
Get-ChildItem -Path "${OctopusPackageInstalledDirectoryPath}"
#Validation
if([string]::IsNullOrEmpty($ProjectName)){
Write-Error "Project Name is Null or Empty. Exiting."
return
}
if([string]::IsNullOrEmpty($IISPortHTTP)){
Write-Error "IISPort is Null or Empty. Exiting"
return
}
#Generated Variables
$ProjectAppPool="${ProjectName}Pool"
$ProjectWebSite="$ProjectName"
$ProjectWebApp="My$ProjectName"
$iisAppPoolDotNetVersion="v4.0"
#File System
$InstallLocation="E:\ILRetail\eBusiness\Applications\${ProjectName}"
$ProjectWebsiteInstallLocation="${InstallLocation}.VD"
$ProjectWebAppInstallLocation="${InstallLocation}"
#Transform Files
Transform-Configurations
#Check if there is an Web Application to Remove
if (Test-Path "IIS:\Sites\${ProjectWebSite}\${ProjectWebApp}"){
Write-Host "WebApplication IIS:\Sites\${ProjectWebSite}\${ProjectWebApp} found. Deleting..."
Remove-WebApplication "${ProjectWebApp}" -Site "${ProjectWebSite}"
}
#Check if there is a Web Site to Remove
if (Test-Path "IIS:\Sites\${ProjectWebSite}"){
Write-Host "Site IIS:\Sites\${ProjectWebSite} found. Deleting..."
Remove-WebSite ${ProjectWebSite}
}
#Check if there is an App Pool to remove
if (Test-Path "IIS:\AppPools\${ProjectAppPool}" -pathType container){
Write-Host "AppPool IIS:\AppPools\${ProjectAppPool} found. Deleting..."
Remove-WebAppPool $ProjectAppPool
}
#Create new App Pool and set to NetworkService and restart at 3am with .NET Version V4.0
$appPool = New-Item "IIS:\AppPools\${ProjectAppPool}"
$appPool | Set-ItemProperty -Name managedRuntimeVersion -Value $iisAppPoolDotNetVersion
$appPool | Set-ItemProperty -Name processModel.identityType -Value 2
$appPool | Set-ItemProperty -Name Recycling.periodicRestart.time -Value 0
$appPool | Set-ItemProperty -Name Recycling.periodicRestart.schedule -Value @{value="03:00"}
#Get Properties#Get-Item "${iisAppPoolName}" | select *
#Create Website
Refresh-Directory -DirectoryPath "${ProjectWebsiteInstallLocation}"
New-WebSite -Name ${ProjectName} -Port ${IISPortHTTP} -IPAddress "*" -PhysicalPath "${ProjectWebsiteInstallLocation}" -ApplicationPool "${ProjectAppPool}"
#Create Web Application
Refresh-Directory -DirectoryPath "${ProjectWebAppInstallLocation}"
New-WebApplication -Name "${ProjectWebApp}" -Site "${ProjectWebSite}" -PhysicalPath "${ProjectWebAppInstallLocation}" -ApplicationPool "${ProjectAppPool}"
#Copy files
Write-Host "::Project Path: ${OctopusPackageInstalledDirectoryPath}"
Get-ChildItem -Path "${OctopusPackageInstalledDirectoryPath}"
Copy-Item -Path "${OctopusPackageInstalledDirectoryPath}\*" -Recurse -Destination "${ProjectWebAppInstallLocation}" -Container
#Start Sites
Start-Website "${ProjectName}"
Start-WebAppPool -Name "${ProjectAppPool}"
#Dummey Request
#$webclient = New-Object Net.WebClient
#$webclient.DownloadString("http://127.0.0.1:${IISPortHTTP}/${$ProjectWebApp}");
#HTTPS Code
$dnsName = '${MachineName}:${IISPortHTTPS}'
$newCert = New-SelfSignedCertificate -DnsName $dnsName -CertStoreLocation cert:\LocalMachine\My
# get the web binding of the site
$binding = Get-WebBinding -Name $ProjectWebSite -Protocol "https"
# set the ssl certificate
$binding.AddSslCertificate($newCert.GetCertHashString(), "my")
New-WebBinding -Name "Default Web Site" -IPAddress "*" -Port 80 -HostHeader TestSite
New-WebBinding -Name "${ProjectWebSite}" -IPAddress * -Port ${IISPortHTTPS} -Protocol "https"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment