Jump to content

schtasksgui


spudw2k
 Share

Recommended Posts

#RequireAdmin
#include <GuiListView.au3>
#include <GuiTab.au3>
#include <Array.au3>

Global $arrTabs[1][4]  ;Hostname|Tab#|ListView|WindDir

$gui = GUICreate("SchTasksGUI", @DesktopWidth * .525, @DesktopHeight * .3725, -1, -1, 0x00070000)
    $tabs = GUICtrlCreateTab(2, 0, (@DesktopWidth * .525) - 6, (@DesktopHeight * .3725) - 22)
        GUICtrlSetResizing(-1, 102)
    $blanktab = GUICtrlCreateTabItem("Connect to ...")
   
_NewConnection()

GUIRegisterMsg(0x004E, "WM_NOTIFY")
GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case -3
            _GUIExit()
        Case -7
            If _GUICtrlTab_GetCurSel($tabs) = _GUICtrlTab_GetItemCount($tabs)-1 Then
                _NewConnection()
                _GUICtrlTab_SetCurFocus($tabs,_GUICtrlTab_GetItemCount($tabs)-2)
            EndIf
    EndSwitch
WEnd

Func _AddTasksToGUI($arrTasks, $listTasks) ;Add captured task information and populate Listview in GUI
    If Not IsArray($arrTasks) Then Return 0
    For $i = 0 To UBound($arrTasks) - 1
        $arrTasks[$i] = StringReplace(StringReplace($arrTasks[$i], Chr(34) & "," & Chr(34), "|"), Chr(34), "")
        $arrEntry = StringSplit($arrTasks[$i], "|")
        $arrEntry[6] = "0x" & Hex($arrEntry[6],8)
        For $x = 2 To 27
            If $x = 2 Then
                _GUICtrlListView_AddItem($listTasks, $arrEntry[$x])
            Else
                _GUICtrlListView_AddSubItem($listTasks, $i, $arrEntry[$x], $x - 2)
            EndIf
        Next
    Next
    For $x = 0 To 24
        If $x = 6 Then
            $x = 9
        Else
            _GUICtrlListView_SetColumnWidth($listTasks, $x, $LVSCW_AUTOSIZE)
        EndIf
    Next
EndFunc   ;==>_AddTasksToGUI

Func _FileDisplayProperties($sFile,$job) ;Display File Properties for Highlighted Items
    $objShell = ObjCreate("Shell.Application")
    $objCP = $objShell.Namespace($sFile)
    If Not IsObj($objCP) Then Return 0
   
    For $clsConn In $objCP.Items
        If $clsConn.Name = $job Then
            For $clsVerb in $clsConn.verbs
                If $clsVerb.name = "P&roperties" then $clsVerb.DoIt
            Next
        EndIf
    Next

    $objShell = 0
    $objCP = 0
EndFunc;==>_FileDisplayProperties

Func _GetTasks($strHost = "")  ;Retrieve task information from SCHTASKS.EXE
    $strExe = "schtasks.exe /query /v /fo csv /nh"
    If $strHost <> "" Then $strExe = $strExe & " /s " & $strHost
    $pid = Run($strExe, @WorkingDir, "", 2)
    $stdout = ""
    While 1
        $line = StdoutRead($pid)
        If @error Then ExitLoop
        $stdout = $stdout & StringReplace($line, @CR, "")
    WEnd
    If StringInStr($stdout, "ERROR:") Or StringInStr($stdout, "INFO:") Then Return 0
    $arrData = StringSplit($stdout, @LF)
    Do
        _ArrayDelete($arrData, 0)
    Until StringInStr($arrData[0], Chr(34) & "," & Chr(34))
    _ArrayDelete($arrData, UBound($arrData) - 1)
    Return $arrData
EndFunc   ;==>_GetTasks

Func _GetWindowsDir($strHost = ".")
    $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strHost & "\root\cimv2")
    If Not IsObj($objWMIService) Then Return 0
    $objWMIQuery = $objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem")
    For $objOS in $objWMIQuery
        Return StringReplace($objOS.WindowsDirectory,":","$")
    Next
EndFunc

Func _GUIDblClick($tasklist="",$strHost="",$varWindowsDir="")
    $itemHit = _GUICtrlListView_SubItemHitTest($tasklist)
    If $itemHit[0] > -1 Then
        $strJob = ""
        $job = _GUICtrlListView_GetItemText($tasklist,$itemHit[0],0)
        If $varWindowsDir Then
            $strJob = "\\" & $strHost & "\" & $varWindowsDir & "\Tasks\"
        Else
            $varWindowsDir = _GetWindowsDir($strHost)
            If $varWindowsDir Then $strJob = "\\" & $strHost & "\" & $varWindowsDir & "\Tasks\"
        EndIf
        If $strJob Then _FileDisplayProperties($strJob,$job)
    EndIf
    Return True
EndFunc

Func _GUIExit()
    GUIDelete($gui)
    Exit
EndFunc

Func _NewConnection()
    $strHost = InputBox("Connect to...", "Hostname?", @ComputerName, "", 200, 120)
    $strHost = StringUpper($strHost)
    If Not Ping($strHost) Then
        msgbox(0,"","Could not Ping " & $strHost)
        Return 0
    EndIf
    $tasks = _GetTasks($strHost)
    If $blanktab<>0 Then GUICtrlDelete($blanktab)
    If $arrTabs[0][1] <> "" Then ReDim $arrTabs[UBound($arrTabs)+1][4]
    $arrTabs[UBound($arrTabs)-1][0] = $strHost
    $arrTabs[UBound($arrTabs)-1][1] = GUICtrlCreateTabItem("Current Tasks on " & $strHost)
    $pos = WinGetPos($gui)
    $varWinHeight = $pos[3]
    $varWinWidth = $pos[2]
    $arrTabs[UBound($arrTabs)-1][2] = GUICtrlCreateListView("TaskName|Next Run Time|Status|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|Repeat: Until Duration|Repeat: Stop If Still Running|Idle Time|Power Management", $varWinWidth - ($varWinWidth * .98), $varWinHeight - ($varWinHeight * .98) + ($varWinHeight * .0475), $varWinWidth * .95, ($varWinHeight * .875) - 12,0x0004)
        GUICtrlSetResizing(-1, 102)
        _AddTasksToGUI($tasks, $arrTabs[UBound($arrTabs)-1][2])
    $blanktab = GUICtrlCreateTabItem("Connect to ...")
    $tasks = ""
EndFunc

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam) ;Notify func
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR
    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    For $x = 0 To UBound($arrTabs)-1
        Switch $hWndFrom
            Case GUICtrlGetHandle($arrTabs[$x][2])
                $tasklist = GUICtrlGetHandle($arrTabs[$x][2])
                $strHost = $arrTabs[$x][0]
                $varWindowsDir = $arrTabs[$x][3]
                Switch $iCode
                    Case -3
                        Return _GUIDblClick($tasklist,$strHost,$varWindowsDir)
                    Case -5
                        Return True
                EndSwitch
        EndSwitch
    Next
EndFunc   ;==>WM_NOTIFY

Edited by spudw2k
Link to comment
Share on other sites

  • 6 months later...

Very nice :P

I tried running it on the latest beta and had to include "GUIListView.au3" for it to work

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

  • 3 years later...

C:UsersXXXDesktopSchTaskGUI.au3 (84) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
Until StringInStr($arrData[0], Chr(34) & "," & Chr(34))
Until StringInStr(^ ERROR

Edited by matwachich
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...