Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

452 lines
14 KiB

  1. '+---------------------------------------------------------------------
  2. '
  3. ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  4. ' ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  5. ' THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  6. ' PARTICULAR PURPOSE.
  7. '
  8. ' Copyright (c) Microsoft Corporation, 1999-2001. All Rights Reserved.
  9. '
  10. ' PROGRAM: qu.vbs
  11. '
  12. ' PURPOSE: Illustrates using VBScript, Query Helper API, and Admin
  13. ' Helper API with Windows Scripting Host to query Indexing
  14. ' Service and to determine the catalog status. The script
  15. ' functions similarly to the C++ Simple(QSample) sample.
  16. '
  17. ' PLATFORM: Windows 2000
  18. '
  19. '----------------------------------------------------------------------
  20. main
  21. '----------------------------------------------------------------------
  22. sub Main
  23. if WScript.Arguments.Count < 1 then call Usage end if
  24. ' set defaults for all arguments
  25. query = ""
  26. catalog = "system"
  27. locale = ""
  28. forceci = TRUE
  29. forcemerge = FALSE
  30. inputfile = ""
  31. shallow = FALSE
  32. dialect = 1
  33. machine = "."
  34. columns = "path"
  35. scope = "\"
  36. quiet = FALSE
  37. sort = ""
  38. groupby = ""
  39. stats = FALSE
  40. uptodate = FALSE
  41. maxhits = 0
  42. firsthits = FALSE
  43. repeat = 1
  44. ' parse command line arguments
  45. for i = 0 to WScript.Arguments.Count - 1
  46. arg = WScript.Arguments( i )
  47. first = left( arg, 1 )
  48. c = mid( arg, 2, 1 )
  49. if "/" = first or "-" = first then
  50. if ":" = mid( arg, 3, 1 ) then
  51. v = mid( arg, 4 )
  52. select case c
  53. case "a" groupby = v
  54. case "c" catalog = v
  55. case "e" locale = v
  56. case "f" forceci = ( v = "+" )
  57. case "i" inputfile = v
  58. case "l" dialect = v
  59. case "m" machine = v
  60. case "o" columns = v
  61. case "p" scope = v
  62. case "r" repeat = v
  63. case "s" sort = v
  64. case "x" maxhits = v
  65. case "y" maxhits = v
  66. firsthits = TRUE
  67. case else Usage
  68. end select
  69. else
  70. select case c
  71. case "g" forcemerge = TRUE
  72. case "j" shallow = TRUE
  73. case "q" quiet = TRUE
  74. case "t" stats = TRUE
  75. case "u" uptodate = TRUE
  76. case else Usage
  77. end select
  78. end if
  79. else
  80. if "" = query then query = arg else Usage
  81. end if
  82. next
  83. ' Turn a relative scope path into an absolute path
  84. if "\" <> scope and "\\" <> left( scope, 2 ) then
  85. set fso = WScript.CreateObject( "Scripting.FileSystemObject" )
  86. scope = fso.GetAbsolutePathName( scope )
  87. end if
  88. for i = 1 to repeat
  89. if "" = inputfile then
  90. if "" = query and not ( stats or uptodate or forcemerge ) then
  91. Usage
  92. end if
  93. DoQuery query, catalog, locale, forceci, forcemerge, shallow, dialect, machine, columns, scope, quiet, sort, stats, uptodate, maxhits, firsthits, groupby
  94. else
  95. if "" <> query then call Usage
  96. ' Open the input file and treat each line as a query.
  97. ' Report errors, but don't stop reading queries.
  98. set fs = WScript.CreateObject( "Scripting.FileSystemObject" )
  99. set f = fs.OpenTextFile( inputfile, 1 )
  100. do until f.AtEndOfStream
  101. line = f.ReadLine
  102. on error resume next
  103. DoQuery line, catalog, locale, forceci, forcemerge, shallow, dialect, machine, columns, scope, quiet, sort, stats, uptodate, maxhits, firsthits, groupby
  104. if 0 <> Err.Number then
  105. out Err.Description
  106. out "The query '" & line & "' failed, error 0x" & Hex( Err.Number )
  107. Err.Clear
  108. end if
  109. out ""
  110. loop
  111. end if
  112. next
  113. end sub
  114. '----------------------------------------------------------------------
  115. sub Out( str )
  116. WScript.echo str
  117. end sub
  118. sub Out2( num, str )
  119. out right( space( 9 ) & num, 9 ) & " " & str
  120. end sub
  121. '----------------------------------------------------------------------
  122. sub Usage
  123. out "usage: cscript qu.vbs <query> [arguments]"
  124. out " <query> An Indexing Service query."
  125. out " arguments:"
  126. out " /a:groupby Columns over which results are grouped."
  127. out " /c:catalog Name of the catalog, default is SYSTEM."
  128. out " /e:locale ISO locale identifier, e.g. EN-US; default is system locale."
  129. out " /f:(+|-) + or -, for force use of index. Default is +."
  130. out " /g Force a master merge."
  131. out " /i:inputfile Text input file with queries, one per line."
  132. out " /j Just return files in the scope path, and not subdirectories."
  133. out " /l:dialect 1 or 2, for old or new dialect, default is 1."
  134. out " /m:machine Name of the machine, default is local machine."
  135. out " /o:columns Output column list, default is path."
  136. out " /p:scope The scope path of the query, absolute or relative."
  137. out " /q Quiet. Don't display info other than query results."
  138. out " /r:# Number of times to repeat the command."
  139. out " /s:sort Sort column list, default is none. e.g.: write[d]."
  140. out " Append [a] for ascending (default) or [d] for descending."
  141. out " /t Display catalog statistics."
  142. out " /u Check if the catalog is up to date."
  143. out " /x:maxhits Maximum number of hits to retrieve, default is no limit."
  144. out " /y:firsthits Only look at the first N hits."
  145. out ""
  146. out " examples: cscript qu.vbs mango /o:size,path"
  147. out " cscript qu.vbs ""peach and not apple"" /s:rank[d] /p:."
  148. out " cscript qu.vbs ""@size > 1000000"" /o:size,path /s:size[a] /m:dogfood"
  149. out " cscript qu.vbs ""@docauthor joe"" /o:docauthor,path /s:docauthor,path"
  150. out " cscript qu.vbs apricot /p:c:\\files"
  151. out " cscript qu.vbs /m:index1 /c:sources pear"
  152. out ""
  153. out " columns: path vpath directory filename write create size attrib"
  154. out " rank hitcount workid fileindex"
  155. out " docauthor doclastauthor dockeywords docsubject doctitle"
  156. out ""
  157. out " locales: af ar-ae ar-bh ar-dz ar-eg ar-iq ar-jo ar-kw ar-lb"
  158. out " ar-ly ar-ma ar-om ar-qa ar-sa ar-sy ar-tn ar-ye be"
  159. out " bg ca cs da de de-at de-ch de-li de-lu e en en"
  160. out " en-au en-bz en-ca en-gb en-ie en-jm en-nz en-tt"
  161. out " en-us en-za es es es-ar es-bo es-c es-co es-cr"
  162. out " es-do es-ec es-gt es-hn es-mx es-ni es-pa es-pe"
  163. out " es-pr es-py es-sv es-uy es-ve et eu fa fi fo fr"
  164. out " fr-be fr-ca fr-ch fr-lu gd gd-ie he hi hr hu in"
  165. out " is it it-ch ja ji ko ko lt lv mk ms mt n neutr"
  166. out " nl-be no no p pt pt-br rm ro ro-mo ru ru-mo s sb"
  167. out " sk sq sr sr sv sv-fi sx sz th tn tr ts uk ur ve"
  168. out " vi xh zh-cn zh-hk zh-sg zh-tw zu"
  169. WScript.Quit( 2 )
  170. end sub
  171. '----------------------------------------------------------------------
  172. function FormatValue( v, t )
  173. if 7 = t or 137 = t then
  174. w = 21
  175. elseif 2 = t or 3 = t or 4 = t or 5 = t or 14 = t or 17 = t or 18 = t or 19 = t then
  176. w = 7
  177. elseif 20 = t or 21 = t then
  178. w = 12
  179. else
  180. w = 0
  181. end if
  182. if 0 = w then
  183. r = v
  184. else
  185. r = right( space( w ) & v, w )
  186. end if
  187. FormatValue = r
  188. end function
  189. '----------------------------------------------------------------------
  190. function DisplayGroupedRowset( rs, level )
  191. const cRowsToGet = 20
  192. rs.CacheSize = cRowsToGet
  193. cHits = 0
  194. do until rs.EOF
  195. row = ""
  196. fChild = FALSE
  197. for c = 0 to rs.Fields.Count - 1
  198. if rs( c ).Name <> "Chapter" then
  199. row = row & " " & rs( c ).Value
  200. else
  201. set rsChild = rs.Fields( "Chapter" ).Value
  202. fChild = TRUE
  203. end if
  204. next
  205. out space( level * 2 ) & row
  206. cHits = cHits + 1
  207. if fChild then
  208. x = DisplayGroupedRowset( rsChild, level + 1 )
  209. rsChild.Close
  210. set rsChild = nothing
  211. end if
  212. rs.MoveNext
  213. loop
  214. DisplayGroupedRowset = cHits
  215. end function
  216. '----------------------------------------------------------------------
  217. function DisplayRowset( rs )
  218. ' Display the results, 20 rows at a time for performance
  219. const cRowsToGet = 20
  220. rs.CacheSize = cRowsToGet
  221. cHits = 0
  222. do until rs.EOF
  223. rows = rs.GetRows( cRowsToGet )
  224. for r = 0 to UBound( rows, 2 )
  225. row = ""
  226. for col = 0 to UBound( rows, 1 )
  227. if 0 <> col then row = row & " "
  228. row = row & FormatValue( rows( col, r ), rs( col ).type )
  229. next
  230. out row
  231. cHits = cHits + 1
  232. next
  233. loop
  234. DisplayRowset = cHits
  235. end function
  236. '----------------------------------------------------------------------
  237. sub DoQuery( query, catalog, locale, forceci, forcemerge, shallow, dialect, machine, columns, scope, quiet, sort, stats, uptodate, maxhits, firsthits, groupby )
  238. if "" <> query then
  239. ' Create the query object and set the query properties
  240. set q = WScript.CreateObject( "ixsso.Query" )
  241. q.Query = query
  242. q.Catalog = "query://" & machine & "/" & catalog
  243. q.AllowEnumeration = not forceci
  244. q.Dialect = dialect
  245. q.Columns = columns
  246. q.CiScope = scope
  247. if shallow then q.CiFlags = "shallow"
  248. if "" <> sort then q.SortBy = sort
  249. if "" <> groupby then q.GroupBy = groupby
  250. ' FirstRows is only supported on Windows XP and later versions, though
  251. if 0 <> maxhits then
  252. if firsthits then
  253. q.FirstRows = maxhits
  254. else
  255. q.MaxRecords = maxhits
  256. end if
  257. end if
  258. if "" <> locale then
  259. set u = WScript.CreateObject( "ixsso.Util" )
  260. q.LocaleId = u.ISOToLocaleID( locale )
  261. end if
  262. set rs = q.CreateRecordSet( "sequential" )
  263. if "" <> groupby then
  264. cHits = DisplayGroupedRowset( rs, 0 )
  265. else
  266. cHits = DisplayRowset( rs )
  267. end if
  268. ' Display query status information
  269. if not quiet then
  270. out CHR(10) & cHits & " files matched the query '" & query & "'"
  271. if q.OutOfDate then
  272. out "The index is out of date"
  273. end if
  274. if q.QueryTimedOut then
  275. out "The query timed out"
  276. end if
  277. if q.QueryIncomplete then
  278. out "The query results are incomplete; may require enumeration"
  279. end if
  280. end if
  281. end if
  282. ' Display catalog status information and/or force a merge
  283. if stats or uptodate or forcemerge then
  284. set a = WScript.CreateObject( "microsoft.ISAdm" )
  285. a.MachineName = machine
  286. set c = a.GetCatalogByName( catalog )
  287. if forcemerge then c.ForceMasterMerge
  288. if stats then
  289. out "Machine: " & machine
  290. out "Catalog: " & catalog
  291. out "Location: " & c.CatalogLocation
  292. out2 c.TotalDocumentCount, "Documents in the catalog"
  293. out2 c.FreshTestCount, "Documents modified since the last master merge"
  294. out2 c.FilteredDocumentCount, "Documents filtered since the service started"
  295. out2 c.DocumentsToFilter, "Documents to filter"
  296. out2 c.DelayedFilterCount, "Documents deferred for filtering"
  297. out2 c.UniqueKeyCount, "Unique keys in the master index"
  298. out2 c.WordListCount, "Wordlists"
  299. out2 c.PersistentIndexCount, "Saved indexes"
  300. out2 c.QueryCount, "Queries executed since the service started"
  301. out2 c.IndexSize, "Megabytes used for index files"
  302. out2 c.PendingScanCount, "Scans scheduled"
  303. s = c.StateInfo
  304. if s and &H1 then out "Shadow merge " & c.PctMergeComplete & "% complete"
  305. if s and &H2 then out "Master merge " & c.PctMergeComplete & "% complete"
  306. if s and &H8 then out "Annealing merge " & c.PctMergeComplete & "% complete"
  307. if s and &H20 then out "Recovery in progress..."
  308. if s and &H80 then out "Indexing paused due to low memory"
  309. if s and &H100 then out "Indexing paused due to high system I/O"
  310. if s and &H400 then out "Catalog is read-only"
  311. if s and &H800 then out "Indexing paused due to running on battery power"
  312. if s and &H1000 then out "Indexing paused due to busy interactive user"
  313. if s and &H2000 then out "Indexing service is starting..."
  314. if s and &H4000 then out "Reading the NTFS USN log(s)"
  315. end if
  316. if uptodate then
  317. if c.IsUpToDate then
  318. out "The catalog is up to date."
  319. else
  320. out "The catalog is not up to date."
  321. end if
  322. end if
  323. end if
  324. end sub