Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Accessing Task Manager programmatically using VBScript

912 views
Skip to first unread message

MANILAS...@googlemail.com

unread,
Apr 28, 2007, 6:18:31 PM4/28/07
to
Hi
I would like to access the Task Manager programmatically using
VBScript so I can view processes and end tasks, etc. Is this
possible?
thanks, Manilas

PW

unread,
Apr 29, 2007, 8:24:48 AM4/29/07
to

I thought that task manager was simply a windows interface to the "AT"
command.

Go to a command prompt (start, run, type 'cmd' and press enter) then type
the command "AT" and it will tell you what tasks are scheduled to run.

If you type "AT ?" it will show you all the command parameters.

Therefore, to automate tasks from vbscript, just run a SHELL command to run
the"AT" command.

Good luck!
PW


Bill James

unread,
Apr 29, 2007, 11:02:11 AM4/29/07
to
Sounds like we have apples and oranges here. Task Manager <> Task Scheduler

--

Bill James


"PW" <pw...@SPAMbigpond.net.au> wrote in message news:ukK3nmli...@TK2MSFTNGP06.phx.gbl...

Michael Harris (MVP)

unread,
Apr 29, 2007, 4:55:50 PM4/29/07
to

The 'Task Manager' as a GUI application does not expose a scriptable COM
object model.

Depending on your script language of choice, common practice is to automate
various command line tools or to use WMI to enumerate and manipulate
Win32_Process instances.

--
Michael Harris
Microsoft.MVP.Scripting


D.R.

unread,
Apr 30, 2007, 2:01:40 AM4/30/07
to
Hi Manilas,

WinXP and Win2003 does have the DOS command SCHTASKS. You could try using
this. I have no examples. Maybe the web will.

The AT command should not be used anymore.

As others have found, AT cannot be used to control/update scheduled tasks.

Regards,
Dave.


<MANILAS...@googlemail.com> wrote in message
news:1177798711.5...@u30g2000hsc.googlegroups.com...

Ayush

unread,
Apr 30, 2007, 3:11:42 AM4/30/07
to
[MANILAS...@googlemail.com]s message :

You can use WMI or the command line tools- taskkill and tasklist

Michael Harris (MVP)

unread,
Apr 30, 2007, 6:33:12 PM4/30/07
to
D.R. wrote:
> Hi Manilas,
>
> WinXP and Win2003 does have the DOS command SCHTASKS. You could try
> using this. I have no examples. Maybe the web will.
>

schtasks.exe has nothing to do with the 'Task Manager'


> The AT command should not be used anymore.
>
> As others have found, AT cannot be used to control/update scheduled
> tasks.
> Regards,
> Dave.
>
>
>
>
> <MANILAS...@googlemail.com> wrote in message
> news:1177798711.5...@u30g2000hsc.googlegroups.com...
>> Hi
>> I would like to access the Task Manager programmatically using
>> VBScript so I can view processes and end tasks, etc. Is this
>> possible?
>> thanks, Manilas

--
Michael Harris
Microsoft.MVP.Scripting


D.R.

unread,
May 1, 2007, 3:58:13 PM5/1/07
to
Hi Michael,

But the DOS help, from:
start "hh" /max hh.exe ms-its:C:\WINDOWS\Help\ntcmds.chm::/ntcmds.htm

...on my WinXP SP2 says:

Remarks
a.. SchTasks.exe performs operations similar to those in Scheduled Tasks
in Control Panel. You can use either tool to create, delete, configure, or
display scheduled tasks.
b.. Typing schtasks without any parameters performs a query.
c.. The user must be a member of the Administrators group on the computer
that the command affects.
d.. To verify that a scheduled task ran or to find out why a scheduled
task did not run, see the Task Scheduler service transaction log,
Systemroot\SchedLgU.txt. This log records attempted runs initiated by all
tools that use the service, including Scheduled Tasks and SchTasks.exe.
e.. On rare occasions, task files become corrupted. Corrupted tasks do not
run. When you try to perform an operation on corrupted tasks, SchTasks.exe
displays the following error message:
ERROR: The data is invalid.You cannot recover corrupted tasks. To restore
the task scheduling features of the system, use SchTasks.exe or Scheduled
Tasks to delete the tasks from the system and reschedule them.

f.. SchTasks.exe replaces At.exe, a tool included in previous versions of
Windows.


...and on my machine SCHTASKS (with no options is a default query) lists my
Control Panel Scheduled Tasks.


Do you know of any good Web resources that cover the whole shenanigans?
i.e. the true facts and facets behind the differences between all four
mechanisms:
1) AT
2) SCHTASKS
3) Control Panel - Scheduled Tasks
4) WMI

In my experience they all overlap in some ways, i.e. some tasks from some
mechanisms show up in others, some features present in some mechanisms not
present in others...etc...

Thanks,
Dave.

"Michael Harris (MVP)" <mikhar.at.mvps.dot.org> wrote in message
news:eZelKe3i...@TK2MSFTNGP05.phx.gbl...

Michael Harris (MVP)

unread,
May 1, 2007, 7:44:51 PM5/1/07
to
D.R. wrote:
> Hi Michael,
>
> But the DOS help, from:
> start "hh" /max hh.exe ms-its:C:\WINDOWS\Help\ntcmds.chm::/ntcmds.htm
>
> ...on my WinXP SP2 says:
>
> Remarks
> a.. SchTasks.exe performs operations similar to those in Scheduled
> Tasks in Control Panel. You can use either tool to create, delete,
> configure, or display scheduled tasks.


