Skip to content

Instantly share code, notes, and snippets.

@mhinze
Created February 22, 2012 14:39
Show Gist options
  • Save mhinze/1885377 to your computer and use it in GitHub Desktop.
Save mhinze/1885377 to your computer and use it in GitHub Desktop.
Sample solutionscript to expose IIS
function global:ExposeIIS()
{
$config = [xml] ( get-content $env:IIS_USER_HOME/config/applicationhost.config )
# Add in new bindings for computer name
foreach($site in $config.configuration."system.applicationHost".sites.site)
{
foreach($bindings in $site.bindings)
{
# no need to add another binding for machine name if its exists
$hasOneAlready = $false
foreach($existingBinding in $bindings.binding)
{
if($existingBinding.bindingInformation -match $env:computername)
{
$hasOneAlready = $true
}
}
if(!$hasOneAlready)
{
foreach($binding in $bindings.binding)
{
if($binding.bindingInformation -match "localhost")
{
# make new IISExpress binding for local machine name
$namebinding = $binding.Clone()
$namebinding.bindingInformation = $namebinding.bindingInformation.Replace("localhost", $env:computername)
$bindings.AppendChild($namebinding)
# make new IISExpress binding for fully qualified local machine name
$namebinding = $binding.Clone()
$namebinding.bindingInformation = $namebinding.bindingInformation.Replace("localhost", $env:computername + ".company.com")
$bindings.AppendChild($namebinding)
# run netsh to add new binding url
$url = $namebinding.bindingInformation -replace "\*?:([^:]*):([^:]*)", 'http://$2:$1/'
$expression = "netsh http add urlacl url=" + $url + " user=everyone"
invoke-expression $expression
$url = $namebinding.bindingInformation -replace "\*?:([^:]*):([^:]*)", 'http://$2.company.com:$1/'
$expression = "netsh http add urlacl url=" + $url + " user=everyone"
invoke-expression $expression
#TODO: This should be transactional somehow
# Right now failing netsh doesn't stop the bindings from being created
}
}
}
}
}
# TODO: Maybe don't save if there was an error? Have to think about it
$config.Save("$env:IIS_USER_HOME/config/applicationhost.config")
}
function global:SealIIS()
{
$config = [xml] ( get-content $env:IIS_USER_HOME/config/applicationhost.config )
# strip bindings for computer name
foreach($site in $config.configuration."system.applicationHost".sites.site)
{
foreach($bindings in $site.bindings)
{
$i = $bindings.binding.length
for($i-le0;$i--)
{
if($bindings.binding[$i].bindingInformation -match $env:computername)
{
# run netsh to remove binding url
$url = $bindings.binding[$i].bindingInformation -replace "\*?:([^:]*):([^:]*)", 'http://$2:$1/'
$expression = "netsh http delete urlacl url=" + $url
invoke-expression $expression
$url = $bindings.binding[$i].bindingInformation -replace "\*?:([^:]*):([^:]*)", 'http://$2.company.com:$1/'
$expression = "netsh http delete urlacl url=" + $url
invoke-expression $expression
# remove binding from IISExpress
$bindings.RemoveChild($bindings.binding[$i])
#TODO: This should be transactional somehow
# Right now failing netsh doesn't stop the bindings from being removed
}
}
}
}
# TODO: Maybe don't save if there was an error? Have to think about it
$config.Save("$env:IIS_USER_HOME/config/applicationhost.config")
}
@mhinze
Copy link
Author

mhinze commented Feb 22, 2012

Fellow on our team wanted to temporarily expose IIS Express to the domain, this is his work in progress.

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