Analyze disks (Perl)

This code can be found in Chapter 3 of Windows Server Cookbook

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the Perl code from this site.

# 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

# ---------------------------------------------------------------
# Adapted from VBScript code contained in the book:
#      "Windows Server Cookbook" by Robbie Allen
# ISBN: 0-596-00633-0
# ---------------------------------------------------------------

use Win32::OLE qw(in);
$Win32::OLE::Warn = 3;

# ------ SCRIPT CONFIGURATION ------
$strComputer = '.';
# ------ END CONFIGURATION ---------
$objWMI = Win32::OLE->GetObject('winmgmts:\\\\' . $strComputer . '\\root\\cimv2');
$colVols = $objWMI->ExecQuery('Select * from Win32_Volume where DriveType = 3');

foreach my $objVol (in $colVols) {
    print 'Analyzing volume ' . $objVol->DriveLetter, "\n";

    $intRC = $objVol->DefragAnalysis($boolDefrag, $objRpt);
    if ($intRC == 0) {

        print ' Volume size: ' . $objRpt->VolumeSize, "\n";
        print ' Cluster size: ' . $objRpt->ClusterSize, "\n";
        print ' Used space: ' . $objRpt->UsedSpace, "\n";
        print ' Free space: ' . $objRpt->FreeSpace, "\n";
        print ' Percent free space: ' . $objRpt->FreeSpacePercent, "\n";
        print ' Total fragmentation: ' . $objRpt->TotalPercentFragmentation, "\n";
        print ' File fragmentation: ' . $objRpt->FilePercentFragmentation, "\n";
        print ' Free space fragmentation: ' . $objRpt->FreeSpacePercentFragmentation, "\n";

        print ' Total files: ' . $objRpt->TotalFiles, "\n";
        print ' Average file size: ' . $objRpt->AverageFileSize, "\n";
        print ' Total fragmented files: ' . $objRpt->TotalFragmentedFiles, "\n";
        print ' Total excess fragments: ' . $objRpt->TotalExcessFragments, "\n";
        print ' Avg fragments per file: ' . $objRpt->AverageFragmentsPerFile, "\n";

        print ' Page file size: ' . $objRpt->PageFileSize, "\n";
        print ' Total page file fragments: ' . $objRpt->TotalPageFileFragments, "\n";

        print ' Total folders: ' . $objRpt->TotalFolders, "\n";
        print ' Fragmented folders: ' . $objRpt->FragmentedFolders, "\n";
        print ' Excess folder fragments: ' . $objRpt->ExcessFolderFragments, "\n";

        print ' Total MFT size: ' . $objRpt->TotalMFTSize, "\n";
        print ' MFT record count: ' . $objRpt->MFTRecordCount, "\n";
        print ' MFT percent in use: ' . $objRpt->MFTPercentInUse, "\n";
        print ' Total MFT fragments: ' . $objRpt->TotalMFTFragments, "\n";

        if ($boolDefrag == 1) {
            print "You should defragment this volume.\n";
        }
        else {
            print "You do not need to defragment this volume.\n";
        }
        print "\n";
    }
    else {
        print 'Error during defragmentation analysis: ' . $intRC, "\n";
    }
}

This code has been viewed 1397 times.

New from the creators of TechTasks.com: StatSheet.com