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.