Stopping a scheduled task with WMIC

automationbatch-filescheduled-taskwindows-server-2008-r2wmi

I need to find some method for disabling a scheduled task on a remote machine and waiting until that task has stopped executing if it happens to be running at the moment. I'd like to do this through WMIC if possible. This will be integrated into a larger batch process.

This is as far as I've gotten:

> wmic /node:%SERVER_NAME% /namespace:\\root\cimv2 path Win32_ScheduledJob

But this just returns:

No Instance(s) Available.

So I'm not sure if I'm doing anything wrong or if maybe I need to configure something on the server.

Best Answer

It seems that WMIC only supports jobs created with WMIC itself or created with the AT command (source - starting page 205).

That's certainly the reason why you get No Instance(s) Available.

I am also a Powershell addict, but if it is an issue you could use schtasks utility (as you mentioned).

To remotely disable a task :

schtasks /change /disable /TN "task name" /S server_fqdn /U domain\user /P password

The advantage of disabling a task is that you are sure that it will continue running until it finishes, in case it was running (only future executions are disabled in that case).

If you want to wait until the task finishes you will have to wait for the process to finish. To avoid the use of another third party tool/command you can do this with schtasks also, by querying the task status :

schtasks /query /TN "task name" /S server_fqdn /U domain\user /P password | finstr Running

(ps : note that i am not sure about the "running" status because my Windows OS is French. Check this according to your language).

schtasks /END will kill the task immediately.