Script to Create a Virtual Directory - IIS

The following VB Script demonstrates how to create a IIS Application programmatically.

Dim Website, AppProtection, IISOBJ, Site, Description, IIS4

ArgComputers = Array("LocalHost")
ArgWebSites = "MyWebSite"

Dim foundSite
Dim compIndex

for compIndex = 0 to UBound(ArgComputers)
    set foundSite = findWeb(ArgComputers(compIndex), ArgWebSites)
    if isObject(foundSite) then
        Website = foundSite.Name
        
    else
        Wscript.Echo "No matching web found."
    end if
next

Function findWeb(computer, webname)
    On Error Resume Next

    Dim websvc, site
    dim webinfo
    Dim aBinding, binding

    set websvc = GetObject("IIS://" & computer & "/W3svc")
    if (Err <> 0) then
        exit function
    end if
    ' First try to open the webname.
    set site = websvc.GetObject("IIsWebServer", webname)
    if (Err = 0) and (not isNull(site)) then
        if (site.class = "IIsWebServer") then
            ' Here we found a site that is a web server.
            set findWeb = site
            exit function
        end if
    end if
    err.clear
    for each site in websvc
        if site.class = "IIsWebServer" then
            '
            ' First, check to see if the ServerComment
            ' matches
            '
            If site.ServerComment = webname Then
                set findWeb = site
                exit function
            End If
            aBinding=site.ServerBindings
            if (IsArray(aBinding)) then
                if aBinding(0) = "" then
                    binding = Null
                else
                    binding = getBinding(computer, aBinding(0))
                end if
            else 
                if aBinding = "" then
                    binding = Null
                else
                    binding = getBinding(computer, aBinding)
                end if
            end if
            if IsArray(binding) then
                if (binding(2) = webname) or (binding(0) = webname) then
                    set findWeb = site
                    exit function
                End If
            end if 
        end if
    next
End Function

' Set some variables and constants we will use...
Dim strVirtualDirectoryName 'IIS Virtual Directory Name
Dim blnInProcessApplication 'IIS In Process Application Flag
Dim objIIS 'ADSI IIS Object
Dim strVirtualDirectoryPath 'IIS Virtual Directory Path
Dim objFileSystem 'VBScript FileSystemObject
Dim strOwner 'NT Folder Owner
Dim objVirtualDirectory 'ADSI IIS Virtual Directory Object
Dim blnScriptPermissions 'IIS script permissions flag
Dim blnExecutePermissions ' IIS Execute permissions flag
Dim blnWritePermissions ' 
Dim blnReadPermissions '
Dim strHTTPReferer 'IIS Referrer Page
Dim objWSH 'Windows Scripting Host Object
Dim objRTC 'Return
Dim strACLCommand 'Command Line string to set ACLs
Dim MachineName ' computer name

strVirtualDirectoryName = "MyVirtualDirectory" 

' Get the Computer name using Wscript.Network and assign to IUSR to 
' create IIS IUSR account name
Set WshNetwork = WScript.CreateObject("WScript.Network")
MachineName = WshNetwork.ComputerName
strOwner = "IUSR_" & MachineName
Set WshNetwork = Nothing
Set wsc = Wscript.CreateObject("WScript.Shell")
wsc.Popup "Setting Permissions for Computer Name = " & strOwner , 1
blnScriptPermissions = "True" 
blnExecutePermissions = "False" '"True"
blnWritePermissions = "False" '"True"
blnReadPermissions = "True"

' Does this IIS application already exist in the metabase?
On Error Resume Next

Set objIIS = GetObject("IIS://localhost/W3SVC/" & Website & "/Root/" & strVirtualDirectoryName)

If Err.Number = 0 Then
    Wscript.echo ("An application with this name already exists. ")
    Wscript.quit
End If

Set objIIS = Nothing

'Now use IIS administration objects to create the IIS application in the metabase. 
'Create the IIS application
Set objIIS = GetObject("IIS://localhost/W3SVC/" & Website & "/Root")
strVirtualDirectoryPath = objIIS.Path & "\" & strVirtualDirectoryName

' First check for and optionally create the physical folder under wwwroot
Set objFileSystem = Wscript.CreateObject("Scripting.FileSystemObject")

On Error Resume Next

Set Folder = objFileSystem.GetFolder(strVirtualDirectoryPath)

If Hex(Err.number) = "4C" Then
    wsc.Popup "Creating folder " & strVirtualDirectoryPath , 1
    set f= objFileSystem.CreateFolder(strVirtualDirectoryPath)
End If

Set objFileSystem = Nothing

'Using IIS Administration object , turn on script/execute permissions 
'and define the virtual directory as an in-process application. 
Set objVirtualDirectory = objIIS.Create("IISWebVirtualDir", strVirtualDirectoryName)
objVirtualDirectory.AccessScript = blnScriptPermissions
objVirtualDirectory.Path = strVirtualDirectoryPath
objVirtualDirectory.AppCreate blnInProcessApplication
objVirtualDirectory.AccessWrite = blnWritePermissions 
objVirtualDirectory.AccessRead = blnReadPermissions
objVirtualDirectory.AccessExecute = blnExecutePermissions
objVirtualDirectory.AuthAnonymous =True
objVirtualDirectory.AnonymousUserName=strOwner
objVirtualDirectory.AnonymousPasswordSync=True
objVirtualDirectory.AppCreate (True)
objVirtualDirectory.AppIsolated = 2 ' Medium (Pooled)
objVirtualDirectory.SetInfo 

