【发布时间】:2010-05-18 21:00:25
【问题描述】:
我可以使用 WMI 创建 IISWebVirtualDir 或 IISWebVirtualDirSetting,但我没有找到将虚拟目录转换为 IIS 应用程序的方法。虚拟目录需要一个 AppFriendlyName 和一个路径。这很容易,因为它们是 ...Setting 对象的一部分。但是为了将虚拟目录变成一个App,你需要设置AppIsolated=2和AppRoot=[它的root]。
我不能用 WMI 做到这一点。我宁愿不混合 ADSI 和 WMI,所以如果有人能指导我在 WMI 中实现这一点,我会非常高兴。
这是我的演示代码:
$server = 'serverName'
$site = 'W3SVC/10/ROOT/'
$app = 'AppName'
# If I use these args, the VirDir is not created at all. Fails to write read-only prop
# $args = @{'Name'=('W3SVC/10/ROOT/' + $app); `
# 'AppIsolated'=2;'AppRoot'='/LM/' + $site + $app}
# So I use this single arg
$args = @{'Name'=($site + $app)}
$args # Look at the args to make sure I'm putting what I think I am
$v = set-wmiinstance -Class IIsWebVirtualDir -Authentication PacketPrivacy `
-ComputerName $server -Namespace root\microsoftiisv2 -Arguments $args
$v.Put()
# VirDir now exists
# Pull the settings object for it and prove I can tweak it
$filter = "Name = '" + $site + $app + "'"
$filter
$v = get-wmiobject -Class IIsWebVirtualDirSetting -Authentication PacketPrivacy `
-ComputerName $server -Namespace root\microsoftiisv2 -Filter $filter
$v.AppFriendlyName = $app
$v.Put()
$v
# Yep. Changes work. Goes without saying I cannot change AppIsolated or AppRoot
# But ADSI can change them without a hitch
# Slower than molasses in January, but it works
$a = [adsi]("IIS://$server/" + $site + $app)
$a.Put("AppIsolated", 2)
$a.Put("AppRoot", ('/LM/' + $site + $app))
$a.Put("Path", "C:\Inetpub\wwwroot\news")
$a.SetInfo()
$a
有什么想法吗?
使用工作代码更新
$server = 'serverName'
$site = 'W3SVC/11/ROOT/'
$app = 'AppName'
$path = "c:\inetpub\wwwroot\news"
$args = @{'Name'=($site + $app)}
$v = set-wmiinstance -Class IIsWebVirtualDir -Authentication PacketPrivacy
-ComputerName $server -Namespace root\microsoftiisv2 -Arguments $args
$v.AppCreate2(2)
$filter = "Name = '" + $site + $app + "'"
$v = get-wmiobject -Class IIsWebVirtualDirSetting -Authentication PacketPrivacy
-ComputerName $server -Namespace root\microsoftiisv2 -Filter $filter
$v.AppFriendlyName = $app
$v.Path = $path
$v.Put()
感谢加勒特和格伦。
【问题讨论】:
-
另一个 WMI 抱怨。 WMI 不提供任何属性来轻松识别该属性是否是继承的。 ADSI 使它成为一个简单的布尔值。我肯定喜欢在纯 WMI 中执行此操作,但它们并不容易。
-
哈!我知道的很少。叹。当您使用 WMI 创建对象时,继承就被吹走了。 WMI 引入每个继承的属性并将它们硬写入新对象。元数据库像威利旺卡中的紫色女孩一样膨胀。但是,ADSI 属性 IsInherit 并非轻而易举。我可能最终不得不步行父母/孩子来避免创造更好地继承的道具。迷人。
标签: iis powershell wmi adsi