Avatar of Kylo Ren
Kylo Ren
Flag for United States of America asked on

windows array

i am trying to create  a very simple array.

i would like to run the command schtasks which will display a list of scheduled tasks output is similar to this

TaskName                             Next Run Time            Status
==================================== ======================== ===============
midborots                            7:00:00 PM, 9/10/2010
midborows01                          7:00:00 PM, 9/10/2010
sbs2003                              7:00:00 PM, 9/10/2010

I then need to pass down the results from the taskname column only and store them in a array.  Appreciate your help.

vbscript or python is fine
Shell ScriptingPythonVB Script

Avatar of undefined
Last Comment
RobSampson

8/22/2022 - Mon
Frosty555

Rather than using SCHTASKS, use WMI in vbscript to enumerate the scheduled tasks. You can do anything you like with the data (store it in an array or otherwise) afterwards.

Probably the objJob.Name property, or the objJob.Command property contains that "taskname" data you are looking for.
strComputer = "."
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\"_
    & strComputer & "\root\cimv2")
Set colScheduledJobs = objWMIService.ExecQuery _
    ("Select * from Win32_ScheduledJob")

' Output all of the scheduled tasks on the system

For Each objJob in colScheduledJobs

    Wscript.Echo "Command: " & objJob.Command & VBNewLine _
    & "Days Of Month: " & objJob.DaysOfMonth & VBNewLine _
    & "Days Of Week: " & objJob.DaysOfWeek & VBNewLine _
    & "Description: " & objJob.Description & VBNewLine _
    & "Elapsed Time: " & objJob.ElapsedTime & VBNewLine _
    & "Install Date: " & objJob.InstallDate & VBNewLine _
    & "Interact with Desktop: " _
        & objJob.InteractWithDesktop & VBNewLine _
    & "Job ID: " & objJob.JobId & VBNewLine _
    & "Job Status: " & objJob.JobStatus & VBNewLine _
    & "Name: " & objJob.Name & VBNewLine _
    & "Notify: " & objJob.Notify & VBNewLine _
    & "Owner: " & objJob.Owner & VBNewLine _
    & "Priority: " & objJob.Priority & VBNewLine _
    & "Run Repeatedly: " & objJob.RunRepeatedly & VBNewLine _
    & "Start Time: " & objJob.StartTime & VBNewLine _
    & "Status: " & objJob.Status & VBNewLine _
    & "Time Submitted: " & objJob.TimeSubmitted & VBNewLine _
    & "Until Time: " & objJob.UntilTime
Next

Open in new window

RobSampson

Another way would be to capture the output from SchTasks, and then from each line, take the first 20 or so characters (whatever the fixed with is), and store those....if the WMI doesn't work for you, let me know (but it should, really).

Rob.
Kylo Ren

ASKER
ok i know how get the desired data but how do i store it in a array? thats the part im confused about

the example given by rob is one way to use schtasks output. wmi  is another way to enumerate the scheduled tasks but i need to store the output in array. how do i do that?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
RobSampson

This shortened WMI version should do it...I'll also provide the command line version.

Rob.
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colScheduledJobs = objWMIService.ExecQuery("Select Name from Win32_ScheduledJob")
strNames = ""
For Each objJob in colScheduledJobs
	MsgBox objJob.Name
	If strNames = "" Then
		strNames = objJob.Name
	Else
		strNames = strNames & ";" & objJob.Name
	End If
Next
arrNames = Split(strNames, ";")
For Each strName In arrNames
	MsgBox strName
Next
MsgBox "Done"

Open in new window

RobSampson

Here is the command line parsing version.

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
strTempOutputFile = objFSO.GetFolder(Replace(wscript.ScriptFullName, wscript.ScriptName, "")).ShortPath & "TempSchTasksOutput.txt"
Set objShell = CreateObject("WScript.Shell")
objShell.Run "cmd /c schtasks > " & strTempOutputFile, 0, True
Set objTemp = objFSO.OpenTextFile(strTempOutputFile, intForReading, False)
strNames = ""
While Not objTemp.AtEndOfStream
	strLine = Trim(objTemp.ReadLine)
	If strLine <> "" Then
		If Left(strLine, 8) <> "TaskName" And Left(strLine, 8) <> "========" And Left(strLine, 8) <> "Folder: " And Left(strLine, 8) <> "INFO: Th" Then
			If strNames = "" Then
				strNames = Trim(Left(strLine, 40))
			Else
				strNames = strNames & ";" & Trim(Left(strLine, 40))
			End If
		End If
	End If
Wend
objTemp.Close
objFSO.DeleteFile strTempOutputFile, True
arrNames = Split(strNames, ";")
For Each strName In arrNames
	WScript.Echo strName
Next
WScript.Echo "Done"

Open in new window

Kylo Ren

ASKER
when  i run the script all i get is done.

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
RobSampson

I got the same on Windows 7 with the WMI for some reason....but the command line one worked.

Rob.
ASKER CERTIFIED SOLUTION
RobSampson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.