Powershell: How to Create Virtual Directories / IIS 6 Web Application Under Subfolder

I am trying to create a / VirtualDirectory web application under a specific subfolder of the IIS 6 website using Powershell as shown below:

IIS website structure <lt; β†’> Structure of the physical directory

Test (website) ----------------> c:\InetPub SubDirectory ------------------> ..\Subdirectory gadgets (Web App) -----------------> ..\Gadgets 

Script

 $WebSiteName = "Test" $virtualDirName = "subdirectory\gadgets" $appPoolName = "DefaultAppPool" $VirtalDirHomePath = "c:\InetPub\Subdirectory\Gadgets" $iisWebSite = Get-WmiObject "IISWebServerSetting" ` -Namespace "root\MicrosoftIISv2" ` -filter "ServerComment like '%$WebSiteName%'" $virtualDirSettings = [wmiclass] "root\MicrosoftIISv2:IIsWebVirtualDirSetting" $newVDir = $virtualDirSettings.CreateInstance() $newVDir.Name = ($iisWebSite.Name + '/ROOT/' + $virtualDirName) $newVDir.Path = $VirtalDirHomePath $newVDir.Put(); $nvdir = $iisWebSite.Name + '/ROOT/' + $virtualDirName $nvdir = $nvdir.Replace("\", "/") $v = Get-WmiObject -Class IIsWebVirtualDir -Namespace root\microsoftiisv2 ` -filter "Name='$nvdir'" #Create WebAppliction $v.AppCreate3(2, $appPoolName, 1) 

If I specify a $virtualDirName with a forward slash path separator ( subdirectory/gadgets ), calling $newVDir.Put() raises the following exception

Aborting the "Put" call with "0" argument (s): "Win32: the system cannot find the path specified.

If I change $virtualDirName using the backslash path separator (subdirectory \ gadgets), the call to $newVDir.Put() returns successfully.

I am not sure if this is correct.

Is there a better way to create a web application / VirtualDirectory under a specific subfolder and how can I list the VirtualDirectory / WebApplication created under a subfolder.

+4
source share
2 answers

An alternative solution to create a virtual directory in IIS 6.0 using scripts that PowerShell does not include is to use the iisvdir.vbs script:

 SET webSiteName=Test SET virtualDirName=subdirectory/gadgets SET virtualDirHomePath=C:\InetPub\Subdirectory\Gadgets cscript %SystemRoot%\system32\iisvdir.vbs /create %webSiteName% %virtualDirName% %virtualDirHomePath% 

Note that the path to the virtual directory in virtualDirName is specified with a slash .

You can also list virtual directories in a specific path using the same iisvdir.vbs script:

 cscript %SystemRoot%\system32\iisvdir.vbs /query %webSiteName%/%virtualDirName% 
+4
source

Give it a try. It connects to the root of website 1 (default website). Creates an IIsWebDirectory object for the gadget folder and assigns an application pool to it.

 $root = [adsi] "IIS://localhost/W3SVC/1/ROOT" $vDir = $root.Create("IIsWebDirectory", "SubDirectory\Gadgets") $vDir.AppCreate3(2, "DefaultAppPool", $false) $vDir.AppFriendlyName = "Andy Test" $vDir.SetInfo() 

If you need to connect to a website other than the default website, you can get the website ID with this command:

 ([adsi] "IIS://localhost/W3SVC").psbase.Children | ? {$_.psbase.schemaclassname -eq "IIsWebServer" } | select Path, ServerComment 

Output:

 Path ServerComment ---- ------------- IIS://localhost/W3SVC/1 {Default Web Site} IIS://localhost/W3SVC/2 {WHS site} 
+3
source

All Articles