Developer's Closet A place where I can put my PHP, SQL, Perl, JavaScript, and VBScript code.

6May/100
List Scheduled Tasks from a Windows Server

This script will query a server and output a list of scheduled tasks as a csv (Excel) file. This file is useful when auditing servers.

Note: This script works only for Server 2003, Server 2008, and Windows XP (requires schtasks.exe).


Dim TimeStamp: TimeStamp = Right("0" & Month(now()), 2) & Right("0" & Day(now()), 2) & Right("0" & Year(now()), 2) & Right("0" & Hour(now()), 2) & Right("0" & Minute(now()), 2) & Right("0" & Second(now()), 2)

TmpFile = "C:\Temp\ScheduledTasks\schtasks.txt"
LogFile = "C:\Temp\ScheduledTasks\output_" & TimeStamp & ".csv"

servers = "SERVER-01,SERVER-02,SERVER-03,SERVER-04"
serverArr = Split(servers, ",")

Const ForReading = 1
Const ForWriting = 2

Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

cmdstr = "cmd /c echo >" & TmpFile
retval = objShell.Run(cmdstr, 1, True)

For Each Computer In serverArr
'WScript.Echo Computer

If Computer  "" Then
    cmdstr = "cmd /c schtasks /query /s \\" & Computer & " /v /fo list >>" & TmpFile
Else
    cmdstr = "cmd /c schtasks /query /v /fo list >>" & TmpFile
End If
retval = objShell.Run(cmdstr, 1, True)
Next

Dim objLog: Set objLog = objFSO.CreateTextFile(LogFile)
Set objLog = Nothing
Set objLog = objFSO.OpenTextFile(LogFile, 2)

If objFSO.FileExists(TmpFile) Then
    Dim linecount, objFSO, objFile, ln, strLine

    linecount = 0 
servercount = 0
 
    Set objFile = objFSO.OpenTextFile(TmpFile, ForReading)
    Do Until objFile.AtEndOfStream
        strLine = Trim(objFile.Readline)

        If Left(strLine, 9) = "HostName:" Then
            hostname = Mid(strLine, 39)
            'wscript.echo vbTab & ""
If (servercount = 0) Then 
objLog.Write """HostName"",""TaskName"",""Next Run Time"",""Status"",""Logon Mode"",""Last Run Time"",""Last Result"",""Creator"",""Schedule"",""Task To Run"",""Start In"",""Comment"",""Scheduled Task State"",""Scheduled Type"",""Start Time"",""Start Date"",""End Date"",""Days"",""Months"",""Run As User"",""Delete Task If Not Rescheduled"",""Stop Task If Runs X Hours and X Mins"",""Repeat: Every"",""Repeat: Until: Time"",""Repeat: Until: Duration"",""Repeat: Stop If Still Running"",""Idle Time"",""Power Management"""
End If
objLog.Write vbCrLf
servercount = servercount + 1
     	 objLog.Write """" & hostname & """"
        ElseIf Left(strLine, 9) = "TaskName:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
            'wscript.echo vbTab & vbTab & "" & hostname & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 14) = "Next Run Time:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 7) = "Status:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """" & "," & """" & "Interactive/Background" & """"
     
        ElseIf Left(strLine, 14) = "Last Run Time:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 12) = "Last Result:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 8) = "Creator:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 9) = "Schedule:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 12) = "Task To Run:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 9) = "Start In:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 8) = "Comment:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 21) = "Scheduled Task State:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 15) = "Scheduled Type:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 11) = "Start Time:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 11) = "Start Date:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 9) = "End Date:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 5) = "Days:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 7) = "Months:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 12) = "Run As User:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 31) = "Delete Task If Not Rescheduled:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 37) = "Stop Task If Runs X Hours and X Mins:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 14) = "Repeat: Every:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 20) = "Repeat: Until: Time:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 24) = "Repeat: Until: Duration:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 30) = "Repeat: Stop If Still Running:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 10) = "Idle Time:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 17) = "Power Management:" Then
            'wscript.echo vbTab & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        End If
        linecount = linecount + 1
    Loop
    objFile.Close
    Set objFSO = Nothing
 
Else
    wscript.echo "fail: file not found"
End If

WScript.Echo "End"
18Feb/101
Move Your Mouse Using VBScript and Excel

This script uses the GetMessagePos and SetCursorPos Windows API functions to move your mouse. The script will first get the current position of the mouse by calling GetMessagePos. Next, the script will move the mouse using SetCursorPos. All this is done through an Excel macro. You can call Excel macros through the ExecuteExcel4Macro method. Needless to say, Excel must be installed locally for this script to work. I've found this script very useful for fooling screensavers.

Option Explicit
Dim Excel, GetMessagePos, x, y, Count, Position

Do While Count < 10
	Set Excel = Wscript.CreateObject("Excel.Application")

	GetMessagePos = excel.ExecuteExcel4Macro("CALL(""user32"",""GetMessagePos"",""J"")")

	x = CLng("&H" & Right(Hex(GetMessagePos), 4))
	y = CLng("&H" & Left(Hex(GetMessagePos), (Len(Hex(GetMessagePos)) - 4)))
	If Count MOD 2 = 0 Then
		Position = "- 30"
	Else
		Position = "+ 30"
	End If
	Excel.ExecuteExcel4Macro("CALL(""user32"",""SetCursorPos"",""JJJ""," & x & " " & Position & "," & y & " " & Position & ")")

	WScript.Sleep(100)
	Count = Count + 1

Loop
WScript.Echo "Program Ended"

Reference: http://msdn.microsoft.com/en-us/library/aa191494%28office.10%29.aspx