I do not see why it is not working, then. I assume it is a variable issue, but I may be wrong.
You could try rewriting the script, as I would consider that the easy way of doing it, since you would usually understand your own scripting better. I would do this (defragmenting C: is boring), but I don't have any server to test it on.