Skip to content

Instantly share code, notes, and snippets.

@MattJeanes
Created October 22, 2018 13:55
Show Gist options
  • Save MattJeanes/0ddab3953822eff2839923bd39164688 to your computer and use it in GitHub Desktop.
Save MattJeanes/0ddab3953822eff2839923bd39164688 to your computer and use it in GitHub Desktop.
Manual launching and handling of IISExpress via command line/launchSettings.json
param([string]$Config, [string]$Site, [switch]$Child=$false)
if(-not $Child){
Write-Host "Starting child process"
$exe = "powershell"
$args = @("-File `"$($MyInvocation.MyCommand.Definition)`"", "-Config `"$Config`"", "-Site `"$Site`"", "-Child")
$args
Start-Process $exe -ArgumentList $args -Wait
}else{
try{
$self = Get-CimInstance -Class Win32_Process -Filter "ProcessId = '$PID'"
$parentPID = $self[0].ParentProcessId
Write-Host "Child process started, parent id $parentPID"
Write-Host "Starting site $Site using config $Config"
$exe = "C:\Program Files\IIS Express\iisexpress.exe"
$args = @("/config:`"$Config`"", "/site:`"$Site`"")
$args
$process = New-Object system.Diagnostics.Process
$si = New-Object System.Diagnostics.ProcessStartInfo
$si.FileName = $exe
$si.Arguments = $args
$si.UseShellExecute = $false
$si.RedirectStandardOutput = $true
$process.StartInfo = $si
$process.Start() | Out-Null
$iisPID = $process.Id
"IIS PID $iisPID"
$finishRead = $false
while((Get-Process -PID $parentPID -ErrorAction SilentlyContinue) -ne $null -and -not $process.HasExited){
if(-not $finishRead){
$line = $process.StandardOutput.ReadLine()
Write-Host $line
if($line -eq "IIS Express is running."){
$finishRead = $true
}
}
Start-Sleep -Milliseconds 100
}
Write-Host "Parent process or IISExpress exited"
if((Get-Process -PID $iisPID -ErrorAction SilentlyContinue) -ne $null) {
Write-Host "Killing IISExpress"
Stop-Process $process
}else{
Write-Host "IISExpress not running"
}
}catch{
Write-Host ($_.Exception)
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment