Sign in to follow this  
Followers 0
billmez

Add new virtual web servers in IIS

1 post in this topic

Hello all,

For any of you that administer a web server, here is a script that will create a new virtual server in IIS5 or IIS6 without using the MS MMC snap-in.

I plan on dropping a GUI front end on it. As always, comments are welcome!

billmez

########## begin code ###############

;==============================================================================

; Function: _createWebSite

; Purpose: To add virtual web servers in IIS5 or IIS6

; on Windows 2000 or 2003 server

; Parameters: All are

;

; $IPAddress1: The IP address for the new web server

; $PortNum1: The server port - default 80

; $HostName1: Host headers for the new server

; (comma separated list)

; $RootDirectory1: The physical root directory of the web site

; (must exist first!)

; $ServerComment1: The site name displayed within IIS MMC

; $Start1: 1 or 0 - start the new server after creation

;

; Returns: 1 for successful creation

;

; Author: Bill Mezian (billmez), converted from VB script

;==============================================================================

; Examples:

$IPAddress1 = "192.168.0.3"

$PortNum1 = 80

$HostName1 = "company.com,www.company.com"

$RootDirectory1 = "C:\siteroot\newsite"

$ServerComment1 = "New Web Site"

$Start1 = 0

_CreateWebSite($IPAddress1, $RootDirectory1, $ServerComment1, $HostName1, $PortNum1, $Start1)

Func _CreateWebSite($IPAddress, $RootDirectory, $ServerComment, $HostName, $PortNum = 80, $Start = 0)

Dim $NewBindings[1]

$HostNames = StringSplit($HostName,",")

$w3svc = ObjGet("IIS://" & @ComputerName & "/w3svc")

If @error <> 0 Then

MsgBox(4112, "Error!", "Unable to open: "&"IIS://" & @ComputerName & "/w3svc")

Exit

EndIf

; check for server already defined

For $WebServer in $w3svc

If $WebServer.Class = "IIsWebServer" Then

$Bindings = $WebServer.ServerBindings

For $HostNameINdex = 1 To UBound($HostNames)-1

for $BindingIndex = 0 to UBound($Bindings)-1

$BindingString = $IPAddress & ":" & $PortNum & ":" & $HostNames[$HostNameINdex]

If ($BindingString = $Bindings[$BindingIndex]) Then

MsgBox(4144, "Critical Error!", "The server Bindings you specified are duplicated in another virtual web server." & @CRLF & "The Web Server name is " & $WebServer.ServerComment & ", Instance ID " & $WebServer.name & " " & @CRLF & "The Conflicting Bindings are at Index " & $BindingIndex & ", " & $Bindings[$BindingIndex] & "")

Exit

EndIf

next

next

EndIf

next

; find the next available site index

$Index = 1

$bDone = 0

SetError(0) ; make sure @errors is clear

While (Not $bDone)

$SiteObj = ObjGet("IIS://" & @ComputerName & "/w3svc/" & $Index)

If Not @error Then

$Index = $Index + 1 ; A web server is already defined at this position so increment

If ($Index > 10000) Then ; set an upper boundry for the index

MsgBox(4112, "Critical Error 66!", "Unable to create new web server. Server number is " & $Index & ".")

Exit

EndIf

Else; index successfully created

$bDone = 1

;MsgBox(4160, "", "New Server Index is " & $Index)

EndIf

WEnd

; create new server

SetError(0) ; make sure @errors is clear

$NewWebServer = $w3svc.Create("IIsWebServer", $Index)

; check for successful creation of new server

$SiteObj = ObjGet("IIS://" & @ComputerName & "/w3svc/" & $Index)

If @error <> 0 Then

MsgBox(4160, "Information", "Web server created. Path is - IIS://" & @ComputerName & "/w3svc/" & $Index)

Else

MsgBox(4112, "Critical Error!", "Unable to create web server - IIS://" & @ComputerName & "/w3svc/" & $Index)

Exit

Endif

redim $NewBindings[uBound($HostNames)-1]

; Process host headers and add new server bindings

For $HostNameINdex = 1 To UBound($HostNames) -1

$BindingString = $IPAddress & ":" & $PortNum & ":" & $HostNames[$HostNameINdex]

; MsgBox(4096, "", $HostNameINdex-1 & " - " & $BindingString)

$NewBindings[$HostNameINdex-1] = $BindingString

next

SetError(0) ; make sure @errors is clear

$NewWebServer.ServerBindings = $NewBindings

$NewWebServer.ServerComment = $ServerComment

$NewWebServer.SetInfo()

If @error <> 0 Then

MsgBox(4112, "Critical Error", "Web server bindings not completed")

Exit

Endif

; create site root

SetError(0) ; make sure @errors is clear

$NewDir = $NewWebServer.Create("IIsWebVirtualDir", "ROOT")

$NewDir.Path = $RootDirectory

$NewDir.AccessFlags = 513

$NewDir.SetInfo()

If @error <> 0 Then

MsgBox(4112, "Critical Error", "Unable to Create Site Root Directory")

Exit

Endif

; enable ASP support

SetError(0)

$EnableASP = ObjGet("IIS://" & @ComputerName & "/W3SVC/" & $Index & "/ROOT")

$EnableASP.AppCreate2(2)

If @error <> 0 Then

MsgBox(4112, "Critical Error", "Unable to Enable ASP")

Exit

Endif

; set db and log directory permissions

$FolderPerms = ObjGet("IIS://" & @ComputerName & "/W3SVC/" & $Index & "/ROOT")

SetError(0)

$DbPerms = $FolderPerms.Create("IIsWebDirectory", "db")

$DbPerms.AccessFlags = 0

$DbPerms.SetInfo()

If @error <> 0 Then

MsgBox(4144, "Error", "Unable to create db folder")

Endif

SetError(0)

$LogPerms = $FolderPerms.Create("IIsWebDirectory", "logs")

$LogPerms.AccessFlags = 0

$LogPerms.SetInfo()

If @error <> 0 Then

MsgBox(4144, "Error", "Unable to create log folder")

Endif

; start new web server

If $Start = 1 Then

SetError(0)

MsgBox(4144, "", "Attempting to Start new web server...")

$NewWebServer = ObjGet("IIS://" & @ComputerName & "/w3svc/" & $Index)

$NewWebServer.Start()

If @error <> 0 Then

MsgBox(4112, "Error!", "Unable to start web server!")

Else

MsgBox(4160, "Information", "Web server started succesfully!")

EndIf

EndIf

Return 1

EndFunc

############## end code #####################

createwebsite.au3

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0