If you script runs fine within the command line interface but fails when you run it within the task scheduler, it might be because the script does not have enough permissions to run within the task scheduler context.
Make sure that "Run with the highest privileges" is selected as shown below: