Jump to content

Wmi Defrag not working


Recommended Posts

i thought this would been done before but i cant find the answer when i search defrag

seems to work on using vb code to defrag with wmi but i get error 11 with autoit witch means unknown error

$WMIService = ObjGet("winmgmts:\\.\root\cimv2")
    $WMIVolumes = $WMIService.ExecQuery("Select * from Win32_Volume WHERE name = 'C:\\'")
    
    If IsObj($WMIVolumes) Then
        For $Volume In $WMIVolumes
;~          $Test = $Volume.DefragAnalysis ; just testing 
            $errR = $Volume.Defrag()
        Next

        If $errR = 0 Then
            MsgBox(0, 'Defrag', "Drive " & $Volume.Name & " Successfully Defragged.")
        Else
            MsgBox(0, 'Defrag', "Error = " & $errR & "  Defrag Failed.")
        EndIf
    EndIf
;~  MsgBox(0, 'Moo', "Drive " & $test)

i have tried with Admin.. this is why im using message box rather than console for the error
next would be check disk with back sectors but 1 thing at a time

(Defrag)
i do know there is a cmd command for this . have also done this by Open gui window then using control clicks so i can see a progress as well but i like to try different ways to learn.. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...