Get DEFRAG analysis from vbscript code from Windows 2003


/ Published in: Visual Basic
Save to your folder(s)

Copy and paste this code in "defrag.vbs" then on the command prompt call
"cscript.exe defrag.vbs > defrag.txt". One nice thing about this code it analyze also mounted volumes. This script only read info !


Copy this code and paste it in your HTML
  1. ' This code simulates the 'defrag /a /v' command except it analyzes
  2. ' all fixed disks, not just a specific one.
  3. ' The Win32_Volume class is new in Windows Server 2003
  4. ' ---------------------------------------------------------------
  5. ' From the book "Windows Server Cookbook" by Robbie Allen
  6. ' ISBN: 0-596-00633-0
  7. ' ---------------------------------------------------------------
  8.  
  9. ' ------ SCRIPT CONFIGURATION ------
  10. strComputer = "."
  11. ' ------ END CONFIGURATION ---------
  12. set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
  13. set colVols = objWMI.ExecQuery("Select * from Win32_Volume where DriveType = 3")
  14.  
  15. for each objVol in colVols
  16. WScript.Echo "Analyzing volume " & objVol.DriveLetter
  17.  
  18. intRC = objVol.DefragAnalysis(boolDefrag, objRpt)
  19. if intRC = 0 then
  20.  
  21. WScript.Echo " Volume size: " & objRpt.VolumeSize
  22. WScript.Echo " Cluster size: " & objRpt.ClusterSize
  23. WScript.Echo " Used space: " & objRpt.UsedSpace
  24. WScript.Echo " Free space: " & objRpt.FreeSpace
  25. WScript.Echo " Percent free space: " & objRpt.FreeSpacePercent
  26. WScript.Echo " Total fragmentation: " & _
  27. objRpt.TotalPercentFragmentation
  28. WScript.Echo " File fragmentation: " & _
  29. objRpt.FilePercentFragmentation
  30. WScript.Echo " Free space fragmentation: " & _
  31. objRpt.FreeSpacePercentFragmentation
  32.  
  33. WScript.Echo " Total files: " & objRpt.TotalFiles
  34. WScript.Echo " Average file size: " & objRpt.AverageFileSize
  35. WScript.Echo " Total fragmented files: " & objRpt.TotalFragmentedFiles
  36. WScript.Echo " Total excess fragments: " & objRpt.TotalExcessFragments
  37. WScript.Echo " Avg fragments per file: " & _
  38. objRpt.AverageFragmentsPerFile
  39.  
  40. WScript.Echo " Page file size: " & objRpt.PageFileSize
  41. WScript.Echo " Total page file fragments: " & _
  42. objRpt.TotalPageFileFragments
  43.  
  44. WScript.Echo " Total folders: " & objRpt.TotalFolders
  45. WScript.Echo " Fragmented folders: " & objRpt.FragmentedFolders
  46. WScript.Echo " Excess folder fragments: " & _
  47. objRpt.ExcessFolderFragments
  48.  
  49. WScript.Echo " Total MFT size: " & objRpt.TotalMFTSize
  50. WScript.Echo " MFT record count: " & objRpt.MFTRecordCount
  51. WScript.Echo " MFT percent in use: " & objRpt.MFTPercentInUse
  52. WScript.Echo " Total MFT fragments: " & objRpt.TotalMFTFragments
  53.  
  54. if boolDefrag = True Then
  55. WScript.Echo "You should defragment this volume."
  56. else
  57. WScript.Echo "You do not need to defragment this volume."
  58. end if
  59. WScript.Echo
  60. else
  61. WScript.Echo "Error during defragmentation analysis: " & intRC
  62. end if
  63. next

URL: http://techtasks.com/code/viewbookcode/484

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.