Creating an IIS 6 Virtual Directory with PowerShell v2 over WMI/ADSI

Posted by codepoke on Stack Overflow See other posts from Stack Overflow or by codepoke
Published on 2010-05-18T21:00:25Z Indexed on 2010/05/18 23:20 UTC
Read the original article Hit count: 1097

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about iis

Related posts about powershell