Solved

windows array

Posted on 2010-09-10
8
427 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
  • 5
  • 2
8 Comments
 
LVL 31

Expert Comment

by:Frosty555
Comment Utility
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
Comment Utility
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 4

Author Comment

by:Kylo Ren
Comment Utility
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
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
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 4

Author Comment

by:Kylo Ren
Comment Utility
when  i run the script all i get is done.

0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
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 500 total points
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Utilizing an array to gracefully append to a list of EmailAddresses
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 several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now