tags:

views:

238

answers:

1

Given a valid $server, the following code will move all the ServerBindings from their existing subnet to a nice, shiny new one at 1.1.1.

$objWMI = [WmiSearcher] "Select * From IIsWebServerSetting WHERE Name = 'w3svc/10'" 
$objWMI.Scope.Path = "\\" + $server + "\root\microsoftiisv2"    
$objWMI.Scope.Options.Authentication = 6 
$sites = $objWMI.Get() 
foreach ($site in $sites) 
{
    $bindings = $site.ServerBindings 
   foreach ($binding in $bindings) 
   {
       $binding.IP = $binding.IP -ireplace "^\d{1,3}\.\d{1,3}\.\d{1,3}","1.1.1" 
   } 
   $site.ServerBindings = $bindings 
   $site.Put() 
}

I like it. It works great.

The rub comes when I try to add a new ServerBinding to the list. It's completely smoked me.

I can clone an existing binding with:

   $newBinding = $existingBinding.Clone()
   $newBinding.Hostname = "test." + $newBinding.Hostname
   $bindings += $newBinding

The bindings will have 1 new element, and the new element will be of the same type as its brothers, but when I attempt to update my $site.ServerBindings, I'm toast:

Exception setting "ServerBindings": "Unable to cast object of type'System.Management.Automation.PSObject' to type 'Sys tem.Management.ManagementBaseObject'."

Adding the new element to the original array changes the array from a ManagementBaseObject into a PSObject?

Nothing else I try seems to work, either. I can't add a new element to the $site.ServerBindings value because it's read only.

I appreciate any help.

+1  A: 

Try unwrapping the PSObject before adding the new server binding e.g.:

$bindings += $newBinding.psobject.baseobject

This appears to be a bug in PowerShell 2.0 that I hope gets fixed soon.

Keith Hill
I thank you, and my forehead thanks you!I'm new to all this, but apparently the baseobject is more generic than the wrapped object and therefore casts cleanly? Whatever it is, I really appreciate your answer.
codepoke
Keith Hill