SoFunction
Updated on 2025-04-13

vbscript automatically configures IIS code


strServerName =""localhost""
strRootPath=""g:\documents"" ''Virtual Directory Path=""g:\documents"" '
strVRName=""Test"" ''Virtual Directory Name
strDefaultDoc=""""" ''The starting document

Dim objIIS
''MsgBox ""IIS://"" &  strServerName &  ""/W3SVC/1""

On Error Resume Next
Set objIIS=GetObject(""IIS://"" &  strServerName &  ""/W3SVC/1"")

If err=-2147024893 Then
MsgBox ""IIS does not exist!"" &  vbcrlf &  "Please verify that IIS has been installed correctly!"",vbcritical

ElseIf err<>0 Then
MsgBox ""Unknown error!"",vbcritical

End If

On Error GoTo 0

Set objVirtualDir=(""IISWebVirtualDir"",""Root"")
For each VR in objVirtualDir
If =strVRName Then
MsgBox ""Virtual Directory"" &  strVRName &  "Already existed!"",vbinformation

End If
Next

On Error Resume Next
Set fs=("""")
Set objFolder=(strRootPath)

If err=76 Then 
MsgBox ""Path"" &  strRootPath & ""Not exist!"",vbcritical

End If

Set objFolder=nothing
Set fs=nothing
On Error GoTo 0

On Error Resume Next
Set VirDir=(""IISWebVirtualDir"",strVRName)
=true
=strRootPath
= &  "","" &  strDefaultDoc



If err<>0 Then
MsgBox ""Creating virtual directory failed!"",vbcritical
Else
MsgBox ""Virtual Directory"" &  strVRName &  ""Successfully created on the server"" &  strServerName &  """"",vbinformation
End If

The key to the code is to create a virtual directory, and its configuration:

Set VirDir=(""IISWebVirtualDir"",strVRName)
=true
=strRootPath
= &  "","" &  strDefaultDoc