3

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.

p.s.w.g
  • 185
  • 2
  • 9

2 Answers2

2

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 }
  • 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](http://serverfault.com/questions/570805/psexec-hangs-intermittently-when-calling-appcmd), where I describe how I have found PsExec unreliable. – p.s.w.g Feb 07 '14 at 00:11
  • You need to [Enable-PSRemoting](http://technet.microsoft.com/en-us/library/hh849694.aspx) on your servers. You can do this on a one-by-one basis or with a GPO. –  Feb 07 '14 at 00: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 07 '14 at 00:16
  • Hmm. Bummer. PowerShell 3.0 has vastly expanded functionality. Try this [link](http://www.peetersonline.nl/2009/06/managing-scheduled-tasks-remotely-using-powershell/) for rolling your own PS2.0 versions of `PSScheduledJob`. –  Feb 07 '14 at 00: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 07 '14 at 00:33
  • `schtasks /End` will kill the task immediately. – krisFR Feb 07 '14 at 02:25
2

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.

krisFR
  • 12,830
  • 3
  • 31
  • 40