WebDev.WebServer PowerShell Function

In experimenting with NWSGI yesterday, I found I wanted the ability to launch the development web server that ships with Visual Studio (WebDev.WebServer.exe) from the command line. I hacked up the following PowerShell function and dropped it into my $profile so I can easily launch the web server in any directory any time I need. Thought I’d share:

function webdev($path,$port=8080,$vpath='/')
{
    $spath = 'C:\Program Files\Common Files\microsoft shared\DevServer9.0\WebDev.WebServer.EXE'
    $rpath = resolve-path $path  
    $params = "/path:`"$rpath`" /port:$port /vpath:$vpath"  

    $ignore = [System.Diagnostics.Process]::Start($spath, $params)  
    "Started WebDev Server for '$path' directory on port $port"  
}

There’s probably an easier way to launch an exe with parameters than Sys.Diags.Process.Start, but it works. Using resolve-path is the key, that lets me pass in a relative path on the command line, but the script converts it to an absolute path in order to pass it to the webdev server. Also, I’m not sure I should have hard coded the path to the exe, but again it works and it’s not like it’s tough to change.

Enjoy.

Update: Tomas Restrepo pointed out an easier way to start the process:

&'C:\Program Files\Common Files\microsoft shared\DevServer9.0\WebDev.WebServer.EXE' "/path:$rpath" "/port:$port" "/vpath:$vpath"

I couldn’t figure out how to correctly launch the exe when the physical path to serve has a space in it. Thanks Tomas.

Comments:

Pretty cool Harry. There's actually an easier way of running it, something like this would work just fine: &'C:Program FilesCommon Filesmicrosoft sharedDevServer9.0WebDev.WebServer.EXE' "/path:$rpath" "/port:$port" "/vpath:$vpath"
I wrote a similar function some time ago. It's a bit longer, but it works on x64, and on machines with only .NET FW 2.0 installed: # Start-WebDevServer function TryLocateWebDevServer($EnvVar, $SubPath) { $e = 'env:' + $EnvVar if (Test-Path $e) { $wdpath = Join-Path (Get-Content $e) (Join-Path $SubPath 'WebDev.WebServer.EXE') if (Test-Path $wdpath) { return $wdpath } } return $FALSE } function Start-WebDevServer($Path, $Port=8080, $VPath='/') { if (-not $Path -or -not (Test-Path $Path)) { Throw "Invalid Path specified!" } $locations = ('CommonProgramFiles(x86)', 'Microsoft SharedDevServer9.0'), ('CommonProgramFiles', 'Microsoft SharedDevServer9.0'), ('SystemRoot', 'Microsoft.NETFrameworkv2.0.50727') foreach ($l in $locations) { $wdpath = TryLocateWebDevServer $l[0] $l[1] if ($wdpath) { break; } } if (-not $wdpath) { Throw 'Cannot locate WebDev.WebServer.EXE!' } $rpath = Resolve-Path $Path Write-Host "Starting WebDev.WebServer located at:" Write-Host " $wdpath" Write-Host " Parameters: ""/path:$rpath"" ""/port:$Port"" ""/vpath:$VPath""" & $wdpath "/path:$rpath" "/port:$Port" "/vpath:$VPath" } Set-Alias webdev Start-WebDevServer Cheers!
@C-J, I really like how you added the function to find the exe rather than hard coding it.
> $ignore = [System.Diagnostics.Process]::Start(...) There are three canonical PSH ways to ignore the result of an expression: - Assign to $null - Redirect to $null (.... > $null) - Cast to [void]