我可以使用 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()
谢谢加勒特和格伦。