Last active
April 22, 2016 04:11
-
-
Save worldspawn/ed3e16accbdefaa8ee35 to your computer and use it in GitHub Desktop.
Powershells script to launch iisexpress. Allows user to specify process env variables, use a unique applicationhost.config and more
This file contains hidden or 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
{ | |
"name": "testapp", | |
"path": "wwwroot", | |
"variables": { | |
"something": null, | |
"foo": "bar" | |
} | |
} |
This file contains hidden or 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
Param ( | |
[switch] $open | |
) | |
function CreateNewConfig($configPath, $name, $root, $port, $hostname) { | |
#configure a default config | |
if ($hostname -eq $null) { | |
$hostname = "localhost" | |
} | |
cp "$env:ProgramFiles\IIS Express\config\templates\PersonalWebServer\applicationhost.config" "$configPath\${name}\applicationHost.config" -Force | |
. "$env:ProgramFiles\IIS Express\appcmd.exe" set config -section:system.applicationHost/sites /-"[id='1']" /commit:apphost /AppHostConfig:"$configPath\${name}\applicationHost.config" | Out-Null | |
. "$env:ProgramFiles\IIS Express\appcmd.exe" set config -section:system.applicationHost/sites /+"[name='$name',id='2']" /commit:apphost /AppHostConfig:"$configPath\${name}\applicationHost.config" | Out-Null | |
. "$env:ProgramFiles\IIS Express\appcmd.exe" set config -section:system.applicationHost/sites /+"[name='$name',id='2'].bindings.[protocol='http',bindingInformation=':${port}:${hostname}']" /commit:apphost /AppHostConfig:"$configPath\${name}\applicationHost.config" | Out-Null | |
. "$env:ProgramFiles\IIS Express\appcmd.exe" set config -section:system.applicationHost/sites /+"[name='$name',id='2'].[path='/']" /commit:apphost /AppHostConfig:"$configPath\${name}\applicationHost.config" | Out-Null | |
. "$env:ProgramFiles\IIS Express\appcmd.exe" set config -section:system.applicationHost/sites /+"[name='$name',id='2'].[path='/'].[path='/', physicalPath='$root']" /commit:apphost /AppHostConfig:"$configPath\${name}\applicationHost.config" | Out-Null | |
return "$configPath\${name}\applicationHost.config" | |
} | |
function WriteExistingConfig($rcpath, $name, $root, $port, $hostname) { | |
if ($hostname -eq $null) { | |
$hostname = "localhost" | |
} | |
$ctpath = [io.path]::combine($rcpath, "applicationhost.config") | |
cp "$ctpath" "$configPath\${name}\applicationHost.config" -force | |
#configure the provided config's path and port number | |
. "$env:ProgramFiles\IIS Express\appcmd.exe" set config -section:system.applicationHost/sites "/site[name='$name'].bindings.[protocol='http'].bindingInformation::${port}:${hostname}" /AppHostConfig:"$configPath\${name}\applicationHost.config" | Out-Null | |
. "$env:ProgramFiles\IIS Express\appcmd.exe" set config -section:system.applicationHost/sites "/site[name='$name'].application[path='/'].virtualDirectory[path='/'].physicalPath:$root" /AppHostConfig:"$configPath\${name}\applicationHost.config" | Out-Null | |
return "$configPath\${name}\applicationHost.config" | |
} | |
function CreateRcConfig($configFilePath, $rcconfigpath, $rcpath, $rcconfig) { | |
$min = 10000 | |
$max = 40000 | |
$port = $min + [System.Math]::Abs("$rcconfigpath".GetHashCode()) % ($max-$min) | |
$root = [io.path]::Combine($rcpath, $rcconfig.path) | |
$hostname = $rcconfig.hostname | |
if ($hostname -eq $null) { | |
$hostname = "localhost" | |
} | |
$config = [pscustomobject]@{ | |
variables = $rcconfig.variables | |
port = $port | |
root = $root | |
name = $rcconfig.name | |
hostname = $hostname | |
} | |
new-item -itemtype file $configFilePath -force | Out-Null | |
sc $configFilePath (ConvertTo-Json $config) | Out-Null | |
return $config | |
} | |
function ResolveRcPath () { | |
$rcpath = Get-Location | |
while (!(Test-Path ([io.path]::combine($rcpath, ".iisexpressrc"))) -and !($rcpath -eq "")) { | |
$rcpath = Split-Path -Path $rcpath -Parent | |
} | |
return $rcpath | |
} | |
$configPath = "$env:UserProfile\.iisexpress" | |
if (!(Test-Path $configPath)) { | |
mkdir $configPath | |
} | |
$rcpath = ResolveRcPath | |
if ($rcpath -eq "") { | |
Write-Host "no rc found" | |
exit 1 | |
} | |
$rcconfigpath = [io.path]::combine($rcpath, ".iisexpressrc") | |
$rcconfig = ConvertFrom-Json -InputObject (Gc $rcconfigpath -Raw) | |
$name = $rcconfig.name | |
$configFilePath = [io.path]::combine($configPath, $name, "." + $name + "rc") | |
$config = $null | |
if (Test-Path ($configFilePath)) { | |
$config = ConvertFrom-Json -InputObject (Gc $configFilePath -Raw) | |
} | |
else { | |
$config = CreateRcConfig $configFilePath $rcconfigpath $rcpath $rcconfig | |
} | |
$appHostPath = $null | |
if (Test-Path ([io.path]::combine($rcpath, "applicationhost.config"))) { | |
$appHostPath = WriteExistingConfig $rcpath $config.name $config.root $config.port $config.hostname | |
} | |
else { | |
$appHostPath = CreateNewConfig $configPath $config.name $config.root $config.port $config.hostname | |
} | |
$process = new-object System.Diagnostics.Process | |
Get-Member -inputObject $config.variables -memberType Properties | foreach { | |
$value = $config.variables | select -exp $_.name | |
$process.StartInfo.EnvironmentVariables.Set_Item("$($name):$($_.name)", $value) | |
} | |
$process.StartInfo.UseShellExecute = $false | |
$process.StartInfo.FileName = "$env:ProgramFiles\IIS Express\iisexpress.exe" | |
$process.StartInfo.Arguments = "/config:`"$appHostPath`" /site:$name" | |
$started = $process.Start() | |
if ($started) { | |
if ($open) { | |
start "http://$($config.hostname):$($config.port)/" | |
} | |
$process.WaitForExit() | |
rm $appHostPath | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this script will look in the cwd and walk up the parents till it finds a file called .iisexpressrc. it will then read the config in that to launch iisexpress. it will also look for an applicationhost.config file in the same folder and use that otherwise it creates a new one based on the installed config template.
the config file is copied to ~/.iisexpress/{name}/.{name}rc
This is the copy that the configuration settings will be read from and can be customised for the current users needs (such as overriding the variable defaults)