« Previous Next »

Thread: mkw3site.vbs for iis7

Last post 01-14-2009 10:28 AM by adrianmaule. 1 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (2 items)

Sort Posts:

  • 01-13-2009, 3:53 PM

    mkw3site.vbs for iis7

    We have a setup routine for our web servers that allows us to create websites from a central administrative server.  We rely heavily on the previously published mkw3site.vbs (modified by Chris Lowe).  This is fine for our iis6.0 sites, but I'm having difficulty using the script to create websites on iis 7.0.  I'm not suprised by this, but i'm wondering if there is a version of this script that can create websites on an iis 7.0 (2008) server from the iis 6.0 (2003) server where our current set up script resides.

      Here is the code from mkw3site.vbs:

     

    '------------------------------------------------------------------------------------------------
    '
    ' This is a simple script to create a new virtual web server.
    '
    ' Usage: MakeWebSite      <--RootDirectory|-r ROOT DIRECTORY>
    '                         <--Comment|-t SERVER COMMENT>
    '                         [--computer|-c COMPUTER1[,COMPUTER2...]]
    '                         [--HostName|-h HOST NAME[,HOSTNAME2...]]
    '                         [--port|-o PORT NUM]
    '                         [--IPAddress|-i IP ADDRESS]
    '                         [--SiteNumber|-n SITENUMBER]
    '                         [--DontStart]
    '                         [--verbose|-v]
    '                         [--URL Redirectionpath]
    '                         [--URLExact]
    '                         [--URLChildOnly]
    '                         [--URLPermanent]
    '                         [--help|-?]
    '
    ' IP ADDRESS            The IP Address to assign to the new server.  Optional.
    ' HOST NAME             The host name of the web site for host headers.
    '                        WARNING: Only use Host Name if DNS is set up find the server.
    ' PORT NUM              The port to which the server should bind
    ' ROOT DIRECTORY        Full path to the root directory for the new server.
    ' SERVER COMMENT        The server comment -- this is the name that appers in the MMC.
    ' SITENUMBER            The Site Number is the number in the path that the web server
    '                        will be created at.  i.e. w3svc/3
    '
    ' Example 1: MakeWebSite.vbs -r D:\Roots\Company11 --DontStart -t "My Company Site"
    ' Example 2: MakeWebSite.vbs -r C:\Inetpub\wwwroot -t Test -o 8080
    '
    '
    ' Modified by chris crowe - www.iisfaq.com to support multiple host headers and URL Redirection
    '------------------------------------------------------------------------------------------------


    ' Force explicit declaration of all variables
    Option Explicit

    On Error Resume Next

    Dim ArgIPAddress, ArgRootDirectory, ArgServerComment, ArgSkeletalDir, ArgHostName, ArgPort
    Dim ArgComputers, ArgStart
    Dim ArgSiteNumber
    Dim oArgs, ArgNum
    Dim verbose

    ' Chris Crowe - iisfaq.com
    Dim ArgURL, ArgURLExact, ArgURLChildOnly, ArgURLPermanent

    ArgIPAddress = ""
    ArgHostName = ""
    ArgPort = 80
    ArgStart = True
    ArgComputers = Array(1)
    ArgComputers(0) = "LocalHost"
    ArgSiteNumber = 0
    verbose = false

    ' Chris Crowe - iisfaq.com
    ArgURL = ""
    ArgURLChildonly = false
    ArgURLExact = false
    ArgURLPermanent = false

    Set oArgs = WScript.Arguments
    ArgNum = 0

    While ArgNum < oArgs.Count

        Select Case LCase(oArgs(ArgNum))
            Case "--port","-o":
                ArgNum = ArgNum + 1
                ArgPort = oArgs(ArgNum)   
            Case "--ipaddress","-i":
                ArgNum = ArgNum + 1
                ArgIPAddress = oArgs(ArgNum)
            Case "--rootdirectory","-r":
                ArgNum = ArgNum + 1
                ArgRootDirectory = oArgs(ArgNum)
            Case "--comment","-t":
                ArgNum = ArgNum + 1
                ArgServerComment = oArgs(ArgNum)
            Case "--hostname","-h":
                ArgNum = ArgNum + 1
                ArgHostName = oArgs(ArgNum)
            Case "--computer","-c":
                ArgNum = ArgNum + 1
                ArgComputers = Split(oArgs(ArgNum), ",", -1)
            Case "--sitenumber","-n":
                ArgNum = ArgNum + 1
                ArgSiteNumber = CLng(oArgs(ArgNum))

            ' Chris Crowe - iisfaq.com
            Case "--url","-u":
                ArgNum = ArgNum + 1
                ArgURL = oArgs(ArgNum)
            Case "--urlexact"
                ArgURLEXACT = true
            Case "--urlchildonly"
                ArgURLChildOnly = true
            Case "--urlpermanent"
                ArgURLPERMANENT = true

               
            Case "--dontstart":
                ArgStart = False
            Case "--help","-?":
                Call DisplayUsage
            Case "--verbose", "-v":
                verbose = true
            Case Else:
                WScript.Echo "Unknown argument "& oArgs(ArgNum)
                Call DisplayUsage
        End Select   

        ArgNum = ArgNum + 1
    Wend

    ' Chris Crowe - iisfaq.com
    If (ArgRootDirectory = "") Then
        WScript.Echo "Missing Root Directory (Required even with URL redirection path)"
        WScript.Echo ""
        Call DisplayUsage
        WScript.Quit(1)
    End If


    ' *** COMMENTED OUT FOR MPAdmin (Bryce Roskamp)
    ' Check if the root directory exists
    'Set FSO = WScript.CreateObject("Scripting.fileSystemObject")
    '        if (FSO.DirExists(ArgRootDirectory) = False) then
    '             WScript.echo "The specified root directory does not exist : " & ArgRootDirectory
    '             WScript.Quit(2)
    '        end if
    'Set FSO = Nothing


    If (ArgServerComment = "") Then
        WScript.Echo "Missing Server Comment"
        WScript.Echo ""
        Call DisplayUsage
        WScript.Quit(1)
    End If
    ' end - chris Crowe - iisfaq.com

    ' chris Crowe - iisfaq.com
    Call ASTCreateWebSite(ArgIPAddress, ArgRootDirectory, ArgURL, ArgURLEXACT, ArgURLCHILDONLY, ArgURLPERMANENT, ArgServerComment, ArgHostName, ArgPort, ArgComputers, ArgStart)

    ' chris Crowe - iisfaq.com
    Sub ASTCreateWebSite(IPAddress, RootDirectory, URL, ArgURLEXACT, ArgURLCHILDONLY, ArgURLPERMANENT, ServerComment, HostName, PortNum, Computers, Start)
        Dim w3svc, WebServer, NewWebServer, NewDir, Bindings, BindingString, NewBindings, ComputerIndex, Index, SiteObj, bDone
        Dim comp, BindingIndex, HostNameINdex, HostNames
       
       'Chris Crowe - www.iisfaq.com
        Dim URLOPTIONS

       
        On Error Resume Next

        hostNames = Split(HostName,",")
        For ComputerIndex = 0 To UBound(Computers)
            comp = Computers(ComputerIndex)
            If ComputerIndex <> UBound(Computers) Then
                Trace "Creating web site on " & comp & "."
            End If

            ' Grab the web service object
            Err.Clear
            Set w3svc = GetObject("IIS://" & comp & "/w3svc")
            If Err.Number <> 0 Then
                Display "Unable to open: "&"IIS://" & comp & "/w3svc"
            End If

            Trace "Making sure this web server doesn't conflict with another..."
            For Each WebServer in w3svc
                If WebServer.Class = "IIsWebServer" Then
                    Bindings = WebServer.ServerBindings

                    For HostNameINdex = 0 To UBound(HostNames)
                        for BindingIndex = 0 to Ubound(Bindings)
                            BindingString = IpAddress & ":" & PortNum & ":" & HostNames(HostNameINdex)
                            If (BindingString = bindings(BindingIndex)) Then
                                Trace "The server bindings you specified are duplicated in another virtual web server."
                                Trace "The Web Server name is [" & WebServer.ServerComment & "], Instance ID [" & WebServer.name & "]"
                                Trace "The Conflicting Bindings are at index [" & bindingIndex & "], [" & bindings(BindingIndex) & "]"
                                WScript.Quit (1)
                            End If
                        next
                    next
                End If
            Next

            Index = 1
            bDone = False
            Trace "Creating new web server..."

            ' If the user specified a SiteNumber, then use that.  Otherwise,
            ' test successive numbers under w3svc until an unoccupied slot is found
            If ArgSiteNumber <> 0 Then
                Set NewWebServer = w3svc.Create("IIsWebServer", ArgSiteNumber)
                NewWebServer.SetInfo
                If (Err.Number <> 0) Then
                    WScript.Echo "Couldn't create a web site with the specified number: " & ArgSiteNumber
                    WScript.Quit (1)
                Else
                    Err.Clear
                    ' Verify that the newly created site can be retrieved
                    Set SiteObj = GetObject("IIS://"&comp&"/w3svc/" & ArgSiteNumber)
                    If (Err.Number = 0) Then
                        bDone = True
                        Trace "Web server created. Path is - "&"IIS://"&comp&"/w3svc/" & ArgSiteNumber
                    Else
                        WScript.Echo "Couldn't create a web site with the specified number: " & ArgSiteNumber
                        WScript.Quit (1)
                    End If
                End If
            Else
                While (Not bDone)
                    Err.Clear
                    Set SiteObj = GetObject("IIS://"&comp&"/w3svc/" & Index)

                    If (Err.Number = 0) Then
                        ' A web server is already defined at this position so increment
                        Index = Index + 1
                    Else
                        Err.Clear
                        Set NewWebServer = w3svc.Create("IIsWebServer", Index)
                        NewWebServer.SetInfo
                        If (Err.Number <> 0) Then
                            ' If call to Create failed then try the next number
                            Index = Index + 1
                        Else
                            Err.Clear
                            ' Verify that the newly created site can be retrieved
                            Set SiteObj = GetObject("IIS://"&comp&"/w3svc/" & Index)
                            If (Err.Number = 0) Then
                                bDone = True
                                Trace "Web server created. Path is - "&"IIS://"&comp&"/w3svc/" & Index
                            Else
                                Index = Index + 1
                            End If
                        End If
                    End If

                    ' sanity check
                    If (Index > 10000) Then
                        Trace "Seem to be unable to create new web server.  Server number is "&Index&"."
                        WScript.Quit (1)
                    End If
                Wend
            End If

            redim NewBindings (ubound(HostNames))

            For HostNameINdex = 0 To UBound(HostNames)
                BindingString = IpAddress & ":" & PortNum & ":" & HostNames(HostNameINdex)
                Trace "Binding = " & BindingString
                NewBindings(HostNameINdex) = BindingString
            next

            NewWebServer.ServerBindings = NewBindings
            NewWebServer.ServerComment = ServerComment
            NewWebServer.SetInfo
           
           

            ' Now create the root directory object.
            Trace "Setting the home directory..."
            Set NewDir = NewWebServer.Create("IIsWebVirtualDir", "ROOT")
            NewDir.Path = RootDirectory
            NewDir.AccessRead = true
           
           
            ' Modified by Chris crowe - January 2002
            if (URL <> "") then
               trace "Redirection to a URL: " & URL
               URLOPTIONS= ""

               if (ArgURLEXACT = true) then
                  URLOPTIONS = ", EXACT_DESTINATION"
               end if
               if (ArgURLCHILDONLY = true) then
                if (URLOPTIONS > "") then
                    URLOPTIONS = URLOPTIONS & ", CHILD_ONLY"   
                else
                      URLOPTIONS = ", CHILD_ONLY"   
                end if
               end if
               if (ArgURLPERMANENT = true) then
                if (URLOPTIONS > "") then
                    URLOPTIONS = URLOPTIONS & ", PERMANENT"   
                else
                      URLOPTIONS = ", PERMANENT"   
                end if
               end if
               trace "Redirection Flags : " & URLOPTIONS
                  newDir.httpredirect= URL & URLOPTIONS
            end if
            ' Modified by Chris crowe - January 2002
           
               
            ' *** Added for MPAdmin (Bryce Roskamp)
            NewDir.AppFriendlyName = HostNames(0)
            NewDir.AccessFlags = 513
           
               
            Err.Clear
            NewDir.SetInfo
           
            ' Modified for MPAdmin (Bryce Roskamp)
            NewDir.AppCreate2 2                            ' Create the Application as Medium(Pooled)

            If (Err.Number = 0) Then
                Trace "Home directory set."
            Else
                Display "Error setting home directory."
            End If

            Trace "Web site created!"

            If Start = True Then
                Trace "Attempting to start new web server..."
                Err.Clear
                Set NewWebServer = GetObject("IIS://" & comp & "/w3svc/" & Index)
                NewWebServer.Start
                If Err.Number <> 0 Then
                    Display "Error starting web server!"
                    Err.Clear
                Else
                    Trace "Web server started succesfully!"
                End If
            End If
        Next
    End Sub


    ' Display the usage message
    Sub DisplayUsage
        WScript.Echo "Usage: MakeWebSite <--RootDirectory|-r ROOT DIRECTORY>"
        WScript.Echo "                   <--Comment|-t SERVER COMMENT>"
        WScript.Echo "                   [--computer|-c COMPUTER1[,COMPUTER2...]]"
        WScript.Echo "                   [--port|-o PORT NUM]"
        WScript.Echo "                   [--IPAddress|-i IP ADDRESS]"
        WScript.Echo "                   [--HostName|-h HOST NAME[,HOSTNAME2...]]"
        WScript.Echo "                   [--SiteNumber|-n SITENUMBER]"
        WScript.Echo "                   [--URL RedirectionPath]"
        WScript.Echo "                   [--URLExact]"
        WScript.Echo "                   [--URLChildOnly]"
        WScript.Echo "                   [--URLPermanent]"
        WScript.Echo "                   [--DontStart]"
        WScript.Echo "                   [--verbose|-v]"
        WScript.Echo "                   [--help|-?]"
        WScript.Echo ""
        WScript.Echo "WARNING: Only use Host Name if DNS is set up find the server."
        WScript.Echo ""
        WScript.Echo "Example 1: MakeWebSite.vbs -r D:\Roots\Company11 --DontStart -t ""My Company Site"""
        WScript.Echo "Example 2: MakeWebSite.vbs -r D:\Roots\Company11 -h ""www.iisfaq.com,www.iis.com"" --DontStart -t ""My Company Site"""
        WScript.Echo "Example 3: MakeWebSite.vbs -r D:\Roots\Company11 -i 192.168.0.1 -t ""My Company Site"" --URL http://www.iisfaq.com --URLExact --URLPermanent"
        WScript.Echo ""
        WScript.Echo "Note: URLRedirection requires a HTTP:// prefix and a root directory also!"
        WScript.Echo ""

        WScript.Quit (1)
    End Sub

    Sub Display(Msg)
        WScript.Echo Now & ". Error Code: " & Hex(Err) & " - " & Msg
    End Sub

    Sub Trace(Msg)
        if verbose = true then
            WScript.Echo Now & " : " & Msg   
        end if
    End Sub

     

     

  • 01-14-2009, 10:28 AM In reply to

    Re: mkw3site.vbs for iis7

     I'm seeing now that this is a matter of permissions across the servers rather than a scripting issue.  The mkw3site.vbs works on the 2008 server.  But when attempting to make a site on the 2008 server from a 2003 server I get the error:

     

    Unable to open:  IIS://servername/w3svc

     

    If you have an answer to what I need to set to make this work, please let me know.  I'll keep working with it and if I get to the resolution, I'll post for everyone else.

     

Page 1 of 1 (2 items)