Click to Rate and Give Feedback
MSDN
MSDN Library
WMI Reference
WMI Classes
Win32 Classes
Win32_Volume
 DefragAnalysis Method of the Win32_...
DefragAnalysis Method of the Win32_Volume Class

The DefragAnalysis method generates a fragmentation analysis for a volume.

This topic uses Managed Object Format (MOF) syntax. For more information about using this method, see Calling a Method.

Syntax

MOF
uint32 DefragAnalysis(
  [out]  boolean DefragRecommended,
  [out]  object DefragAnalysis
);

Parameters

DefragRecommended [out]

If true, defragmentation of the volume is recommended.

On Windows Vista, this value will be true if the file fragmentation on the volume exceeds 10%. On earlier versions of Windows, this value will be true if the average of the percent file fragmentation and percent free space fragmentation of the volume exceeds 10%.

DefragAnalysis [out]

Embedded instance of a Win32_DefragAnalysis containing the properties that describe the extent to which the volume is fragmented.

Return Value

Returns one of the following numeric values.

Return codeDescription
0

Success

1

Access Denied

2

Not Supported

3

Volume Dirty Bit Is Set

4

Not Enough Free Space

5

Corrupt Master File Table Detected

6

Call Canceled

7

Call Cancellation Request Too Late

8

Defrag Engine Is Already Running

9

Unable To Connect To Defrag Engine

10

Defrag Engine Error

11

Unknown Error

 

Examples

For script code examples, see WMI Tasks for Scripts and Applications and the TechNet ScriptCenter Script Repository.

For C++ code examples, see WMI C++ Application Examples.

Requirements

Minimum supported clientNone supported
Minimum supported serverWindows Server 2003
MOFVds.mof
DLLVdswmi.dll
Namespace\root\cimv2

See Also

Win32_Volume

Send comments about this topic to Microsoft

Build date: 11/3/2009

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Win32_Volume Class DefragAnalysis sample using PowerShell      Thomas Lee   |   Edit   |   Show History
# WMI-DefragStats.ps1
# Uses WMI to get defrag statistics from the local C:
# Only tested on Longhorn - does not work on XP
# thomas lee - tfl@psp.co.uk

# get volumes on local system
$v = get-wmiobject win32_volume
# Display Number of volumes
"Number of volumes {0}: " -f $v.length
# Now get the C:\ volume
$v1=$v | where {$_.name -eq "C:\"}
# Perform a defrag analysis on the C: drive
"Performing Defrag Analysis"
$dfa = $v1.DefragAnalysis().DefragAnalysis
# Display results
"";"Defrag Results - defrag of C:"
"-----------------------------"

"Average File Size (KB) : {0} KB" -f ($dfa.AverageFileSize/1kb)
"Average Fragments per File : {0} " -f $dfa.averageFragmentsPerfile
"Average Free Space per Extent : {0} " -f $dfa.AverageFreeSpacePerExtent
"Cluster Size (KB) : {0} " -f ($dfa.clustersize/1KB)
"Excess Folder Fragments : {0} " -f $dfa.ExcessFolderFragments
"File Percent Fragementation : {0} " -f $dfa.FilePercentFragementation
"Fragmented folders : {0} " -f $dfa.FragmentedFolders
"Free Space (GB) : {0} GB" -f ($dfa.FreeSpace/1gb)
"Free Space Percent : {0} " -f $dfa.FreeSpacePercent
"Free Space Percent Fragementation : {0} " -f $dfa.FreeSpacePercentFragementaion
"Largest free Space Extent : {0} " -f $dfa.LargestFreeSpaceExtent
"MFT Percent In Use : {0} " -f $dfa.MFTPercentInUse
"MFT Record count : {0} " -f $dfa.MFTRecordCount
"Page File Size : {0} " -f $dfa.PageFileSize
"Total Excess Fragements : {0} " -f $dfa.TotalExcessFragements
"Total Files : {0} " -f $dfa.TotalFiles
"Total Folders : {0} " -f $dfa.TotalFolders
"Total Fragmented Files : {0} " -f $dfa.TotalFragmentedFiles
"Total Free Space Extents : {0} " -f $dfa.TotalFreeSpaceExtents
"Total MFT Fragments : {0} " -f $dfa.TotalMftFragments
"Total MFT Size : {0} " -f $dfa.TotalMftSize
"Total Page File Fragements : {0} " -f $dfa.TotalPageFileFragements
"Total Percent Fragementation : {0} " -f $dfa.TotalPercentFragementation
"Total Unmovable Files : {0} " -f $dfa.TotalUnmovableFiles
"Used Space (GB) : {0} GB" -f ($dfa.UsedSpace/1gb)
"Volume Name : {0} " -f $dfa.VolumeName
"Volume Size (GB) : {0} GB" -f ($dfa.VolumeSize/1gb)


On a Windows Server 2008 VM, this script returned the following output:

  
PS C:\Users\Administrator> .\wmi-defragstats.ps1
Number of volumes 2:
Performing Defrag Analysis

Defrag Results - defrag of C:
-----------------------------
Average File Size (KB) : 194.904296875 KB
Average Fragments per File : 1.04
Average Free Space per Extent : 66928900
Cluster Size (KB) : 4
Excess Folder Fragments : 0
File Percent Fragementation :
Fragmented folders : 1
Free Space (GB) : 55.9121589660645 GB
Free Space Percent : 87
Free Space Percent Fragementation :
Largest free Space Extent : 34251751424
MFT Percent In Use : 91
MFT Record count : 45363
Page File Size : 0
Total Excess Fragements :
Total Files : 37044
Total Folders : 8223
Total Fragmented Files : 14
Total Free Space Extents : 897
Total MFT Fragments : 2
Total MFT Size : 50790400
Total Page File Fragements :
Total Percent Fragementation :
Total Unmovable Files : 24
Used Space (GB) : 8.08490753173828 GB
Volume Name :
Volume Size (GB) : 63.9970664978027 GB
PS C:\Users\Administrator>
Flag as ContentBug
Perl Win32_Volume Class DefragAnalysis sample      Dennis R   |   Edit   |   Show History

Hey all, I've been hacking away at this for a few hours. Getting an OLE VT_DISPATCH object back as an out parameter is a lot trickier than I thought it would be. Hope it helps so that it doesn't take you as long! :-)

