使用PowerShell v2通过WMI / ADSI创建IIS 6虚拟目录

时间:2010-05-18 21:00:25

标签: iis powershell wmi adsi

我可以使用WMI创建IISWebVirtualDir或IISWebVirtualDirSetting,但我发现无法将虚拟目录转换为IIS应用程序。虚拟目录需要AppFriendlyName和Path。这很简单,因为它们是......设置对象的一部分。但是为了将虚拟目录转换为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()

感谢加勒特和格伦。

2 个答案:

答案 0 :(得分:1)

看起来你错过了AppCreate2命令? http://arcware.net/creating-iis-applications-with-powershell/

答案 1 :(得分:1)

这没有经过测试,但是这些方面的内容如何:

$appCreateParams = $v.PSBase.GetMethodParameters("AppCreate2")
$appCreateParams["AppMode"] = 2

$v.PSBase.InvokeMethod("AppCreate2", $appCreateParams, null)