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. ' Copyright (c) Microsoft Corporation. All rights reserved.
  4. '
  5. '-------------------------------------------------------------------------
  6. Const CONST_wbemPrivilegeSecurity=7 'Privilege constant for WMI connection
  7. Const CONST_DELAYBEFORESHUTDOWN = 17000
  8. '---------------------------------------------------------------------------------
  9. '---------------------------------------------------------------------------------
  10. 'Function name: LaunchProcess
  11. 'Description: To launch process through cmd exe
  12. 'Input Variables: strCommand,strCurDir
  13. 'Output Variables: None
  14. 'Returns: None
  15. 'Global Variables: CONST_*
  16. '----------------------------------------------------------------------------------
  17. Function LaunchProcess(strCommand, strCurDir)
  18. Err.Clear
  19. On error Resume Next
  20. Dim objService
  21. Dim objClass
  22. Dim objProc
  23. Dim objProcStartup
  24. Dim nretval
  25. Dim nPID
  26. Dim objTemp
  27. nretval = 0
  28. Set objService=getWMIConnection(CONST_WMI_WIN32_NAMESPACE)
  29. 'objService.Security_.Privileges.Add CONST_wbemPrivilegeSecurity 'giving the req Privilege
  30. Set objClass = objService.Get("Win32_ProcessStartup")
  31. Set objProcStartup = objClass.SpawnInstance_()
  32. objProcStartup.ShowWindow = 2
  33. Set objProc = objService.Get("Win32_Process")
  34. nretval = objProc.Create(strCommand, strCurDir, objProcStartup,nPID)
  35. If Err.number <> 0 Then
  36. nretval=-1
  37. LaunchProcess = nretval
  38. Exit function
  39. End If
  40. set objTemp = objService.Get("Win32_Process.Handle='"&nPID&"'")
  41. while Err.number <> -2147217406
  42. set objTemp = objService.Get("Win32_Process.Handle='"&nPID&"'")
  43. wend
  44. Err.Clear
  45. LaunchProcess = nretval
  46. End Function
  47. '----------------------------------------------------------------------------
  48. ' Function name: isScheduleShutdown
  49. ' Description: Serves in checking if a shutdown job is scheduled or not
  50. ' Input Variables: None
  51. ' Output Variables: None
  52. ' Return Values: True/False
  53. ' Global Variables: CONST_*
  54. ' returns true if a shutdown is scheduled ,false if it is not scheduled.
  55. '----------------------------------------------------------------------------
  56. Function isScheduleShutdown()
  57. On error resume next
  58. Err.Clear()
  59. Dim objWMIConnection 'Wmi connection
  60. Dim strQuery 'wmi query variable
  61. Dim objCommand 'instances object
  62. Dim objInstance 'insances count variable
  63. Const CONST_RESTART = "RESTART"
  64. Const CONST_SHUTDOWN = "SHUTDOWN"
  65. isScheduleShutdown = false
  66. 'Trying to connect to the server
  67. set objWMIConnection = getWMIConnection(CONST_WMI_WIN32_NAMESPACE)
  68. strQuery = "Select * From Win32_ScheduledJob"
  69. set objCommand= objWMIConnection.ExecQuery(strQuery)
  70. If Err.number <> 0 or objCommand.count=0 then
  71. Call SA_TraceOut("inc_Shutdown.asp", "Cannot find whether shutdown/restart is scheduled")
  72. exit function
  73. end if
  74. 'checking whether the wmiclass instance has the scheduled task
  75. For Each objInstance in objCommand
  76. 'checking the scheduled task type
  77. If instr(Ucase(right(objInstance.Command,15)),CONST_RESTART)>0 or instr(Ucase(right(objInstance.Command,15)),CONST_SHUTDOWN)>0 Then
  78. Call SA_TraceOut("inc_Shutdown.asp", "Shutdown/Restart scheduled")
  79. 'Remove this
  80. isScheduleShutdown = true
  81. Exit For
  82. End if
  83. Next
  84. 'Release the objects
  85. Set objWMIConnection = Nothing
  86. Set objCommand = Nothing
  87. End Function
  88. '---------------------------------------------------------------------------------
  89. 'Function name: SendMessage
  90. 'Description: To send message to the users connected to the server appliance
  91. 'Input Variables: strTime
  92. 'Output Variables: None
  93. 'Returns: None
  94. 'Global Variables: In:L_(*)-Localized Strings
  95. '----------------------------------------------------------------------------------
  96. Function SendMessage(strTime,strTask)
  97. Err.Clear
  98. on error resume next
  99. Dim strServerName
  100. Dim objWinNtSysInfo
  101. Dim objWshShell
  102. Dim arrRepStrings 'holds the replacement strings
  103. redim arrRepStrings(3)
  104. SendMessage = false
  105. 'getting the server name
  106. Set objWinNTSysInfo = CreateObject("WinNTSystemInfo")
  107. strServerName = objWinNTSysInfo.ComputerName
  108. arrRepStrings(0) = cstr(strServerName)
  109. if ucase(strTask) = ucase(CONST_RESTART_APPLIANCE) then
  110. arrRepStrings(1) = cstr(L_RESTARTMSG_TEXT)
  111. arrRepStrings(3) = cstr(L_RESTARTMSG_TEXT)
  112. else
  113. arrRepStrings(1) = cstr(L_SHUTDOWNMSG_TEXT)
  114. arrRepStrings(3) = cstr(L_SHUTDOWNNETMSG_TEXT)
  115. end if
  116. arrRepStrings(2) = cstr(strTime)
  117. Set objWshShell =CreateObject("WScript.Shell")
  118. L_NETSENDMESSAGE_TEXT = SA_GetLocString("sashutdown_msg.dll", "40400055", arrRepStrings)
  119. objWshShell.Run "net send /users "& L_NETSENDMESSAGE_TEXT,0,true
  120. If Err.number <> 0 then
  121. SA_SetErrMsg L_UNABLETOSENDMESSAGE_ERRORMESSAGE
  122. SendMessage=False
  123. Exit Function
  124. End If
  125. SendMessage = true
  126. 'release objects
  127. Set objWinNTSysInfo = nothing
  128. Set objWshShell = nothing
  129. End Function
  130. '-------------------------------------------------------------------------
  131. 'Function: DeleteAlert()
  132. 'Description: Clears Scheduled alert
  133. 'Input Variables: Alert ID - alert ID of alert to clear
  134. 'Output Variables: None
  135. 'Returns: True or False
  136. 'Global Variables: None
  137. '-------------------------------------------------------------------------
  138. Function DeleteAlert(AlertID)
  139. on error resume next
  140. err.clear
  141. Dim objAM
  142. Dim rc
  143. Dim objConnection
  144. Dim objAlert
  145. Dim instAlert
  146. Dim intCookie
  147. Dim strQuery
  148. Const CONST_SHUTDOWN = "ShutdownPending"
  149. Const CONST_RESTART = "RestartPending"
  150. DeleteAlert = false
  151. set objConnection = getWMIConnection(CONST_WMI_WIN32_NAMESPACE)
  152. strQuery="Select * FROM Microsoft_SA_Alert WHERE AlertID=" & AlertID & " AND AlertLog=" & "'" & CONST_SHUTDOWN & "'" & " OR AlertLog=" & "'" & CONST_RESTART & "'"
  153. set objAlert = objConnection.Execquery(strQuery)
  154. If Err.number <>0 or objAlert.count = 0 then
  155. Exit Function
  156. end if
  157. Set objAM = GetObject("WINMGMTS:" & SA_GetWMIConnectionAttributes() & "!\\" & GetServerName & "\root\cimv2:Microsoft_SA_Manager=@" )
  158. If ( Err.Number <> 0 ) Then
  159. Call SA_TraceOut(SA_GetScriptFileName(), "Get Microsoft_SA_Manager failed: " + CStr(Hex(Err.Number)) + " " + Err.Description)
  160. Exit Function
  161. End If
  162. For Each instAlert In objAlert
  163. 'if instAlert.AlertID = AlertID AND Then
  164. intCookie = instAlert.Cookie
  165. rc = objAM.ClearAlert(CInt(intCookie))
  166. If rc = 0 And Err = 0 Then
  167. DeleteAlert = True
  168. Else
  169. DeleteAlert = False
  170. End If
  171. 'end if
  172. next
  173. 'Release the object
  174. Set objAM = Nothing
  175. set objConnection = nothing
  176. Set objAlert = nothing
  177. DeleteAlert = true
  178. End Function
  179. '------------------------------------------------------------------------------------
  180. 'Function name :ShutdownRaiseAlert
  181. 'Description :Raise alerts for restart/shutdown
  182. 'Input Variables :CONST_WMI_WIN32_NAMESPACE
  183. 'Localization variables - L_*
  184. 'Output Variables :None
  185. 'Returns :Boolean
  186. '-------------------------------------------------------------------------------------
  187. Function ShutdownRaiseAlert(Alertid,AlertLog,ReplacementString)
  188. Err.Clear
  189. on error resume next
  190. Dim objAS 'holds Appliance services object
  191. Dim rc 'holds return value
  192. Dim objConnection 'holds WMI connection object
  193. Dim oAlertCollection 'holds Alert instances
  194. Dim oAlert 'holds an instance
  195. Dim retval 'holds return value
  196. Dim Rawdata 'holds Rawdata
  197. Dim AlertType 'holds Alert type
  198. Dim TimeToLive 'holds time to live
  199. Dim arrRepStrings 'holds the replacement strings
  200. redim arrRepStrings(2)
  201. arrRepStrings(0) = cstr(ReplacementString(0))
  202. arrRepStrings(1) = cstr(ReplacementString(1))
  203. 'get WMI Connection
  204. set objConnection = getWMIConnection(CONST_WMI_WIN32_NAMESPACE)
  205. If ( Err.Number <> 0 ) Then
  206. Call SA_TraceOut(SA_GetScriptFileName(), "getWMIConnection(CONST_WMI_WIN32_NAMESPACE) failed: " + CStr(Hex(Err.Number)) + " " + Err.Description)
  207. Exit Function
  208. End If
  209. set oAlertCollection = objConnection.instancesOf("Microsoft_SA_Alert")
  210. If ( Err.Number <> 0 ) Then
  211. Call SA_TraceOut(SA_GetScriptFileName(), "objConnection.instancesOf('Microsoft_SA_Alert') failed: " + CStr(Hex(Err.Number)) + " " + Err.Description)
  212. Exit Function
  213. End If
  214. Dim sAlertIn
  215. Dim sAlertThis
  216. sAlertIn = AlertLog+CStr(AlertId)
  217. Call SA_TraceOut(SA_GetScriptFileName(), "Looking for alert matching: " + sAlertIn )
  218. For each oAlert in oAlertCollection
  219. sAlertThis = oAlert.AlertLog + CStr(oAlert.AlertID)
  220. Call SA_TraceOut(SA_GetScriptFileName(), "Checking alert: " + sAlertThis )
  221. If ( sAlertThis = sAlertIn ) Then
  222. Call SA_TraceOut(SA_GetScriptFileName(), "Shutdown Alert exists, exiting ShutdownRaiseAlert" )
  223. Exit Function
  224. End If
  225. Next
  226. 'Create Appliance services object
  227. Set objAS = CreateObject("Appsrvcs.ApplianceServices")
  228. If Err.Number <> 0 Then
  229. Call SA_TraceOut(SA_GetScriptFileName(), "CreateObject(Appsrvcs.ApplianceServices) failed: " + CStr(Hex(Err.Number)) + " " + Err.Description)
  230. SA_SetErrMsg L_UNABLETOGETINSTANCE_ERRORMESSAGE
  231. exit function
  232. End If
  233. call SA_TraceOut("In inc_shutdown", "Created the appliance services obj")
  234. ' Initialize the task
  235. objAS.Initialize()
  236. If Err.Number <> 0 Then
  237. Call SA_TraceOut(SA_GetScriptFileName(), "objAS.Initialize() failed: " + CStr(Hex(Err.Number)) + " " + Err.Description)
  238. SA_SetErrMsg L_UNABLETOGETINSTANCE_ERRORMESSAGE
  239. exit function
  240. End If
  241. ' /**** Structure of Raising Alert ********/
  242. 'long RaiseAlert(
  243. ' [in] long lAlertType,
  244. ' [in] long lAlertId,
  245. ' [in] BSTR bstrAlertLog,
  246. ' [in] BSTR bstrAlertSource,
  247. ' [in] long lTimeToLive,
  248. ' [in] VARIANT* pReplacementStrings,
  249. ' [in] VARIANT* pRawData);
  250. AlertType = 2
  251. TimeToLive = 2147483647
  252. call SA_TraceOut("In inc_shutdown", "calling raisealert func")
  253. retval = objAS.RaiseAlert(AlertType,Alertid,AlertLog,"Microsoft_SA_Resource",TimeToLive,arrRepStrings, Rawdata )
  254. if Err.number <> 0 then
  255. Call SA_TraceOut(SA_GetScriptFileName(), "objAS.RaiseAlert(..) failed: " + CStr(Hex(Err.Number)) + " " + Err.Description)
  256. SA_SetErrMsg L_UNABLETORAISEALERT_ERRORMESSAGE
  257. end if
  258. call SA_TraceOut("In inc_shutdown", "called raisealert func:"+retval)
  259. 'Release the objects
  260. Set objAS = Nothing
  261. set objConnection = nothing
  262. Set oAlertCollection = nothing
  263. End function
  264. '----------------------------------------------------------------------------
  265. ' Function : ExecuteShutdownTask
  266. ' Description: Executes the Shutdown task
  267. ' Input Variables: powerOff - bool indicating power off or restart
  268. ' OutputVariables: None
  269. ' Returns: True/False for success/failure
  270. '
  271. '----------------------------------------------------------------------------
  272. Public Function ExecuteShutdownTask(ByVal powerOff )
  273. Err.Clear
  274. On Error Resume Next
  275. Dim delayBeforeShutdown
  276. Dim objTaskContext ' to hold taskcontext object
  277. Dim objAS ' to hold ApplianceServices object
  278. Const CONST_METHODNAME = "ShutdownAppliance"
  279. 'Function call to get the delay
  280. delayBeforeShutdown = GetShutdownDelay()
  281. 'Initialize to default
  282. ExecuteShutdownTask = FALSE
  283. Set objTaskContext = CreateObject("Taskctx.TaskContext")
  284. If Err.Number <> 0 Then
  285. SA_SetErrMsg L_TASKCTX_FAILED_ERRORMESSAGE & "(" & Hex(Err.Number) & ")"
  286. Exit Function
  287. End If
  288. Set objAS = CreateObject("Appsrvcs.ApplianceServices")
  289. If Err.Number <> 0 Then
  290. SA_SetErrMsg L_UNABLETOGETINSTANCE_ERRORMESSAGE & "(" & Hex(Err.Number) & ")"
  291. Exit Function
  292. End If
  293. '
  294. ' Set task parameters
  295. '
  296. objTaskContext.SetParameter "Method Name", CONST_METHODNAME
  297. objTaskContext.SetParameter "SleepDuration", delayBeforeShutdown
  298. objTaskContext.SetParameter "PowerOff", powerOff
  299. If Err.Number <> 0 Then
  300. SA_SetErrMsg L_SETPARAMETER_FAILED_ERRORMESSAGE & "(" & Hex(Err.Number) & ")"
  301. Exit Function
  302. End If
  303. ' Initialize the task
  304. objAS.Initialize()
  305. If Err.Number <> 0 Then
  306. SA_SetErrMsg L_INITIALIZATION_FAILED_ERRORMESSAGE & "(" & Hex(Err.Number) & ")"
  307. Exit Function
  308. End If
  309. Call objAS.ExecuteTaskAsync("ApplianceShutdownTask", objTaskContext)
  310. If Err.Number <> 0 Then
  311. SA_SetErrMsg L_EXECUTETASK_FAILED_ERRORMESSAGE & "(" & Hex(Err.Number) & ")"
  312. Exit Function
  313. End If
  314. 'Release the objects
  315. Set objAS = Nothing
  316. Set objTaskContext = Nothing
  317. ExecuteShutdownTask = TRUE
  318. End Function
  319. '----------------------------------------------------------------------------
  320. ' Function : GetShutdownDelay
  321. ' Description : support function for getting the no of seconds
  322. ' Input Variables : None
  323. ' OutputVariables : None
  324. ' Returns : int-delay in seconds
  325. ' Global Variables : In:CONST_DELAYBEFORESHUTDOWN
  326. '----------------------------------------------------------------------------
  327. Private Function GetShutdownDelay()
  328. On error resume Next
  329. Dim objRegistry
  330. Dim nShutdownDelay
  331. Set objRegistry = RegConnection()
  332. 'Function call to get the required value from the registry
  333. nShutdownDelay = GetRegkeyValue( objRegistry, _
  334. "SOFTWARE\Microsoft\ServerAppliance\WebFramework",_
  335. "RestartTaskDelay", CONST_DWORD)
  336. 'Cheking for non numeric
  337. If ( not IsNumeric(nShutdownDelay)) Then
  338. nShutdownDelay = CONST_DELAYBEFORESHUTDOWN 'Assign to default
  339. ElseIf nShutdownDelay=0 or nShutdownDelay < CONST_DELAYBEFORESHUTDOWN then
  340. nShutdownDelay = CONST_DELAYBEFORESHUTDOWN 'Assign to default
  341. End If
  342. 'Set to nothing
  343. Set objRegistry = Nothing
  344. GetShutdownDelay=nShutdownDelay
  345. If Err.Number <> 0 Then
  346. Call SA_TraceOut(SA_GetScriptFileName , "error in getting delay")
  347. End If
  348. End Function
  349. %>