Defragment system disks (Python)

You can trigger system defragment with below script:

import subprocess;
process=subprocess.Popen((‘defrag /C’),shell=True,stdout=subprocess.PIPE);
stdout=process.communicate()[0]
print stdout

You can customize it to run over other drives as well.

Example output would be:

Hi @Ilker

I keep getting an error running this script
‘defrag’ is not recognized as an internal or external command, operable program or batch file.

Let us check this out again. Could you please share which OS version you tried as well?

Hi @Ilker

I’ve tried this on Windows 10, Server 2008/2012

It looks like problem on X64 systems rather than specific OS. We are looking what can be done to resolve the issue.

Hi @Ilker

Ok thanx

Can I ask where the output appears? Does it appear in report to admin or on-screen to user or into a sys or other log file? Thanks.

Hello @azon2111

The results are shown here: Device List -> Click on the device -> go to Procedure tab -> you will see an entry there for the procedure that you ran. Click on the Details option to show the results.

Thank you.

I got this as well on a test computer, is there a script work around or is there an issue with running some scripts on x64 computers (which I am pretty sure is all the ones I look after)

Thanks

EDIT:
ok so I tried a few different things and got the same result

process=subprocess.Popen((‘C:\Windows\System32\defrag.exe /C’),shell=True,stdout=subprocess.PIPE);

‘C:\Windows\System32\defrag.exe’ is not recognized as an internal or external command, operable program or batch file. Do I assume this is a bug then and that this will just not work or should something else be changed in the script?

Hi,

Its a windows 64bit problem if you run the defrag command from within python it expects the defrag.exe file to be in the c:\windows\syswow64 folder for 64bit systems if this script can be updated to automatically copy the defrag.exe from c:\windows\system32 to c:\windows\syswow64 before running it should work or alternatively copy the defrag.exe file to the same folder path as the python executable before running.

Or one could add a conditional statement to the script to check for the file in system32, if it doesn’t exist, it would look in the syswow64, or better yet, since most systems are now 64 bit do it the other way around.

Hi @BOSS

you would think that would be a solution but even if you stipulate the file must be run from c:\windows\system32 it somehow expects it in the c:\windows\syswow64 folder and it is not in their by default even on 64bit systems I think its a Python/windows 64 issue but i decided to use a 3rd party Defrag utility that i run using the (download/install/run python script) in the Script library

Hi all,

We have another Script that can run the Defrag on both 32 and 64 bit.

https://forum.mspconsortium.com/node/4569

Hi @Ilker

Thank you for the update but we now use Auslogics Disk Defrag they have a command line utility cdefrag.exe that works really well we made our own script that downloads and runs it from our own web host. it also gives a bit of detail instead of just pass/fail and does a really good job of an optimized defrag. here is a link to their parameters if anyone is interested http://www.auslogics.com/en/software/disk-defrag/command-line/

sure, beauty is you can install, trigger and control any application with new scripting capability :slight_smile:

I must be doing something wrong, but I just tried running the predefined “Run Defrag on System Disk” procedure on a test endpoint and it didn’t seem to do anything. To run them, am I doing this correctly?

  • it’s as simple as selecting the endpoint.
  • then selecting “Run Procedure”.
  • then enter the characters of the procedure you want to run in order to select the actual procedure you want to run. (select it)
  • then “Run as logged in user” (they’re an administrator).
  • then click on the “Run” button.

It seems fairly straightforward, but when I run the “Run Defrag on System Disk” procedure it seems like it runs. However, there are no changes to the fragmentation on the drives. The procedure logs say start and finish “at” are the same, the status is “FINISHED_SUCCESS”, and there’s no additional info in the “details”.

Is there a certain amount of time I should give it in order for it to start the process or run the defrag?

Hello @mediamoon

Please open the ticket created for the procedure and there should be more information in it.
In order to do that, please access the device -> Procedure logs -> Details of the procedure -> Ticket tab -> open the ticket and the first line marked as “Data” should reveal more information.

The procedure should run directly on the machine.

Thank you for the heads up, @Ethan! Unfortunately, there are no tickets shown in that location (the device -> Procedure logs -> Details of the procedure -> Ticket tab). Is there a way to enable that so that whenever there is a procedure run a ticket is created in the location you specified?

Hello @mediamoon ,

We will contact you on email to troubleshoot this issue.

We appreciate your patience and understanding in this matter.