'Set Change Permissions for the owner using CACLS.exe
' need to "|" pipe the "Y" yes answer to the command "Are you sure?" 
' prompt for this to work (see KB: Q135268)
strACLCommand = "cmd /c echo y| CACLS "
strACLCommand = strACLCommand & strVirtualDirectoryPath
strACLCommand = strACLCommand & " /g " & strOwner & ":C"
Set objWSH = Server.CreateObject("WScript.Shell")
objRTC = objWSH.Run (strACLCommand , 0, True)
Set objWSH = Nothing
strRes = "Web Application Created Sucessfully" & vbCRlf
strRes = strRes & "Path : "& strVirtualDirectoryPath & vbCRlf
strRes =strRes & "Script Permissions : "& blnScriptPermissions & vbCRlf
strRes = strRes & "Read Permissions : " & blnReadPermissions & vbCRlf
strRes = strRes & "Write Permissions: " & blnWritePermissions & vbCrLf
strRes = strRes & "Execute Permission: " & blnExecutePermissions & vbCrLf
strRes = strREs & strOwner & " granted change permissions" & vbCrlF
wscript.echo strRes

The following VBScript will create a virtual directory in IIS as specified by the strVirtualDirectoryName and strPhysicalDirectoryPath variables.

Dim strVirtualDirectoryName   ' IIS Virtual Directory Name
Dim strPhysicalDirectoryPath  ' Physical path for hosting the website

strVirtualDirectoryName = "MyNewWebsite" 
strPhysicalDirectoryPath = "C:\MyDirectory\MySubDirectory\MyNewWebsite"

Dim blnInProcessApplication   ' IIS In Process Application Flag
Dim objIIS                    ' ADSI IIS Object
Dim strVirtualDirectoryPath   ' IIS Virtual Directory Path
Dim objFileSystem             ' VBScript FileSystemObject
Dim strOwner                  ' NT Folder Owner
Dim objVirtualDirectory       ' ADSI IIS Virtual Directory Object
Dim blnScriptPermissions      ' IIS script permissions flag
Dim blnExecutePermissions     ' IIS Execute permissions flag
Dim blnWritePermissions       ' 
Dim blnReadPermissions        '
Dim strHTTPReferer            ' IIS Referrer Page
Dim objWSH                    ' Windows Scripting Host Object
Dim objRTC                    ' Return
Dim strACLCommand             ' Command Line string to set ACLs
Dim MachineName               ' computer name

' Get the Computer name using Wscript.Network and assign to IUSR to create IIS IUSR account name
Set WshNetwork = WScript.CreateObject("WScript.Network")
MachineName = WshNetwork.ComputerName
strOwner = "IUSR_" & MachineName
Set WshNetwork = Nothing
Set wsc = Wscript.CreateObject("WScript.Shell")
wsc.Popup "Setting Permissions for Computer Name = " & strOwner , 1
blnScriptPermissions = "True" 
blnExecutePermissions = "True"
blnWritePermissions = "True"
blnReadPermissions = "True"

' Does this IIS application already exist in the metabase?
On Error Resume Next
Set objIIS = GetObject("IIS://localhost/W3SVC/1/Root/" & strVirtualDirectoryName)
If Err.Number = 0 Then
	Wscript.echo ("An application with this name already exists. ")
	Wscript.quit
End If
Set objIIS = Nothing

'Now use IIS administration objects to create the IIS application in the metabase. 
'Create the IIS application
Set objIIS = GetObject("IIS://localhost/W3SVC/1/Root")
'strVirtualDirectoryPath = objIIS.Path & "\" & strVirtualDirectoryName


' First check for and optionally create the physical folder under wwwroot
Set objFileSystem = Wscript.CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set Folder = objFileSystem.GetFolder(strPhysicalDirectoryPath)
If Hex(Err.number) = "4C" Then
	wsc.Popup "Creating folder " & strPhysicalDirectoryPath , 1
	set f= objFileSystem.CreateFolder(strPhysicalDirectoryPath)
End If
Set objFileSystem = Nothing

'Using IIS Administration object , turn on script/execute permissions and define the virtual directory as an 'in-process application. 
Set objVirtualDirectory = objIIS.Create("IISWebVirtualDir", strVirtualDirectoryName)
objVirtualDirectory.AccessScript = blnScriptPermissions
objVirtualDirectory.Path = strPhysicalDirectoryPath
objVirtualDirectory.AppCreate blnInProcessApplication
objVirtualDirectory.AccessWrite = blnWritePermissions 
objVirtualDirectory.AccessRead = blnReadPermissions
objVirtualDirectory.AccessExecute = blnExecutePermissions
objVirtualDirectory.AuthAnonymous = True
objVirtualDirectory.AnonymousUserName = strOwner
objVirtualDirectory.AnonymousPasswordSync = True
objVirtualDirectory.AppCreate (True)
objVirtualDirectory.SetInfo 

'Set Change Permissions for the owner using CACLS.exe
' need to "|" pipe the "Y" yes answer to the command "Are you sure?" prompt for this to work (see KB: Q135268 )
strACLCommand = "cmd /c echo y| CACLS "
strACLCommand = strACLCommand & strVirtualDirectoryPath
strACLCommand = strACLCommand & " /g " & strOwner & ":C"
Set objWSH = Server.CreateObject("WScript.Shell")
objRTC = objWSH.Run (strACLCommand , 0, True)
Set objWSH = Nothing
strRes = "Web Application Created Sucessfully" & vbCRlf
strRes = strRes & "Path : "& strVirtualDirectoryPath & vbCRlf
strRes = strRes & "Script Permissions : "& blnScriptPermissions & vbCRlf
strRes = strRes & "Read Permissions : " & blnReadPermissions & vbCRlf
strRes = strRes & "Write Permissions: " & blnWritePermissions & vbCrLf
strRes = strRes & "Execute Permission: " & blnExecutePermissions & vbCrLf
strRes = strREs & strOwner & " granted change permissions" & vbCrlF
WScript.echo strRes