1 Set shell
= Wscript
.CreateObject( "WScript.Shell" )
3 If Wscript
.Arguments
.Count
< 2 Then
4 usage
= "USAGE: CleanUpVdir.vbs virtual_directory_name directory_location_to_map" & vbCrLf
& vbCrLf
5 usage
= usage
& "=========" & vbCrLf
6 usage
= usage
& "YOU DO NOT NEED TO RUN THIS SCRIPT MANUALLY TO RUN THE SAMPLES. " & vbCrLf
7 usage
= usage
& "The Visual Studio projects call it when built." & vbCrLf
8 usage
= usage
& "=========" & vbCrLf
& vbCrLf
9 usage
= usage
& "ARGUMENTS" & vbCrLf
10 usage
= usage
& "=========" & vbCrLf
11 usage
= usage
& "virtual_directory_name : String that represents the name of the" & vbCrLf
12 usage
= usage
& " virtual directory, in the form parent1/parent2/virtual_directory_name. " & vbCrLf
13 usage
= usage
& " Only virtual_directory_name is created." & vbCrLf
14 usage
= usage
& "directory_location_to_map: Directory to be mapped to the virtual" & vbCrLf
15 usage
= usage
& " directory. Can be relative to the" & vbCrLf
16 usage
= usage
& " current directory." & vbCrLf
21 fullVDirName
= Wscript
.Arguments(0)
22 vDirPath
= Wscript
.Arguments(1)
24 ' Split vDirName in parent vdir and vdir to create
26 vDirName
= fullVDirName
27 If InStr(fullVDirName
, "/") > 0 Then
29 ' Strip "/" from beginning of string
30 If (InStr(fullVDirName
, "/")) = 1 Then
31 fullVDirName
= Right(fullVDirName
, Len(fullVDirName
) - 1)
35 vDirArray
= Split(StrReverse(fullVDirName
), "/", 2)
36 vDirName
= StrReverse(vDirArray(0))
37 parentVDirName
= StrReverse(vDirArray(1))
41 shell
.Popup
"vDirName invalid: " & vDirName
, 0, "Error" & " (CleanUpVdir.vbs)", 16 ' 16 = Stop
45 ' Get the full path of the directory_location_to_map
46 Set fso
= WScript
.CreateObject( "Scripting.FileSystemObject" )
47 vDirPath
= fso
.GetFolder( vDirPath
).Path
49 ' Does the parent vdir exist in the in the metabase?
50 If Not VDirExists(parentVDirName
) Then
51 shell
.Popup
"Parent virtual directory does not exist: " & parentVDirName
, 0, "Error" & " (CleanUpVdir.vbs)", 16 ' 16 = Stop
55 ' Does vDirName IIS application already exist in the metabase?
57 If VDirExists(fullVDirName
) Then
59 ' Check whether the path is as desired
60 Set objIIS
= GetObject( "IIS://localhost/W3SVC/1/Root/" & fullVDirName
)
62 If Err
.Number
<> 0 Then
63 shell
.Popup Err
.Description
, 0, "Error" & " (CleanUpVdir.vbs)", 16 ' 16 = Stop
67 If objIIS
.Path
= vDirPath
Then
70 ' Need to remap virtual directory
73 ' We could warn, but do not (Just tell them that we mapped a new vdir.)
74 'result = shell.Popup( "A virtual directory named " & fullVDirName & " already exists. " & vbCrLf & vbCrLf _
75 '& "Would you like it re-mapped for this sample?", 0 ,"Remap Virtual Directory?" & " (CleanUpVdir.vbs)", 4 + 32 )' 4 = YesNo & 32 = Question
77 If result
= 6 Then ' 6 = Yes
78 DeleteVirtualDirectory parentVDirName
, vDirName
85 'Using IIS Administration object , turn on script/execute permissions and define the virtual directory as an 'in-process application.
87 If parentVDirName
= "" Then
88 iisName
= "IIS://localhost/W3SVC/1/Root"
90 iisName
= "IIS://localhost/W3SVC/1/Root/" & parentVDirName
93 Set objIIS
= GetObject(iisName
)
95 If Err
.Number
<> 0 Then
96 shell
.Popup
"Could not access " & iisName
& ": " & Err
.Description
, 0, "Error" & " (CleanUpVdir.vbs)", 16 ' 16 = Stop
100 Set vDirObj
= objIIS
.Create( "IISWebVirtualDir", vDirName
)
102 If Err
.Number
<> 0 Then
103 shell
.Popup
"Could not create " & iisName
& "/" & vDirName
& ": " & Err
.Description
, 0, "Error" & " (CleanUpVdir.vbs)", 16 ' 16 = Stop
107 vDirObj
.Path
= vDirPath
108 vDirObj
.AuthNTLM
= True
109 vDirObj
.AccessRead
= True
110 vDirObj
.AccessWrite
= True
111 vDirObj
.AccessScript
= True
112 vDirObj
.AccessExecute
= True
113 vDirObj
.AuthAnonymous
= True
114 'vDirObj.AnonymousUserName = owner
115 vDirObj
.AnonymousPasswordSync
= True
116 vDirObj
.AppFriendlyName
= vDirName
& "App"
117 vDirObj
.AppCreate
True
120 If Err
.Number
<> 0 Then
121 shell
.Popup
"Could not set properties on " & iisName
& "/" & vDirName
& ": " & Err
.Description
, 0, "Error" & " (CleanUpVdir.vbs)", 16 ' 16 = Stop
125 ' Get the name of the account for the anonymous user in IIS
126 owner
= vDirObj
.AnonymousUserName
128 ' Change necessary folder permissions using CACLS.exe
129 aclCmd
= "cmd /c echo y| CACLS "
130 aclCmd
= aclCmd
& """" & vDirPath
& """"
131 aclCmd
= aclCmd
& " /E /G " & owner
& ":C"
132 rtc
= shell
.Run( aclCmd
, 0, True )
134 aclCmd
= "cmd /c echo y| CACLS "
135 aclCmd
= aclCmd
& """" & vDirPath
& """"
136 aclCmd
= aclCmd
& " /E /G ""VS Developers"":C"
137 rtc
= shell
.Run( aclCmd
, 0, True )
139 If Err
.Number
<> 0 Then
140 shell
.Popup
"Could not set ACLs on " & iisName
& "/" & vDirName
& ": " & Err
.Description
, 0, "Error" & " (CleanUpVdir.vbs)", 16 ' 16 = Stop
143 res
= "Created '" & "/" & fullVDirName
& "' virtual directory mapped to " & vbCrLf
& vDirPath
144 shell
.Popup res
, 0, "Virtual Directory Created" & " (CleanUpVdir.vbs)", 64 ' 64 = Information
147 Sub DeleteVirtualDirectory( parentVDirName
, vDirName
)
150 If parentVDirName
= "" Then
151 iisName
= "IIS://localhost/W3SVC/1/Root"
153 iisName
= "IIS://localhost/W3SVC/1/Root/" & parentVDirName
155 Set iis
= GetObject(iisName
)
157 If Err
.Number
<> 0 Then
158 shell
.Popup Err
.Description
, 0, "Error" & " (CleanUpVdir.vbs)", 16 ' 16 = Stop
162 iis
.Delete
"IISWebVirtualDir", vDirName
164 If Err
.Number
<> 0 Then
166 errorString
= "Unable to delete existing virtual directory."
168 If Err
.Description Is
Nothing Then
169 errorString
= errorString
& "Error Code: " & Err
.Number
171 errorString
= errorString
& "Description: " & Err
.Description
174 shell
.Popup errorString
, 0, "Error" & " (CleanUpVdir.vbs)", 16 ' 16 = Stop
179 Function VDirExists( name
)
181 If IsEmpty(name
) Or IsNull(name
) Or Len(name
) = 0 Then
187 Set objIIS
= GetObject( "IIS://localhost/W3SVC/1/Root/" & name
)
188 If Err
.Number
= 0 Then