title: In WMI, can I use a join (or something similar) to acquire the IisWebServer object for a site, given server name and deployment location
body: <p>Given a server name and a physical path, I'd like to be able to hunt down the <a href="http://msdn.microsoft.com/en-us/library/ms525545.aspx" rel="nofollow noreferrer">IISWebServer</a> object and <a href="http://msdn.microsoft.com/en-us/library/ms525600.aspx" rel="nofollow noreferrer">ApplicationPool</a>. Website url is also an acceptable input. </p>
  
  <p>Our technologies are IIS <strong>6</strong>, WMI, and access via C# or Powershell 2. I'm certain this would be easier with <a href="http://msdn.microsoft.com/en-us/library/aa347649.aspx" rel="nofollow noreferrer">IIS 7 its managed API</a>. We don't have that yet.</p>
  
  <p>Here's what I can do:</p>
  
  <p>Get a list of IIS virtual directories from <a href="http://msdn.microsoft.com/en-us/library/ms525005.aspx" rel="nofollow noreferrer">IISWebVirtualDirSetting</a> and filter (offline) for the matching physical path.</p>
  
  <pre><code>$theVirtualDir = gwmi -Namespace "root/MicrosoftIISv2" `
      -ComputerName $servername -authentication PacketPrivacy `
      -class "IISWebVirtualDirSetting" `
      | where-object {$_.Path -like $deployLocation}
  </code></pre>
  
  <p>From the virtual directory object, I can get a name (like W3SVC/40565456/root). Given this name, I can get to other goodies, such as the <a href="http://msdn.microsoft.com/en-us/library/ms525545.aspx" rel="nofollow noreferrer">IIS web server object</a>. </p>
  
  <pre><code>gwmi -Namespace "root/MicrosoftIISv2" `
      -ComputerName $servername `
      -authentication PacketPrivacy `
      -Query "SELECT * FROM IisWebServer WHERE Name='W3SVC/40589473'" 
  </code></pre>
  
  <p><strong>The questions, restated:</strong></p>
  
  <p>1) This is a query language. Can I join or subquery so that 1 WMI query statement gets web servers based on IISWebVirtualDir.Path? How?</p>
  
  <p>2) In solving 1, you'll have to explain how to query on the Path property. Why is this an invalid query? <code>"SELECT * FROM IISWebVirtualDirSetting WHERE Path='D:\sites\globaldominator'"</code></p>