title: IIS 6 ServerBindings with WMI and Powershell v2
body: <p>I've long had a bunch of VBS automations for IIS 6, including one that gets/sets complex server bindings on several farms of paired servers, each having dozens of apps, each app having 3-12 host headers. Each app has hostname, hostname-fullyqualified, and Disaster Recovery enabled hostname, so they can be a mess to maintain manually. </p>
  
  <p>I did all my vbs stuff using ADSI, but I'm thinking WMI is probably more flexible than ADSI from a full server maintenance perspective. Please correct me if I'm wrong. So now I'm trying to move up to PowerShell + WMI to prepare for Windows 2008 + IIS 7.5. I'm enjoying the learning process, but I've hit a roadblock on this problem. </p>
  
  <p>I can get/set all properties via WMI on my IIS 6 web servers, except ServerBindings. I feel like I'm close, but I'm missing some layer of containment, and I just can't get the objects I'm building to cast over to the right automation object. </p>
  
  <p>The following code gets and reads the ServerBindings just fine. I simply can't figure out a way to write my changes back. Any advice is welcomed. </p>
  
  <pre><code>$objWMI = [WmiSearcher] "Select * From IISWebServerSetting"
  $objWMI.Scope.Path = "\\" + $server + "\root\microsoftiisv2" 
  $objWMI.Scope.Options.Authentication = 6 
  $sites = $objWMI.Get() 
  foreach ($site in $sites)
  {
      $bindings = $site.psbase.properties | ? {$_.Name -contains "ServerBindings"} 
      foreach ($pair in $bindings.Value.GetEnumerator())
      {
          # The pair object is a single binding and contains the correct data
          $pair
          $pair.IP    
          $pair.Port
          $pair.Hostname
          # And this line will successfully erase the contents of 
          # the ServerBindings
          $bindings.Value = @{}
          # but I can't figure out what to do to update $bindings.Value
      }
      $site.Put()
  }
  </code></pre>
  
  <p>I'm liking Powershell so far, so thanks for any help you're able to offer. </p>