Skip to content

Instantly share code, notes, and snippets.

@stephengodbold
Created September 9, 2014 01:13
Show Gist options
  • Save stephengodbold/e01934fd093ed66249c5 to your computer and use it in GitHub Desktop.
Save stephengodbold/e01934fd093ed66249c5 to your computer and use it in GitHub Desktop.
Setup a development environment for IISExpress using *.localtest.me
#requires -version 2.0
param(
[Parameter(Mandatory = $true)]
[string]
$project,
[Parameter(Mandatory = $true)]
[string]
$hostname,
[Parameter(Mandatory = $true)]
[int]
$port,
[Parameter(Mandatory = $true)]
[ValidateSet('http', 'https')]
[string]
$protocol
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
function Set-IISExpressBinding {
param(
$project,
$hostname,
$port,
$protocol
)
$iisExpressConfigPath = Join-Path $env:USERPROFILE 'Documents\IISExpress\config\applicationhost.config'
if (-not(Test-Path($iisExpressConfigPath))) { throw "Could not locate IIS Express config under $iisExpressConfigPath" }
#here be dragons. I blame XML
[xml] $configContent = Get-Content $iisExpressConfigPath
$binding = [String]::Format("*:{0}:{1}", $port, $hostname)
$defaultTLSBinding = [String]::Format("*:{0}:{1}", $port, 'localhost')
[System.Xml.XmlElement] $bindingElement = $configContent.CreateElement('binding')
$bindingElement.SetAttribute('protocol', $protocol)
$bindingElement.SetAttribute('bindingInformation', $binding)
$configContent.configuration."system.applicationHost".sites.site |
Where-Object { $_.name -eq $project } |
% {
$foundBinding = $_.bindings.binding | Where-Object { $_.bindingInformation -eq $binding }
$localhostBinding = $_.bindings.binding | Where-Object { $_.bindingInformation -eq $defaultTLSBinding }
if ((-not($foundBinding)) -and ($localhostBinding)) {
$_.bindings.ReplaceChild($bindingElement, $localhostBinding)
}
}
Set-Content $iisExpressConfigPath $configContent.InnerXml -Force
}
function Set-UrlACL {
param(
$hostname,
$port,
$protocol
)
[UriBuilder] $urlBuilder = New-Object System.UriBuilder @($protocol, $hostname, $port)
$url = $urlBuilder.ToString()
#hi, it looks like you're trying to setup a different kind of environment. Can I help with that?
if (-not([Uri]::IsWellFormedUriString($url, [UriKind]::Absolute))) {
throw "You've supplied an invalid scheme, host name or port and we can't form a valid url!"
}
#netsh likes to see a trailing space, or we'll get an error 87.
if (-not($url.EndsWith('/'))) {
$url = $url + '/'
}
& netsh http add urlacl url=$url user=everyone
}
function Set-SSLCertificate {
param(
$port
)
Get-ChildItem -path cert:\localmachine\my |
Where-Object {
$_.FriendlyName -match 'IIS Express Development Certificate'
} | % {
"netsh http delete sslcert `"ipport=127.0.0.1:$($port)`""
"netsh http add sslcert ipport=127.0.0.1:$($port) appid={214124cd-d05b-4309-9af9-9caa44b2b74a} certstorename=MY certhash=$($_.Thumbprint)"
} | % { $_ | cmd }
}
$CurrentPrincipal = New-Object Security.Principal.WindowsPrincipal( [Security.Principal.WindowsIdentity]::GetCurrent( ) )
if ( -not ($currentPrincipal.IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator ) ) )
{
Write-Error “This script must be executed with elevated permissions!”
}
Set-UrlACL $hostname $port $protocol
Set-SSLCertificate $port
Set-IISExpressBinding $project $hostname $port $protocol
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment