Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
Thank you very much. I'll give this a shot and let you know how if I have any issues. –  p.s.w.g Feb 7 at 3:21

Why not PowerShell? WMIC is pretty cool but I can't help but feel that PowerShell is now the preferable solution for this kind of thing:

Invoke-Command -ComputerName $Server -ScriptBlock { Disable-ScheduledJob -Name $Task }
share|improve this answer
    
I can't seem to get the cmdlets to work. I'm guessing I need to enable some feature on the server? The reason I prefere WMIC is that this is an extension of my other question, where I describe how I have found PsExec unreliable. –  p.s.w.g Feb 7 at 0:11
    
You need to Enable-PSRemoting on your servers. You can do this on a one-by-one basis or with a GPO. –  kce Feb 7 at 0:15
    
On closer inspection it looks like the version of powershell installed on our servers is 2.0 and PSScheduledJob was introduced in 3.0. I'll have to talk to our infrastructure team about upgrading, but that may take some time. –  p.s.w.g Feb 7 at 0:16
    
Hmm. Bummer. PowerShell 3.0 has vastly expanded functionality. Try this link for rolling your own PS2.0 versions of PSScheduledJob. –  kce Feb 7 at 0:21
    
Actually that link is all about wrapping schtasks. I didn't think could be used remotely, but it turns out it can, and it seems to work pretty well for my needs right now. I'll see what we can do about getting PS installed tomorrow. Any thoughts on the other part of my question? Will schtasks /End wait until the task completes or will it kill the task immediately? –  p.s.w.g Feb 7 at 0:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.