I am trying to create a Web application/VirtualDirectory under a specific subfolder of a IIS 6 website using Powershell as show below:
IIS WebSite Structure <<<>>> Physical Directory Structure
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 the $virtualDirName
with forward slash path separator (subdirectory/gadgets
) , $newVDir.Put()
call throws following exception
Exception calling "Put" with "0" argument(s): "Win32: The system cannot find the path specified.
If I change the $virtualDirName
with backslash path separator (subdirectory\gadgets) $newVDir.Put()
call returns successfully.
I am not sure whether this is the right way.
Is there any better way to create Web Application/VirtualDirectory under a specific subfolder and How can I list VirtualDirectory/WebApplication created under a subfolder.
Give this a try. It connects to the root of website 1 (Default Website). Creates a IIsWebDirectory object for the gadgets folder and assigns it an application pool.
If you need to connect to a website other than the default web site you can get the ID of the website with this command:
Output:
An alternative solution to create a virtual directory in IIS 6.0 through scripting, which doesn't involve PowerShell, is to use the iisvdir.vbs script:
Note that the virtual directory path in
virtualDirName
is specified using forward slashes.You can also list the virtual directories in a specific path using the same iisvdir.vbs script: