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.

78 lines
2.3 KiB

  1. '//on error resume next
  2. set objArgs = wscript.Arguments
  3. if objArgs.count < 1 then
  4. wscript.echo "Usage defragAnalysis volume"
  5. wscript.quit(1)
  6. end if
  7. strVolume = Replace(objArgs(0), "\", "\\")
  8. '// Get the volume
  9. strQuery = "select * from Win32_Volume where Name = '" & strVolume & "'"
  10. set VolumeSet = GetObject("winmgmts:").ExecQuery(strQuery)
  11. for each obj in VolumeSet
  12. set Volume = obj
  13. exit for
  14. next
  15. wscript.echo "Volume: " & Volume.Name
  16. ' illustration of how to handle input parameters - not tested
  17. Set objMethod = Volume.Methods_.Item("Defrag")
  18. Set objInParams = objMethod.InParameters.SpawnInstance_
  19. objInParams.Force = True
  20. fNotDone = True
  21. Set objSink = WScript.CreateObject("WbemScripting.SWbemSink","SINK_")
  22. Volume.ExecMethodAsync_ objSink, "Defrag", objInParams
  23. WScript.Echo "Waiting for data return..."
  24. wscript.Sleep(2000)
  25. objSink.cancel
  26. while (fNotDone)
  27. wscript.Sleep(1000)
  28. wend
  29. Sub SINK_OnObjectReady(objObject, objAsyncContext)
  30. 'WScript.Echo objObject.Name
  31. WScript.Echo "ObjectReady"
  32. Result = objObject.ReturnValue
  33. wscript.echo "Defrag returned: " & Result & " : " & MapErrorCode("Win32_Volume", "Defrag", Result)
  34. Set objAnalysis = objObject.DefragAnalysis
  35. if Result = 0 then
  36. wscript.echo "TotalFragmentation: " & objAnalysis.TotalPercentFragmentation
  37. end if
  38. End Sub
  39. Sub SINK_OnProgress(intTotal, intCurrent, strMessage, objContext)
  40. WScript.Echo "OnProgress: " & intCurrent & "/" & intTotal
  41. if intCurrent > 25 AND fNotCancel = True then
  42. wscript.echo "OnProgress: progress above 25% cancelling call..."
  43. fNotCancel = False
  44. end if
  45. End Sub
  46. Sub SINK_OnCompleted(intHresult, objError, objContext)
  47. WScript.Echo "method complete hresult: " & intHresult
  48. fNotDone = False
  49. End Sub
  50. Function MapErrorCode(ByRef strClass, ByRef strMethod, ByRef intCode)
  51. set objClass = GetObject("winmgmts:").Get(strClass, &h20000)
  52. set objMethod = objClass.methods_(strMethod)
  53. values = objMethod.qualifiers_("values")
  54. if ubound(values) < intCode then
  55. wscript.echo " FAILURE - no error message found for " & intCode & " : " & strClass & "." & strMethod
  56. MapErrorCode = ""
  57. else
  58. MapErrorCode = values(intCode)
  59. end if
  60. End Function