views:

298

answers:

2

I can create an IISWebVirtualDir or IISWebVirtualDirSetting with WMI, but I've found no way to turn the virtual directory into an IIS Application. The virtual directory wants an AppFriendlyName and a Path. That's easy because they're part of the ...Setting object. But in order to turn the virtual directory into an App, you need to set AppIsolated=2 and AppRoot=[its root].

I cannot do this with WMI. I'd rather not mix ADSI and WMI, so if anyone can coach me through to amking this happen in WMI I'd be very happy.

Here's my demo code:

$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

Any thoughts?

Update with working code

 $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()

Thanks Garrett and Glenn.

+1  A: 

Looks like you are missing the AppCreate2 command? http://arcware.net/creating-iis-applications-with-powershell/

Glenn Sandoval
Glenn, the link you provided showed the AppCreate2 method being called on an ADSI object. That threw me off the scent (being easily distracted and all. :-) ) WMI does provide the same method, though, and it does work smoothly.
codepoke
+1  A: 

This is not tested, but how about something along these lines:

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

$v.PSBase.InvokeMethod("AppCreate2", $appCreateParams, null)
Garett
Garett, your code didn't work at all, but you convinced me to look for the AppCreate2 method in the WMI doc, and there it was. It'd been staring at me all along. The actual answer was as simple as $v.AppCreate2(2), and curse my eyes for failing me again. :-) Thanks!
codepoke
InvokeMethod should work, but the syntax may be a little off. regardless, I'm glad to hear that you were able to find a solution.
Garett