Return to Snippet

Revision: 12839
at March 30, 2009 14:45 by pollusb


Initial Code
' This code simulates the 'defrag /a /v' command except it analyzes 
' all fixed disks, not just a specific one.  
' The Win32_Volume class is new in Windows Server 2003
' ---------------------------------------------------------------
' From the book "Windows Server Cookbook" by Robbie Allen
' ISBN: 0-596-00633-0
' ---------------------------------------------------------------

' ------ SCRIPT CONFIGURATION ------
strComputer = "."
' ------ END CONFIGURATION ---------
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colVols = objWMI.ExecQuery("Select * from Win32_Volume where DriveType = 3")

for each objVol in colVols
    WScript.Echo "Analyzing volume " & objVol.DriveLetter

    intRC = objVol.DefragAnalysis(boolDefrag, objRpt)
    if intRC = 0 then

        WScript.Echo " Volume size: " & objRpt.VolumeSize       
        WScript.Echo " Cluster size: " & objRpt.ClusterSize
        WScript.Echo " Used space: " & objRpt.UsedSpace
        WScript.Echo " Free space: " & objRpt.FreeSpace
        WScript.Echo " Percent free space: " & objRpt.FreeSpacePercent
        WScript.Echo " Total fragmentation: " & _
                     objRpt.TotalPercentFragmentation
        WScript.Echo " File fragmentation: " & _
                     objRpt.FilePercentFragmentation
        WScript.Echo " Free space fragmentation: " & _
                     objRpt.FreeSpacePercentFragmentation

        WScript.Echo " Total files: " & objRpt.TotalFiles
        WScript.Echo " Average file size: " & objRpt.AverageFileSize
        WScript.Echo " Total fragmented files: " & objRpt.TotalFragmentedFiles
        WScript.Echo " Total excess fragments: " & objRpt.TotalExcessFragments
        WScript.Echo " Avg fragments per file: " & _
                     objRpt.AverageFragmentsPerFile

        WScript.Echo " Page file size: " & objRpt.PageFileSize
        WScript.Echo " Total page file fragments: " & _
                     objRpt.TotalPageFileFragments

        WScript.Echo " Total folders: " & objRpt.TotalFolders
        WScript.Echo " Fragmented folders: " & objRpt.FragmentedFolders
        WScript.Echo " Excess folder fragments: " & _
                     objRpt.ExcessFolderFragments

        WScript.Echo " Total MFT size: " & objRpt.TotalMFTSize
        WScript.Echo " MFT record count: " & objRpt.MFTRecordCount
        WScript.Echo " MFT percent in use: " & objRpt.MFTPercentInUse
        WScript.Echo " Total MFT fragments: " & objRpt.TotalMFTFragments

        if boolDefrag = True Then
           WScript.Echo "You should defragment this volume."
        else
           WScript.Echo "You do not need to defragment this volume."
        end if
        WScript.Echo
    else
        WScript.Echo "Error during defragmentation analysis: " & intRC
    end if
next

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

Initial Description
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 !

Initial Title
Get DEFRAG analysis from vbscript code from Windows 2003

Initial Tags

                                

Initial Language
Visual Basic