Sometimes you would like to start IIS Express without hitting the F5 or Shift F5 in Visual Studio. This is especially helpful when you would like to run integration, performance or load tests continuously on your own machine. To enable this scenario you need to start IIS Express as a background process, otherwise the load or performance test will hang. One of the possibilities to achieve this is PowerShell.

Starting IIS Express in PowerShell as background process

Starting IIS Express from the command line isn’t something magic. The only problem with classic console shell is that the parent process of console is keeping the IIS Express alive. This is good for debugging but bad for us than on this way console never returns and let test waiting.

Start-Process -FilePath "c:\program files\iis express\iisexpress.exe" -ArgumentList /site:WhatAQuote.WebApplication -WindowStyle Hidden

Run-IIS-Express.ps1

The script above is starting already registered application but you can use any argument(s) provided by IIS Express command line. The really important aspect here is to use Start-Process command.

You want use Stop-Process to stop already started IIS Express.

Stop-Process -processname iisexpress

Stop-IIS-Express.ps1

One important thing. To start or stop process you need special execution policy, RemoteSigned, otherwise you will get the error like this one:

File cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
+ CategoryInfo : SecurityError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnauthorizedAccess

Normally you will set execution policy in PowerShell console. But it seems that scripts started from Visual Studio have they own policy. To set them, start Windows PowerShell ISE as a administrator and set the execution policy here.

image

Run Performance or Load Tests with IIS Express

To run IIS Express whenever you run Load or Performance test you will need to set Setup and Cleanup Scripts. For this purpose I created Setup.bat and Cleanup.bat files.

powershell C:\anecon.visualstudio.com\WhatAQuote\Main\Source\WhatAQuoteSolution\Scripts\Run-IIS-Express.ps1

Setup.bat

powershell C:\anecon.visualstudio.com\WhatAQuote\Main\Source\WhatAQuoteSolution\Scripts\Stop-IIS-Express.ps1

Cleanup.bat

To call these scripts you open Local.testsettings file in Visual Studio and set Setup and Cleanup scripts file paths.

image

That is. Now each time when you run Load or Performance Test, IIS Express is starting automatically.