Jump to content

Check If computer is Online - If Online Run a Scheduled Task


Recommended Posts

Hi,

So I am creating another project hereĀ šŸ˜µĀ this would be my I guess 4th / 5th AutoIT project and I am trying to get better at this, and still learning.Ā 

The below script I am creating does 2 things, from the list of computers, If we select the checkbox shows whether the PC isĀ online & if that PC isĀ online, run a pre-configured scheduled task.

And this script works like 90% everything is functional. the area i am getting stuck with is theĀ TaskSchedulerOnLocalComputer()

Basically I having problems running the task on local computer, even though I have full admin access on my PC and I can right click & Start the Task from Task Scheduler, when i run the script, for every other computer i get SUCCESS: Attempted to run the scheduled task "GoogleUpdateTaskMachineCore". and it works but when I run the same on my local PC (by selecting it from the list) I get Access Denied :(

So I tried to use RunAs but that doesn't display any window or anything and also doesn't run the task, So if someone can help me get this working for local computer that would be great.

Thanks

;Script to Push the installation of MatrixCodeBase
;Reference: https://docs.microsoft.com/en-us/windows/desktop/taskschd/schtasks

#include <AutoItConstants.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <GUIConstantsEx.au3>
#include <GUIConstants.au3>

SelectComputer()

Func CheckIfComputerIsOnline()
If RunWait("ping.exe -n 1 " & $ComputerName, "", @SW_HIDE) == 0 Then
    MsgBox(0, "", $ComputerName & " is Online")
    RunScheduledTask($ComputerName)
Else
    MsgBox(0, "", $ComputerName & " is Offline")
    Send("{SPACE}")
EndIf
EndFunc

Func SelectComputer()
   Local $hGUI = GUICreate("Select Computer", 300, 300)
   Opt("GUICoordMode", 0) ;1=absolute, 0=relative, 2=cell
   Local $idCheckbox1 = GUICtrlCreateCheckbox("Computer1", 10, 10)
   Local $idCheckbox2 = GUICtrlCreateCheckbox("Computer2", -1, 25)
   Local $idCheckbox3 = GUICtrlCreateCheckbox("Computer3", -1, 25)
   Local $idCheckbox4 = GUICtrlCreateCheckbox("Computer4", -1, 25)
   Local $idCheckbox5 = GUICtrlCreateCheckbox("Computer5", -1, 25)
   Local $idCheckbox6 = GUICtrlCreateCheckbox("Computer6", -1, 25)
   Local $idCheckbox7 = GUICtrlCreateCheckbox("Computer7", -1, 25)
   Local $idCheckbox8 = GUICtrlCreateCheckbox("Computer8", -1, 25)
   Local $idCheckbox9 = GUICtrlCreateCheckbox("Computer9", -1, 25)
   Local $idCheckbox10 = GUICtrlCreateCheckbox("Computer10", -1, 25)
   Local $idButton_Insert = GUICtrlCreateButton("Done", 150, 10)
   Local $idButton_Close = GUICtrlCreateButton("Close", 50, 0)
   GUISetState(@SW_SHOW, $hGUI)

   While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idButton_Close
                ExitLoop

            Case $idCheckbox1
               Global $ComputerName = "Computer1"
                If _IsChecked($idCheckbox1) Then
                    CheckIfComputerIsOnline()
                 Else
                    _IsUnchecked($idCheckbox1)
                    EndIf
            Case $idCheckbox2
               Global $ComputerName = "Computer2"
                If _IsChecked($idCheckbox2) Then
                    CheckIfComputerIsOnline()
                 Else
                    _IsUnchecked($idCheckbox2)
                    EndIf
           Case $idCheckbox3
               Global $ComputerName = "Computer3"
                If _IsChecked($idCheckbox3) Then
                    CheckIfComputerIsOnline()
                 Else
                    _IsUnchecked($idCheckbox3)
                    EndIf
            Case $idCheckbox4
               Global $ComputerName = "Computer4"
                If _IsChecked($idCheckbox4) Then
                    CheckIfComputerIsOnline()
                 Else
                    _IsUnchecked($idCheckbox4)
                    EndIf
            Case $idCheckbox5
               Global $ComputerName = "Computer5"
                If _IsChecked($idCheckbox5) Then
                    CheckIfComputerIsOnline()
                 Else
                    _IsUnchecked($idCheckbox5)
                    EndIf
            Case $idCheckbox6
               Global $ComputerName = "Computer6"
                If _IsChecked($idCheckbox6) Then
                    CheckIfComputerIsOnline()
                 Else
                    _IsUnchecked($idCheckbox6)
                    EndIf
            Case $idCheckbox7
               Global $ComputerName = "Computer7"
                If _IsChecked($idCheckbox7) Then
                    CheckIfComputerIsOnline()
                 Else
                    _IsUnchecked($idCheckbox7)
                    EndIf
            Case $idCheckbox8
               Global $ComputerName = "Computer8"
                If _IsChecked($idCheckbox8) Then
                    CheckIfComputerIsOnline()
                 Else
                    _IsUnchecked($idCheckbox8)
                    EndIf
            Case $idCheckbox9
               Global $ComputerName = "Computer9"
                If _IsChecked($idCheckbox9) Then
                    CheckIfComputerIsOnline()
                 Else
                    _IsUnchecked($idCheckbox9)
                    EndIf
            Case $idCheckbox10
               Global $ComputerName = "Computer10"
                If _IsChecked($idCheckbox10) Then
                    CheckIfComputerIsOnline()
                 Else
                    _IsUnchecked($idCheckbox10)
                    EndIf
        EndSwitch
    WEnd
    GUIDelete($hGUI)
 EndFunc


Func _IsChecked($idControlID)
    Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED
 EndFunc

 Func _IsUnchecked($idControlID)
    Return BitAND(GUICtrlRead($idControlID), $GUI_UNCHECKED) = $GUI_UNCHECKED
 EndFunc

 Func RunScheduledTask($ComputerName)

    If @ComputerName = $ComputerName Then
       TaskSchedulerOnLocalComputer()
    Else
       TaskSchedulerOnRemoteComputer()
    EndIf
 EndFunc

Func TaskSchedulerOnLocalComputer()
   $sUser = "username"
   $sPass = "password"
   $sDomain = "domain"
   $LocalSCHTASKS = "schtasks /Run /TN GoogleUpdateTaskMachineCore"
   RunAs($sUser,$sDomain, $sPass,0, @ComSpec & '" /k ' & $LocalSCHTASKS, @SystemDir)
   Sleep(3000)
   Send("Exit")
   Send ("{Enter}")
EndFunc


Func TaskSchedulerOnRemoteComputer()
   $RemoteSCHTASKS = "schtasks /U username /p password /Run /TN GoogleUpdateTaskMachineCore /S " & $ComputerName
   RunWait('"' & @ComSpec & '" /k ' & $RemoteSCHTASKS, @SystemDir)
   Sleep(3000)
   Send("Exit")
   Send ("{Enter}")
EndFunc

Ā 

Link to comment
Share on other sites

Sorry @DeyeĀ I didnot understand that, Task is already there and created, and it also runs when the user executes it. it doesn't work through SCHTASKS.EXE though, but when i run cmd as admin and then run SCHTASKS it works fine. so I need AutoIT to run elevated cmd which its not doing for me as it doesn't show me the cmd window when using RunAs

Link to comment
Share on other sites

No there noĀ schedule point, the task should will run when triggered manually, irrespective of when the automatic triggered are configured.

Let me explain.

I have been working on this project for my client, who runs a dev project which has some office addins to be deployed to local computers.

I with the help of some forum members here created an autoit script that checks if a folder contains an msi file, checks its version, if its newer than the installed version it will upgrade, if its older it will upgrade, if its the same it will exit.Ā 

This will be added to each computers as a scheduled task to run atleast once per day and triggered manually as well.

The script i am creating above is to call that scheduled task manually for the dev team to force push the installs on computers immediately instead of once per day. e.g. lets say users are facing a problem and cannot work in Office Apps due to the Addin, dev team can resolve the issue & force push the package.

In the script i posted in the first page, I am just using the default task that i know exists on all computers that has Google Chrome installed so basically this can be used on any computer, and the script just works fine for all of the 9/10 computers in that list except for the local computer.Ā 
Regardless of what that computer is the script just doesn't want to run on a local pc.

e.g. if I call the script from Computer1,Ā  to check if Computer2 is online - it runs, says "Computer2 is online" and then opens CMD prompt and starts the task. I can confirm that it works because i can login on Computer2 and see that the task was triggered in its history. But If I call Computer1, it says Computer1 is online but doesn't run the script even when using RunAs() administrator.

Edited by AasimPathan
Link to comment
Share on other sites

I've made the following change in the script which seems to have done the trick, but I would still like to see if we can get this to work without requiring admin permissions, rest of the script remains the same.

#RequireAdmin

Func TaskSchedulerOnLocalComputer()
   $LocalSCHTASKS = "schtasks /Run /TN GoogleUpdateTaskMachineCore"
   RunWait('"' & @ComSpec & '" /k ' & $LocalSCHTASKS, @SystemDir)
   Sleep(3000)
   WinClose("Administrator: C:\WINDOWS\system32\cmd.exe")
EndFunc

Ā 

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