strServerName ="localhost"
strRootPath="g:/documents" ''虚拟目录路径
strVRName="Test" ''虚拟目录名称
strDefaultDoc="index.asp" ''起始文档
Dim objIIS
''MsgBox "IIS://" & strServerName & "/W3SVC/1"
On Error Resume Next
Set objIIS=GetObject("IIS://" & strServerName & "/W3SVC/1")
If err=-2147024893 Then
MsgBox "IIS不存在!" & vbcrlf & "请验证IIS是否已正确安装!",vbcritical
Wscript.Quit
ElseIf err<>0 Then
MsgBox "未知错误!",vbcritical
Wscript.Quit
End If
On Error GoTo 0
Set objVirtualDir=objIIS.GetObject("IISWebVirtualDir","Root")
For each VR in objVirtualDir
If VR.Name=strVRName Then
MsgBox "虚拟目录" & strVRName & "已存在!",vbinformation
Wscript.Quit
End If
Next
On Error Resume Next
Set fs=Wscript.CreateObject("Scripting.FileSystemObject")
Set objFolder=fs.GetFolder(strRootPath)
If err=76 Then
MsgBox "路径" & strRootPath & "不存在!",vbcritical
Wscript.Quit
End If
Set objFolder=nothing
Set fs=nothing
On Error GoTo 0
On Error Resume Next
Set VirDir=objVirtualDir.Create("IISWebVirtualDir",strVRName)
VirDir.AccessRead=true
VirDir.Path=strRootPath
VirDir.DefaultDoc=VirDir.DefaultDoc & "," & strDefaultDoc
VirDir.setInfo
If err<>0 Then
MsgBox "创建虚拟目录失败!",vbcritical
Else
MsgBox "虚拟目录" & strVRName & "成功创建在服务器" & strServerName & "上!",vbinformation
End If
代码的关键在于创建虚拟目录,及其中的配置:
Set VirDir=objVirtualDir.Create("IISWebVirtualDir",strVRName)
VirDir.AccessRead=true
VirDir.Path=strRootPath
VirDir.DefaultDoc=VirDir.DefaultDoc & "," & strDefaultDoc
VirDir.setInfo