Using VIX to Defrag Windows Guests

by bunchc on January 12, 2009

Why? Because it’s awesome, that’s why. Below is an example, complete with syntax and screenshots on how to defragment a windows guest using VMware’s VIX.

Here is what it looked like before:

2009-01-09_1841

Here is the command used from the host where VIX is installed:

C:\Program Files\VMware\VMware VIX>vmrun -T esx -h https://vCenter.server.com/sdk
u domain\user -p pass -gu domain\user -gp pass runProgramInGuest
"[Local Storage] vm/vm.vmx" C:\windows\system32\defrag.exe -f c:

A bit of explanation is needed there. In the vmrun command the “-T esx” specifies we connect to an ESX host, and the “-h http://” gives the URL to the SDK of either the host, or in this case, the vCenter (Virtual Center) that the host belongs to. The next sets of parameters specify a user on the vCenter, or ESX host “-u” & “-p” respectively. As well as a user and password for the guest “-gu” & “-gp”.

Next we specify that we want to run a program inside the guest “runProgramInGuest”, and then which guest to run it on “[Local Storage]… .vmx”

The last, and most important parts, are the actual command "C:\windows\system32\defrag.exe –f c:” I’ve found that it’s important to specify the entire path to the command you are accessing, as it seems VIX does not have visibility into your path variable.

So, now that we’ve done all that, what does our disk look like afterwards:

2009-01-09_1926

  • Tom

    How is this any better than using Task Scheduler to do the same thing??

  • http://professionalvmware.com professionalvmware

    Just another way to do the same thing. However, this can be managed from a central location and applied out to hundreds of thousands of VM's, rather than needing to be setup on each individual VM.

  • Tom

    How is this any better than using Task Scheduler to do the same thing??

  • http://professionalvmware.com professionalvmware

    Just another way to do the same thing. However, this can be managed from a central location and applied out to hundreds of thousands of VM's, rather than needing to be setup on each individual VM.

  • john191

    Really great information in your blog. Please write more so that we can get more updates in your blog. Thanks a lot!
    regards
    sears parts

  • http://professionalvmware.com professionalvmware

    Anything specifc you would like to see?

  • Albert

    OK, so the idea is to have this script on the mgmt Server and then just customized for each new VM ?

  • http://professionalvmware.com professionalvmware

    So, this script can be run from anywhere you have VIX installed. Be that a central mgmt server, or your mgmt workstation, etc. It can then be further customized to handle one or more VMs as you see fit.

  • Albert

    OK, so the idea is to have this script on the mgmt Server and then just customized for each new VM ?

  • http://professionalvmware.com professionalvmware

    So, this script can be run from anywhere you have VIX installed. Be that a central mgmt server, or your mgmt workstation, etc. It can then be further customized to handle one or more VMs as you see fit.

  • http://www.3f3.com/ العاب فلاش

    Thanks for this method, this looks easy and efficient way, I'm gonna try this ASAP.

Previous post:

Next post: