added samples
[windows-sources.git] / sdk / samples / WCFSamples / TechnologySamples / Scenario / Federation / CS / Scripts / SetUpVDir.vbs
blob5baf058518475ed20af8cde6c23cee0b43e3924b
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
17 WScript.Echo usage
18 Wscript.Quit
19 End If
21 fullVDirName = Wscript.Arguments(0)
22 vDirPath = Wscript.Arguments(1)
24 ' Split vDirName in parent vdir and vdir to create
25 parentVDirName = ""
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)
32 End If
34 ' Split
35 vDirArray = Split(StrReverse(fullVDirName), "/", 2)
36 vDirName = StrReverse(vDirArray(0))
37 parentVDirName = StrReverse(vDirArray(1))
38 End If
40 If vDirName = "" Then
41 shell.Popup "vDirName invalid: " & vDirName, 0, "Error" & " (CleanUpVdir.vbs)", 16 ' 16 = Stop
42 WScript.Quit
43 End If
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
52 WScript.Quit
53 End If
55 ' Does vDirName IIS application already exist in the metabase?
56 On Error Resume Next
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
64 WScript.Quit
65 End If
67 If objIIS.Path = vDirPath Then
68 WScript.Quit
69 Else
70 ' Need to remap virtual directory
71 result = 6
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
79 Else
80 WScript.Quit
81 End If
82 End If
83 End If
85 'Using IIS Administration object , turn on script/execute permissions and define the virtual directory as an 'in-process application.
86 Dim iisName
87 If parentVDirName = "" Then
88 iisName = "IIS://localhost/W3SVC/1/Root"
89 Else
90 iisName = "IIS://localhost/W3SVC/1/Root/" & parentVDirName
91 End If
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
97 WScript.Quit
98 End If
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
104 WScript.Quit
105 End If
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
118 vDirObj.SetInfo
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
122 WScript.Quit
123 End If
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
141 WScript.Quit
142 Else
143 res = "Created '" & "/" & fullVDirName & "' virtual directory mapped to " & vbCrLf & vDirPath
144 shell.Popup res, 0, "Virtual Directory Created" & " (CleanUpVdir.vbs)", 64 ' 64 = Information
145 End If
147 Sub DeleteVirtualDirectory( parentVDirName, vDirName )
149 Dim iisName
150 If parentVDirName = "" Then
151 iisName = "IIS://localhost/W3SVC/1/Root"
152 Else
153 iisName = "IIS://localhost/W3SVC/1/Root/" & parentVDirName
154 End If
155 Set iis = GetObject(iisName)
157 If Err.Number <> 0 Then
158 shell.Popup Err.Description, 0, "Error" & " (CleanUpVdir.vbs)", 16 ' 16 = Stop
159 WScript.Quit
160 End If
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
170 Else
171 errorString = errorString & "Description: " & Err.Description
172 End If
174 shell.Popup errorString, 0, "Error" & " (CleanUpVdir.vbs)", 16 ' 16 = Stop
175 End If
177 End Sub
179 Function VDirExists( name )
181 If IsEmpty(name) Or IsNull(name) Or Len(name) = 0 Then
182 VDirExists = True
183 Exit Function
184 End If
186 On Error Resume Next
187 Set objIIS = GetObject( "IIS://localhost/W3SVC/1/Root/" & name )
188 If Err.Number = 0 Then
189 VDirExists = True
190 Else
191 Err.Clear
192 VDirExists = False
193 End If
194 End Function