Skip to content

Instantly share code, notes, and snippets.

@drmohundro
Last active December 25, 2018 06:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save drmohundro/5a131d7ff6f291a33334 to your computer and use it in GitHub Desktop.
Save drmohundro/5a131d7ff6f291a33334 to your computer and use it in GitHub Desktop.
PowerShell functions to start/stop IIS Express in the specified directory as a background job
$jobName = 'IisExpressJob'
function Start-IisExpress($pathToSource) {
Start-Job -Name $jobName -Arg $pathToSource -ScriptBlock {
param ($pathToSource)
& 'C:\Program Files (x86)\IIS Express\iisexpress.exe' /port:1234 /path:$pathToSource
}
}
function Stop-IisExpress {
Stop-Job -Name $jobName
Remove-Job -Name $jobName
}
@drmohundro
Copy link
Author

To use, just dot source them from your $profile. I use a slightly customized version that doesn't require the source. Change as needed.

@drmohundro
Copy link
Author

Fixed issue with the argument list getting passed to the job.

@michaelthyregod
Copy link

Slightly modified functions that checks for the jobname if it exists, and also parses arguments as parameters - just if it is usefull

Function Start-IISExpress (
[Parameter(Mandatory=$true)][string]$port = "8080",
[Parameter(Mandatory=$true)][string]$path,
[Parameter(Mandatory=$true)][string]$jobName = "IISExpressJob",
[Parameter(Mandatory=$false)][string]$iisExpressExe = "C:\Program Files\IIS Express\iisexpress.exe"
)
{
if ( [bool](get-job -Name $jobName -ea silentlycontinue) )
{
Stop-IISExpress -JobName $jobName
}

Start-Job -Name $jobName -Arg $iisExpressExe, $port, $path -ScriptBlock {
    param ($iisExpressExe, $port, $path)
    Start-process $iisExpressExe -ArgumentList "/port:$port /path:$path" -WindowStyle Hidden
    Start-Sleep -m 1000
}

Write-Host "Site started"

}

Function Stop-IISExpress(
[Parameter(Mandatory=$true)][string]$jobName = "IISExpressJob"
)
{
if ( [bool](get-job -Name $jobName -ea silentlycontinue) )
{
Stop-Job -Name $jobName
Remove-Job -Name $jobName
}
}

@mohankumarp
Copy link

Stop-IISExpress is not stopping website which is started by Start-IISExpress , Any clue?

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