Skip to content

Instantly share code, notes, and snippets.

@ediblecode
Last active September 3, 2018 10:44
Show Gist options
  • Save ediblecode/811cbf6bfe999ad79f5600ff5464b732 to your computer and use it in GitHub Desktop.
Save ediblecode/811cbf6bfe999ad79f5600ff5464b732 to your computer and use it in GitHub Desktop.
Creates a host entry, App Pool and Site in IIS and sets required permissions on the folder. Useful for local dev on a .NET app hosted within IIS.
#requires -version 5
$siteHost = "perflocal"
$siteName = "PerformanceDashboard"
$appPoolName = "PerformanceDashboard"
####################
# INSTALL AND IMPORT CARBON
if (Get-Module -ListAvailable -Name Carbon) {
Write-Host "Carbon is already installed: great!"
} else {
Write-Host "Carbon is required and not installed. Installing now."
Install-Module -Name 'Carbon'
Write-Host "Carbon installed"
}
Import-Module 'Carbon'
####################
# ADD HOSTS ENTRY
Write-Host "Adding hosts file entry"
Set-HostsEntry -IPAddress "127.0.0.1" -HostName $siteHost
Write-Host "Hosts added"
####################
# CHECK FOR IIS
if((Get-Service W3SVC -ComputerName $env:computername) -eq $Null)
{
Write-Warning "Please enable IIS before continuing. See Control Panel -> Turn Windows Features On"
Exit
}
Write-Host "IIS is installed. Creating app pool"
####################
# CHECK FOR WEBSITE
Import-module WebAdministration
$sitePath = "IIS:\Sites\$siteName"
$siteDir = (Get-Item -Path ".\" -Verbose).FullName + "\PerformanceDashboard\"
if (Test-Path $sitePath) {
Write-Host "Site $siteName already exists."
Exit
}
####################
# CHECK/CREATE APP POOL
$appPoolPath = "IIS:\AppPools\$appPoolName"
Write-Host "Checking for AppPool $appPoolName"
if (Test-Path $appPoolPath -pathType container)
{
Write-Host "AppPool $appPoolName already exists"
}
else
{
Write-Host "Creating AppPool $appPoolName"
$appPool = New-Item $appPoolPath
$appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value "v4.0"
Write-Host "AppPool $appPoolName created"
}
####################
# CREATE WEB SITE
Write-Host "Creating site $siteName with url $siteHost"
$iisApp = New-Item $sitePath -bindings @{protocol="http";bindingInformation=":80:" + $siteHost} -physicalPath $siteDir
$iisApp | Set-ItemProperty -Name "applicationPool" -Value $appPoolName
Write-Host "Site $siteName created"
####################
# SET PERMISSIONS
if (Get-Module -ListAvailable -Name NTFSSecurity) {
Write-Host "NTFSSecurity is already installed: great!"
} else {
Write-Host "NTFSSecurity is required and not installed. Installing now."
Install-Module -Name 'NTFSSecurity'
Write-Host "NTFSSecurity installed"
}
Import-Module 'NTFSSecurity'
Write-Host "Enabling permissions for IIS_IUSRS and IUSR"
Add-NTFSAccess -Path $siteDir -Account IIS_IUSRS -AccessRights Modify
Add-NTFSAccess -Path $siteDir -Account IUSR -AccessRights Modify
Write-Host "Permissions added"
# Finally, open up the URL
Start "http://$siteHost"
@ediblecode
Copy link
Author

Use this as a basis for creating a host entry, app pool and site automatically - include it as part of readme steps for example.

You can change AccessRights to be what you need - ReadAndExecute is probably better than Modify!

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