Solved

windows array

Posted on 2010-09-10
8
430 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
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

772 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