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.

296 lines
9.0 KiB

  1. '
  2. ' Copyright (c) Microsoft Corporation. All rights reserved.
  3. '
  4. ' VBScript Source File
  5. '
  6. ' Script Name: IIsApp.vbs
  7. '
  8. Option Explicit
  9. On Error Resume Next
  10. ' Error codes
  11. Const ERR_OK = 0
  12. Const ERR_GENERAL_FAILURE = 1
  13. '''''''''''''''''''''
  14. ' Messages
  15. Const L_Gen_ErrorMessage = "%1 : %2"
  16. Const L_CmdLib_ErrorMessage = "Could not create an instance of the CmdLib object."
  17. Const L_ChkCmdLibReg_ErrorMessage = "Please register the Microsoft.CmdLib component."
  18. Const L_ScriptHelper_ErrorMessage = "Could not create an instance of the IIsScriptHelper object."
  19. Const L_ChkScpHelperReg_ErrorMessage = "Please, check if the Microsoft.IIsScriptHelper is registered."
  20. Const L_InvalidSwitch_ErrorMessage = "Invalid switch: %1"
  21. Const L_NotEnoughParams_ErrorMessage = "Not enough parameters."
  22. Const L_Query_ErrorMessage = "Error executing query"
  23. Const L_Serving_Message = "The following W3WP.exe processes are serving AppPool: %1"
  24. Const L_NoW3_ErrorMessage = "Error - no w3wp.exe processes are running at this time"
  25. Const L_PID_Message = "W3WP.exe PID: %1"
  26. Const L_NoResults_ErrorMessage = "Error - no results"
  27. Const L_APID_Message = "W3WP.exe PID: %1 AppPoolId: %2"
  28. Const L_NotW3_ErrorMessage = "ERROR: ProcessId specified is NOT an instance of W3WP.exe - EXITING"
  29. Const L_PIDNotValid_ErrorMessage = "ERROR: PID is not valid"
  30. '''''''''''''''''''''
  31. ' Help
  32. Const L_Empty_Text = ""
  33. ' General help messages
  34. Const L_SeeHelp_Message = "Type IIsApp /? for help."
  35. Const L_Help_HELP_General01_Text = "Description: list IIS worker processes."
  36. Const L_Help_HELP_General02_Text = "Syntax: IIsApp.vbs [/a <app_pool_id> | /p <pid>]"
  37. Const L_Help_HELP_General04_Text = "Parameters:"
  38. Const L_Help_HELP_General05_Text = ""
  39. Const L_Help_HELP_General06_Text = "Value Description"
  40. Const L_Help_HELP_General07_Text = "/a <app_pool_id> report PIDs of currently running"
  41. Const L_Help_HELP_General08_Text = " w3wp.exe processes serving pool"
  42. Const L_Help_HELP_General09_Text = " <app_pool_id>. Surround <app_pool_id>"
  43. Const L_Help_HELP_General10_Text = " with quotes if it contains spaces."
  44. Const L_Help_HELP_General11_Text = " not specified by default and exclusive"
  45. Const L_Help_HELP_General12_Text = " to /p switch."
  46. Const L_Help_HELP_General13_Text = "/p <pid> report AppPoolId of w3wp specified by <pid>"
  47. Const L_Help_HELP_General14_Text = "DEFAULT: no switches will print out the PID and AppPoolId"
  48. Const L_Help_HELP_General15_Text = " for each w3wp.exe"
  49. ''''''''''''''''''''''''
  50. ' Operation codes
  51. Const OPER_BY_NAME = 1
  52. Const OPER_BY_PID = 2
  53. Const OPER_ALL = 3
  54. '
  55. ' Main block
  56. '
  57. Dim oScriptHelper, oCmdLib
  58. Dim intOperation, intResult
  59. Dim strCmdLineOptions
  60. Dim oError
  61. Dim aArgs
  62. Dim apoolID, PID
  63. Dim oProviderObj
  64. ' Default
  65. intOperation = OPER_ALL
  66. Const wmiConnect = "winmgmts:{(debug)}:/root/cimv2"
  67. Const queryString = "select * from Win32_Process where Name='w3wp.exe'"
  68. Const pidQuery = "select * from Win32_Process where ProcessId="
  69. ' get NT WMI provider
  70. Set oProviderObj = GetObject(wmiConnect)
  71. ' Instantiate the CmdLib for output string formatting
  72. Set oCmdLib = CreateObject("Microsoft.CmdLib")
  73. If Err.Number <> 0 Then
  74. WScript.Echo L_CmdLib_ErrorMessage
  75. WScript.Echo L_ChkCmdLibReg_ErrorMessage
  76. WScript.Quit(ERR_GENERAL_FAILURE)
  77. End If
  78. Set oCmdLib.ScriptingHost = WScript.Application
  79. ' Instantiate script helper object
  80. Set oScriptHelper = CreateObject("Microsoft.IIsScriptHelper")
  81. If Err.Number <> 0 Then
  82. WScript.Echo L_ScriptHelper_ErrorMessage
  83. WScript.Echo L_ChkScpHelperReg_ErrorMessage
  84. WScript.Quit(ERR_GENERAL_FAILURE)
  85. End If
  86. Set oScriptHelper.ScriptHost = WScript
  87. ' Check if we are being run with cscript.exe instead of wscript.exe
  88. oScriptHelper.CheckScriptEngine
  89. ' Command Line parsing
  90. Dim argObj, arg
  91. Set argObj = WScript.Arguments
  92. strCmdLineOptions = "a:a:1;p:p:1"
  93. If argObj.Named.Count > 0 Then
  94. Set oError = oScriptHelper.ParseCmdLineOptions(strCmdLineOptions)
  95. If Not oError Is Nothing Then
  96. If oError.ErrorCode = oScriptHelper.ERROR_NOT_ENOUGH_ARGS Then
  97. ' Not enough arguments for a specified switch
  98. WScript.Echo L_NotEnoughParams_ErrorMessage
  99. WScript.Echo L_SeeHelp_Message
  100. Else
  101. ' Invalid switch
  102. oCmdLib.vbPrintf L_InvalidSwitch_ErrorMessage, Array(oError.SwitchName)
  103. WScript.Echo L_SeeHelp_Message
  104. End If
  105. WScript.Quit(ERR_GENERAL_FAILURE)
  106. End If
  107. If oScriptHelper.GlobalHelpRequested Then
  108. DisplayHelpMessage
  109. WScript.Quit(ERR_OK)
  110. End If
  111. For Each arg In oScriptHelper.Switches
  112. Select Case arg
  113. Case "a"
  114. apoolID = oScriptHelper.GetSwitch(arg)
  115. intOperation = OPER_BY_NAME
  116. Case "p"
  117. PID = oScriptHelper.GetSwitch(arg)
  118. intOperation = OPER_BY_PID
  119. End Select
  120. Next
  121. End If
  122. ' Choose operation
  123. Select Case intOperation
  124. Case OPER_BY_NAME
  125. intResult = GetByPool(apoolID)
  126. Case OPER_BY_PID
  127. intResult = GetByPid(PID)
  128. Case OPER_ALL
  129. intResult = GetAllW3WP()
  130. End Select
  131. ' Return value to command processor
  132. WScript.Quit(intResult)
  133. '''''''''''''''''''''''''
  134. ' End Of Main Block
  135. '''''''''''''''''''''
  136. '''''''''''''''''''''''''''
  137. ' DisplayHelpMessage
  138. '''''''''''''''''''''''''''
  139. Sub DisplayHelpMessage()
  140. WScript.Echo L_Help_HELP_General01_Text
  141. WScript.Echo L_Empty_Text
  142. WScript.Echo L_Help_HELP_General02_Text
  143. WScript.Echo L_Empty_Text
  144. WScript.Echo L_Help_HELP_General04_Text
  145. WScript.Echo L_Help_HELP_General05_Text
  146. WScript.Echo L_Help_HELP_General06_Text
  147. WScript.Echo L_Help_HELP_General07_Text
  148. WScript.Echo L_Help_HELP_General08_Text
  149. WScript.Echo L_Help_HELP_General09_Text
  150. WScript.Echo L_Help_HELP_General10_Text
  151. WScript.Echo L_Help_HELP_General11_Text
  152. WScript.Echo L_Help_HELP_General12_Text
  153. WScript.Echo L_Help_HELP_General13_Text
  154. WScript.Echo L_Empty_Text
  155. WScript.Echo L_Help_HELP_General14_Text
  156. WScript.Echo L_Help_HELP_General15_Text
  157. End Sub
  158. Function GetAppPoolId(strArg)
  159. Dim Submatches
  160. Dim strPoolId
  161. Dim re
  162. Dim Matches
  163. On Error Resume Next
  164. Set re = New RegExp
  165. re.Pattern = "-ap ""(.+)"""
  166. re.IgnoreCase = True
  167. Set Matches = re.Execute(strArg)
  168. Set SubMatches = Matches(0).Submatches
  169. strPoolId = Submatches(0)
  170. GetAppPoolId = strPoolId
  171. End Function
  172. Function GetByPool(strPoolName)
  173. Dim W3WPList
  174. Dim strQuery
  175. Dim W3WP
  176. On Error Resume Next
  177. strQuery = queryString
  178. Set W3WPList = oProviderObj.ExecQuery(strQuery)
  179. If (Err.Number <> 0) Then
  180. WScript.Echo L_Query_ErrorMessage
  181. oCmdLib.vbPrintf L_Gen_ErrorMessage, Array(Hex(Err.Number), Err.Description)
  182. GetByPid = 2
  183. Else
  184. oCmdLib.vbPrintf L_Serving_Message, Array(strPoolName)
  185. If (W3WPList.Count < 1) Then
  186. WScript.Echo L_NoW3_ErrorMessage
  187. GetByPool = 1
  188. Else
  189. For Each W3WP In W3WPList
  190. If (UCase(GetAppPoolId(W3WP.CommandLine)) = UCase(strPoolName)) Then
  191. oCmdLib.vbPrintf L_PID_Message, Array(W3WP.ProcessId)
  192. End If
  193. Next
  194. GetByPool = 0
  195. End If
  196. End If
  197. End Function
  198. Function GetByPid(pid)
  199. Dim W3WPList
  200. Dim strQuery
  201. Dim W3WP
  202. On Error Resume Next
  203. If (IsNumeric(pid)) Then
  204. strQuery = pidQuery & pid
  205. Set W3WPList = oProviderObj.ExecQuery(strQuery)
  206. If (Err.Number <> 0) Then
  207. WScript.Echo L_Query_ErrorMessage
  208. oCmdLib.vbPrintf L_Gen_ErrorMessage, Array(Hex(Err.Number), Err.Description)
  209. GetByPid = 2
  210. Else
  211. If (W3WPList.Count < 1) Then
  212. WScript.Echo L_NoResults_ErrorMessage
  213. GetByPid = 2
  214. Else
  215. For Each W3WP In W3WPList
  216. If (W3WP.Name = "w3wp.exe") Then
  217. oCmdLib.vbPrintf L_APID_Message, Array(pid, GetAppPoolId(W3WP.CommandLine))
  218. GetByPid = 0
  219. Else
  220. WScript.Echo(L_NotW3_ErrorMessage)
  221. GetByPid = 2
  222. End If
  223. Next
  224. End If
  225. End If
  226. Else
  227. WScript.Echo(L_PIDNotValid_ErrorMessage)
  228. GetByPid = 2
  229. End If
  230. End Function
  231. Function GetAllW3WP()
  232. Dim W3WPList
  233. Dim strQuery
  234. Dim W3WP
  235. On Error Resume Next
  236. strQuery = queryString
  237. Set W3WPList = oProviderObj.ExecQuery(strQuery)
  238. If (Err.Number <> 0) Then
  239. WScript.Echo L_Query_ErrorMessage
  240. oCmdLib.vbPrintf L_Gen_ErrorMessage, Array(Hex(Err.Number), Err.Description)
  241. GetByPid = 2
  242. Else
  243. If (W3WPList.Count < 1) Then
  244. WScript.Echo L_NoResults_ErrorMessage
  245. GetAllW3WP = 2
  246. Else
  247. For Each W3WP In W3WPList
  248. oCmdLib.vbPrintf L_APID_Message, Array(W3WP.ProcessId, GetAppPoolId(W3WP.CommandLine))
  249. Next
  250. GetAllW3WP = 0
  251. End If
  252. End If
  253. End Function