从命令行(或 cscript)创建 IIS 网站?

从命令行(或 cscript)创建 IIS 网站?

有没有办法编写创建 IIS 6 网站的脚本?我无法访问 powershell,因此必须使用常规命令或 cscript 脚本。

答案1

此方法适用于Windows Server 2003 IIS 6.0:

strComputer = "."
Set objWMIService = GetObject _
    ("winmgmts:{authenticationLevel=pktPrivacy}\\" _
        & strComputer & "\root\microsoftiisv2")

Set objWebService = objWMIService.ExecQuery _
    ("Select * From IISWebService")

arrBindings = Array(0)
Set arrBindings(0) = _
    objWMIService.Get("ServerBinding").SpawnInstance_()
arrBindings(0).IP = "192.168.1.1"
arrBindings(0).Port = "80"
arrBindings(0).Hostname = "www.example.com"

For Each objItem in objWebService
    objItem.CreateNewSite "Test Site", arrBindings, _
        "c:\inetpub\wwwroot\testsite"
Next

以下是与或相关的 Microsoft TechNet 脚本存储库的链接在 Internet Information Server 6.0 上管理网站

另一个有用的TechNet 文章解释如何以编程方式创建 AppPool。


这是另一种方法,也适用于 Windows Server 2000 (IIS 5.0)。编写一个 .vbs 文件:

IISWebName = "Name of the Web"
IISWebPath = "D:\Websites\MyWeb"
IISWebHeader = "http://www.example.com"
IISIPAddress = "10.0.0.10"

Set wsShell = CreateObject("WScript.Shell") 
wsShell.Run "iisweb.vbs /create """ & IISWebPath & """ """ & IISWebName & """ /b 80 /i """ & IISIPAddress & """" /d """ & IISWebHeader & """"

如果您想添加虚拟目录,您可以这样做:

oShell2.Run "iisvdir.vbs /create """ & IISWebName & """ pfengine ""D:\Common\engine"""

答案2

您可能还想自定义网站,而不是保留其默认设置。经过一番摸索,我得出了以下结论:

Sub CreateWebsite(name, hostName, physicalPath)
    ' Make connections to WMI, to the IIS namespace on the local machine. Then grab a reference to the WWW service 
    Dim locatorObj : set locatorObj = CreateObject("Wbemscripting.SWbemLocator") 
    Dim providerObj : set providerObj = locatorObj.ConnectServer(".", "root/MicrosoftIISv2") 
    Dim serviceObj : set serviceObj = providerObj.Get("IIsWebService='W3SVC'") 

    'Create a new instance as per splattne's answer
    Dim Bindings : Bindings = Array(0) 
    Set Bindings(0) = providerObj.get("ServerBinding").SpawnInstance_() 
    Bindings(0).IP = "" 
    Bindings(0).Port = "80" 
    Bindings(0).Hostname = hostName

    ' Create the new Web site using the CreateNewSite method of the IIsWebService object. 
    Dim newSitePath : newSitePath = serviceObj.CreateNewSite(name, Bindings, physicalPath) 

    ' CreateNewSite returns a string reference to the created web service. To alter the settings we need to create a string which references the root virtual directory settings.
    Dim settingsPath : settingsPath = Replace(Left(newSitePath, Len(newSitePath) - 1) & "/ROOT'", "IIsWebServer","IIsWebVirtualDirSetting")

    ' Grab a reference to the settings
    Dim settings : set settings = providerObj.get(settingsPath)

    ' By comparing the settings of an existing, manually set up site, and one created with Splattne's method I realised I needed to change the following
    settings.AspEnableParentPaths = True
    settings.AccessFlags = 512
    settings.AccessRead = True
    settings.AuthAnonymous = True
    settings.AuthFlags = 5
    settings.AuthNTLM = True
    settings.AccessScript = True
    settings.AppFriendlyName = name
    settings.Put_()

    ' Set a custom handler for 500 errors. In this case a url called 500.asp. This is a bit hacky but it works.
    settings.HttpErrors(41).Properties_("HttpErrorCode") = 500
    settings.HttpErrors(41).Properties_("HttpErrorSubcode") = "*"
    settings.HttpErrors(41).Properties_("HandlerType") = "URL"
    settings.HttpErrors(41).Properties_("HandlerLocation") = "/500.asp"
    settings.Put_()

    ' Start the service
    Dim newSite: set newSite = providerObj.Get(newSitePath)
    newSite.Start
End Sub

答案3

我认为 PowerShell 确实适用于 IIS 7。您确定要 IIS 6 而不是 IIS 7 吗?IIS 7 有 appcmd 命令,虽然使用起来有点困难,但可以工作。

相关内容