Passion * Technology * Ruthless Competence

Thursday, March 20, 2008

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\DevServer\9.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\DevServer\9.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.

Posted By Harry Pierson at 2:39 PM Pacific Standard Time
Thursday, March 20, 2008 5:01:40 PM (Pacific Standard Time, UTC-08:00)
Pretty cool Harry. There's actually an easier way of running it, something like this would work just fine:

&'C:\Program Files\Common Files\microsoft shared\DevServer\9.0\WebDev.WebServer.EXE' "/path:$rpath" "/port:$port" "/vpath:$vpath"
Friday, March 21, 2008 2:28:02 PM (Pacific Standard Time, UTC-08:00)
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 Shared\DevServer\9.0'),
('CommonProgramFiles', 'Microsoft Shared\DevServer\9.0'),
('SystemRoot', 'Microsoft.NET\Framework\v2.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 Berg
Monday, March 24, 2008 10:19:05 AM (Pacific Standard Time, UTC-08:00)
@C-J, I really like how you added the function to find the exe rather than hard coding it.
DevHawk
Wednesday, March 26, 2008 6:57:11 AM (Pacific Standard Time, UTC-08:00)
> $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]
Richard
Comments are closed.

SoCal Code Camp

PDC08

patterns & practices
Summit 2008

Change Congress
Recent Bookmarks
Tags .NET Framework (2) ADO.NET (5) Agile (7) AJAX (3) Architecture (284) Guidance (6) Interop (2) Modelling (61) Patterns (7) Process (4) SOA (93) Web Services (5) ASP.NET (24) Battlestar Galactica (3) BI (2) BizTalk (4) Blogging (115) dasBlog (11) Podcasting (4) BPM (1) C# (10) C++ (4) Capitals (5) CardSpace (3) CLR (2) College Football (10) Comedy Central (1) Community (81) Concurrency (6) Consumer Electronics (1) Database (13) Dependency Injection (2) Development (117) C Plus Plus (1) Embedded (5) Lanugages (37) Media (2) P2P (11) Rotor (1) SharePoint (6) SOP (3) DIY (1) DLR (16) Domain Specific Languages (13) Durable Messaging (5) Dynamic Languages (10) Dynamic Silverlight (1) Education (3) Enterprise 2.0 (1) Entertainment (14) ETech (15) F# (51) Functional Programming (17) Game Development (2) Guidance Automation (3) Hardware (8) HawkEye (3) Hockey (29) Home Electronics (1) Home Network (5) Humor (5) IASA (1) Idempotence (3) infrastructure (5) Instrumentation (4) Integration (2) IronPython (45) IronRuby (12) Java (2) Job (3) LINQ (23) Live Mesh (2) Lost (1) Master Data Management (1) Media 2.0 (6) Microsoft (30) MIX06 (2) Mobile Phone (1) Monads (5) Morning Coffee (172) Object Oriented (4) Office (5) Open Source (5) Open Space (2) Operations (3) Other (135) Art (1) Books (1) Family (31) Games (18) General Geekery (26) Home Theater (1) Movies (23) Music (20) Politics (3) Society (1) Sports (37) Working at MSFT (15) Parallel Programming (3) Parsing Expression Grammar (16) patterns & practices (2) PDC08 (5) Politics (47) PowerPoint (2) PowerShell (34) Presentation (5) Projects (1) HawkWiki (1) Python (4) Quote of the Day (4) Refactoring (1) Research (2) REST (18) Reuse (5) Robotics (2) Rock Band (4) Rome (5) Ruby (23) Ruby on Rails (1) Sci-Fi (2) Scripting (4) Security (3) Service Broker (14) SharePoint (2) Silverlight (18) Social Software (1) Software + Services (2) Software Design (1) Software Factories (11) Software Industry (1) Spark (1) SQL Server (2) Stephen Colbert (1) TechEd (7) TechEd06 (1) TechRec League (1) Television (6) Travel (6) Unified Client (1) Unit Testing (4) USC (1) UX (1) Virtual PC (2) Visual Basic (1) Visual Studio (20) Volta (2) Washington Capitals (34) WCF (31) Web 2.0 (65) Web Services (5) WF (21) Windows Live (23) WPF (7) Xbox (1) Xbox 360 (53) XML (11) XNA (14) Zune (4)
Disclaimer: The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Inappropriate comments will be deleted at the authors discretion.