And how does this relate to the question (below) that the OP actually asked?

D.R.

unread,
May 2, 2007, 2:28:20 AM5/2/07
to
Hi Michael,

Can's SCHTASKS be used to list and end tasks?

C:\Program Files\Support Tools> schtasks /?

SCHTASKS /parameter [arguments]

Description:
Enables an administrator to create, delete, query, change, run and
end scheduled tasks on a local or remote system. Replaces AT.exe.

Parameter List:
/Create Creates a new scheduled task.

/Delete Deletes the scheduled task(s).

/Query Displays all scheduled tasks.

/Change Changes the properties of scheduled task.

/Run Runs the scheduled task immediately.

/End Stops the currently running scheduled task.

/? Displays this help/usage.

Examples:
SCHTASKS
SCHTASKS /?
SCHTASKS /Run /?
SCHTASKS /End /?
SCHTASKS /Create /?
SCHTASKS /Delete /?
SCHTASKS /Query /?
SCHTASKS /Change /?


...by calling DOS from within VBScript with something like?

Function fs_run_command( ps_command )
Const cs_fac = "%fs_run_command, "
Dim ls_output, ls_text, ll_status, lo_file, lo_chan

fs_run_command = "<none>"

ls_text = "Failed to run command `" & ps_command & "`."

On Error Resume Next
ll_status = go_wsh.Run( "%comspec% /c " & ps_command & " >" &
gs_temp_spec, 0, True )
Select Case Err.Number
Case 0
Case Else
Call s_error( cs_fac & ls_text )
End Select
On Error Goto 0

Set lo_file = go_fso.GetFile( gs_temp_spec )
If lo_file.Size = 0 Then Exit Function

Set lo_chan = go_fso.OpenTextFile( gs_temp_spec, ci_for_reading )
ls_output = lo_chan.ReadAll
lo_chan.Close
go_fso.DeleteFile gs_temp_spec

ls_text = ls_text & vbCrlf & vbTab & "Status: " & ll_status & vbCrlf &
vbTab & "Output: " & ls_output
Select Case ll_status
Case 0
Case Else
Call s_error( cs_fac & ls_text )
End Select

fs_run_command = ls_output
End Function


...to run different SCHTASKS commands and then processing (stepping through)
the output of each different command? It looks to me as though the SCHTASKS
command can be used to list, run and end "tasks".

The original posting subject line was "Accessing Task Manager
programmatically using VBScript", and I think I've tried to answer that.

Maybe this "scripting guy" article is more useful:
http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept04/hey0922.mspx

...or for processes:
http://www.computerperformance.co.uk/vbscript/wmi_process.htm

Regards,
Dave.


"Michael Harris (MVP)" <mikhar.at.mvps.dot.org> wrote in message

news:uofG4qE...@TK2MSFTNGP05.phx.gbl...

Reventlov

unread,
May 2, 2007, 10:04:28 AM5/2/07
to
Il giorno 28 Apr 2007 15:18:31 -0700, MANILAS...@googlemail.com ha scritto:
>I would like to access the Task Manager programmatically using
>VBScript so I can view processes and end tasks, etc. Is this
>possible?

Dumb way: use "net start" command to list all active SERVICES to a file.
Set WshShell = CreateObject("WScript.Shell")
WSHShell.Run "%comspec% /c net start >c:\ActiveServices.tmp", 0, True

And then look into this file to find if a service is running

Or (first clever way, from an old Fosco's post) you can start and terminate a task.
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("calc")
WScript.Echo "status" &" >>>"& oExec.Status
oExec.Terminate()

Or (second clever way, from a Tom Lavedas post) use wmi to search for an active process
and try to terminate it.
count = 0
closed = 0
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colThreadList = objWMIService.ExecQuery _
("Select * from Win32_Process where Name
= 'Excel.exe'",, 48)
For Each objThread in colThreadList
with GetObject(,"Excel.Application")
Do while Not .ActiveWorkbook.Saved
resp = MsgBox("This workbook contains unsaved " & _
& "changes." & vbCRLF & "Save before closing?" _
, vbYesNoCancel + vbInformation, _
"Closing Excel")
if resp = vbYes Then
.ActiveWorkbook.Save
Elseif resp = vbNo Then
.ActiveWorkbook.Saved = True
Else
exit do
End if
Loop
if .ActiveWorkbook.Saved Then
.quit
closed = closed + 1
End If
count = count + 1
End with
Next
wsh.echo "Found", count, "instance(s) of Excel.", vbCRLF, _
"Closed", closed, "of these."


--
Giovanni Cenati (Aosta, Italy)
Write to user "Reventlov" and domain at katamail com
http://digilander.libero.it/Cenati (Esempi e programmi in VbScript)
--

Michael Harris (MVP)

unread,
May 2, 2007, 7:34:17 PM5/2/07
to
D.R. wrote:
> Hi Michael,
>
> Can's SCHTASKS be used to list and end tasks?

Not everything that runs on a Windows computer is 'a scheduled task'.

--
Michael Harris
Microsoft.MVP.Scripting


D.R.

unread,
May 2, 2007, 7:42:39 PM5/2/07
to
Rootkits, services, daemons, processes, threads, spinlocks, idlers...


"Michael Harris (MVP)" <mikhar.at.mvps.dot.org> wrote in message

news:%23rXUdJR...@TK2MSFTNGP05.phx.gbl...

0 new messages