This has been tested and works like a charm in ActiveState Perl 5.8.8.

use Win32::OLE('in');
use Win32::OLE::Variant;
 
use constant wbemFlagReturnImmediately => 0x10;
use constant wbemFlagForwardOnly => 0x20;
 
%return_code = (
0 => 'Success',
1 => 'Access denied',
2 => 'Not supported',
3 => 'Volume dirty bit is set',
4 => 'Not enough free space',
5 => 'Corrupt master file table detected',
6 => 'Call canceled',
7 => 'Call cancellation request too late',
8 => 'Defrag engine is already running',
9 => 'Unable to connect to defrag engine',
10 => 'Defrag engine error',
11 => 'Unknown error',
);
 
$host = ".";
$objWMI = Win32::OLE->GetObject
("winmgmts:{impersonationLevel=impersonate}!\\\\$host\\root\\cimv2")
or die "WMI connection failed.\n";
 
$colItems = $objWMI->ExecQuery("Select * from Win32_Volume where " .
"DriveType = 3", "WQL", wbemFlagReturnImmediately | wbemFlagForwardOnly)
or die "Unable to query.\n";
 
foreach $objItem (in $colItems)
{
$df_recommended = Variant(VT_BOOL|VT_BYREF, 0);
$df_a = Variant(VT_DISPATCH|VT_BYREF);
$result = $objItem->DefragAnalysis($df_recommended, $df_a);
$df_a = $df_a->Value();

if ($result == 0) { # Success
print "Volume $objItem->{Name}\n";
print " Defrag recommended: ".($df_recommended ? "Yes" : "No") ."\n";
print " Average file size: ".$df_a->{AverageFileSize} ."\n";
print " Total files: ".$df_a->{TotalFiles} ."\n";
print " File fragmentation: ".$df_a->{FilePercentFragmentation} ."\n";
print " Used space: ".$df_a->{UsedSpace} ."\n";
print " Free space: ".$df_a->{FreeSpace} ."\n";
print " MFT percent in use: ".$df_a->{MFTPercentInUse} ."\n";
# ...and whatever other properties you want to see
print "\n";
}
else {
print "Volume $objItem->{Name}\n";
print " " . $return_code{$result} . "\n";
print "\n";
}
}
Tags What's this?: perl (x) saul (x) wmi (x) Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker