?
Solved

windows array

Posted on 2010-09-10
8
Medium Priority
?
443 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:Kylo Ren
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 31

Expert Comment

by:Frosty555
ID: 33650626
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

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 33651783
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.
0
 
LVL 5

Author Comment

by:Kylo Ren
ID: 33652036
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?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 65

Expert Comment

by:RobSampson
ID: 33652058
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

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 33652095
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

0
 
LVL 5

Author Comment

by:Kylo Ren
ID: 33652109
when  i run the script all i get is done.

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 33652111
I got the same on Windows 7 with the WMI for some reason....but the command line one worked.

Rob.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 33652435
If you're running Vista or Windows 7, you can put this in a batch file
cscript.exe ShowTasks.vbs

and then right click the batch file, and click Run As Administrator.

The query needs administrative rights.

Regards,

Rob.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses
Course of the Month12 days, 11 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question