Powershell : How to create IIS 6 Virtual Directory

2020-04-08 13:34发布

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.

2条回答
Bombasti
2楼-- · 2020-04-08 14:27

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.

$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 web site you can get the ID of the website 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}
查看更多
Lonely孤独者°
3楼-- · 2020-04-08 14:32

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:

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 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:

cscript %SystemRoot%\system32\iisvdir.vbs /query %webSiteName%/%virtualDirName%
查看更多
登录 后发表回答