IIS 6 ServerBindings 与 WMI 和 Powershell v2
我长期以来一直有一堆针对 IIS 6 的 VBS 自动化,其中一个在多个配对服务器场上获取/设置复杂的服务器绑定,每个服务器场有数十个应用程序,每个应用程序有 3-12 个主机标头。每个应用程序都有主机名、完全限定的主机名和启用灾难恢复的主机名,因此手动维护它们可能会很混乱。
我使用 ADSI 完成了所有 vbs 工作,但我认为从完整服务器维护的角度来看,WMI 可能比 ADSI 更灵活。如果我错了,请纠正我。因此,现在我正在尝试升级到 PowerShell + WMI,为 Windows 2008 + IIS 7.5 做好准备。我很享受学习过程,但我在这个问题上遇到了障碍。
我可以通过 WMI 获取/设置 IIS 6 Web 服务器上的所有属性(ServerBindings 除外)。我觉得我已经很接近了,但我缺少一些遏制层,而且我无法将我正在构建的对象转换为正确的自动化对象。
以下代码可以很好地获取并读取 ServerBindings。我只是想不出一种方法来写回我的更改。欢迎任何建议。
$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()
}
到目前为止我很喜欢 Powershell,因此感谢您提供的任何帮助。
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.
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.
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.
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.
$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()
}
I'm liking Powershell so far, so thanks for any help you're able to offer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧。我因重大磁盘故障而分心。乐趣永远不会停止。
不管怎样,这个问题的解决方案比我提出的更简单:
ServerBindings 是一个数组,但它喜欢成为一个自己类型的数组。我试图从头开始构建阵列,但我的自制阵列在 Powershell 中感觉不太对劲。因此,将数组从 ServerBindings 中拉出到一个新变量中,操作该变量,然后将操作后的变量分配回 ServerBindings 属性。这样可以保证所有正确的输入。它像丝绸一样光滑,而且看起来比 ADSI 更容易。
Alright. I got distracted with major disk failures. The fun never stops.
Anyway, the solution to this problem is simpler than I'd made it:
ServerBindings is an array, but it likes to be an array of its own kind. I was trying to build the array from scratch, but my home-rolled array didn't smell right to Powershell. So, pull the array out of ServerBindings into a new variable, manipulate the variable, then assign the manipulated variable back to the ServerBindings property. That keeps all the right typing in place. It's smooth as silk, and seems easier than ADSI.