Powershell: Stopping active defrag
Ok, so I can start up a defrag in powershell, but if after 'x' period of time it's not done, is there a graceful way to kill the active defrag?  I can do a returnvalue at said interval to get current status, and if not complete I want to send it a stop command.  Outside of killing the defrag.exe or dfrgntfs.exe process cold (not advisable), is there a method or call within powershell to do this?  Please, oh please, oh please, oh please?
March 3rd, 2010 10:10pm

I`m not quite sure how to cancel the defrag using WMI and PowerShell, however, you can get a status by running:
((gwmi win32_volume)[0]).Defraganalysis()
[0] are the index of the drive currently bein defragged. Usually index 0 is the C drive, 1 is the D drive and so on. To check this, run gwmi win32_volume

To see what the ReturnValue means:
http://msdn.microsoft.com/en-us/library/aa389832(VS.85).aspx

I`ll let you know if I can dig up some more information.
Free Windows Admin Tool Kit Click here and download it now
March 3rd, 2010 10:42pm

Ya, I have all that in the script now, including a switch for returnvalue (successful, dirty bit, not enough free space, etc).  What I want to do is come behind that after 'x' period of time, do an analysis and if -ne successful (returnvalue of 0), stop the defrag.  It's the stop part I can't seem to find info on, or perhaps it's because it doesn't exist.  I've looked at the callcancel feature, but not sure how that works.  Not sure if it would actually stop the defrag (gracefully, which is what I would want), of if it simply tells powershell to go on without waiting on return/response from last call/command.

March 3rd, 2010 11:15pm

I don`t have a system to test on right now, but you could try to set up an event sink before initializing the defrag, and then call the Cancel method on the object:
$a = new-object -com WbemScripting.SWbemSink

http://msdn.microsoft.com/en-us/library/aa393877(VS.85).aspx
Free Windows Admin Tool Kit Click here and download it now
March 4th, 2010 12:53am

Hey TobieJ,

Were you successful getting your defrag script processing to end cleanly if it ran overtime?  I have a similar requirement - have to shutdown defrags when prime shift starts - but have been totally unsuccesful getting any automated stop to work.

Are you willing / able to share you script(s)?

 

thanks, Roland

July 29th, 2010 11:27pm

Did you find away to stop defrag?
Free Windows Admin Tool Kit Click here and download it now
January 13th, 2014 5:28pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics