1

i have one task scheduler with 2 .bat file on action.

List .bat file on action tab

when task scheduler running how to i know which one .bat is running ?

this is xml file task scheduler:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2020-07-21T08:40:20.4842672</Date>
    <Author>WIN-9DQ9EVI3R22\Administrator</Author>
    <URI>\RESTORE</URI>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <Repetition>
        <Interval>PT5M</Interval>
        <StopAtDurationEnd>false</StopAtDurationEnd>
      </Repetition>
      <StartBoundary>2020-07-21T00:00:00</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>S-1-5-21-316211975-241055689-3246940587-500</UserId>
      <LogonType>Password</LogonType>
      <RunLevel>LeastPrivilege</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>"D:\Batch File\LS_RESTORE_221.bat"</Command>
    </Exec>
    <Exec>
      <Command>"D:\Batch File\LS_RESTORE_MAINDB.bat"</Command>
    </Exec>
  </Actions>
</Task>

see at the xml file i have 2 .bat on action tag

this is .bat file script

sqlcmd -Q "exec SP_LS_RESTORE" -S WIN-9DQ9EVI3R22 -d dbSaaS_HOST_221 -o "D:\Batch File\LS_Restore_out_221.txt"
TylerH
  • 20,799
  • 66
  • 75
  • 101
febry
  • 27
  • 7

1 Answers1

0

You can give a try for this batch file :

@echo off
Color 0B & Title Showing No Microsoft Scheduled Tasks List by Hackoo 2020
Set "TmpFile=%~n0_Abs_cmdline.txt"
Set "LogFile=%~n0_cmdline.txt
If Exist "%TmpFile%" Del "%TmpFile%"
If Exist "%LogFile%" Del "%LogFile%"
Set ProcessNames="cmd.exe" "wscript.exe" "cscript.exe"
SetLocal EnableDelayedExpansion
for %%A in (%ProcessNames%) Do (
    Call :GetCommandLine %%A ProcessCmd
    If defined ProcessCmd ( 
        echo !ProcessCmd!>>"%TmpFile%"
    )
)
If Exist "%TmpFile%" Call :Extract "%TmpFile%" "%LogFile%"
If Exist "%LogFile%" Start "" "%LogFile%"

echo(
echo( ****************************************************************************************************
echo(                                No Microsoft Scheduled Tasks List
echo( ****************************************************************************************************
Set "EXT=BAT"
@For /F "tokens=2,9 delims=," %%a in (
'SCHTASKS /Query /NH /FO CSV /V ^|find /I /V "Microsoft" ^|find /I /V "ADOBE" ^|findstr /I /C:"%EXT%"'
) do (
    REM Set TaskName=%%~a
    Set TaskPath=%%~b
    REM Call :Trim_Dequote !TaskName! TaskName
    Call :Trim_Dequote !TaskPath! TaskPath
    REM echo "!TaskName!"
    echo( "!TaskPath!"
)
Pause & exit
::-----------------------------------------------------------------------------------
:Trim_Dequote <Var> <NewVar>
Set "VbsFile=%Tmp%\%~n0.vbs"
(
    echo    Wscript.echo Trim_Dequote("%~1"^)
    echo    Function Trim_Dequote(S^)
    echo    If Left(S, 1^) = """" And Right(S, 1^) = """" Then Trim_Dequote = Trim(Mid(S, 2, Len(S^) - 2^)^) Else Trim_Dequote = Trim(S^)
    echo    End Function
)>"%VbsFile%"
for /f "delims=" %%a in ('Cscript //nologo "%VbsFile%"') do (
    set "%2=%%a"
)
Del "%VbsFile%" /F >nul 2>&1
exit /b
::---------------------------------------------------------------------------------------------------------------
:GetCommandLine <ProcessName> <ProcessCmd>
Set "ProcessCmd="
for /f "tokens=2 delims==" %%P in (
'2^>nul wmic process where caption^="%~1" get commandline /format:list ^| findstr /I "%~1" ^| find /I /V "%~nx0"'
) do (
    if not defined %2 Set "%2=%%P"
)
Exit /b
::---------------------------------------------------------------------------------------------------------------
:Extract <InputData> <OutPutData>
(
echo Data = WScript.StdIn.ReadAll
echo Data = Extract(Data,"(^?^!.*(\x22\w\W^)^).*(\.ps1^|\.vbs^|\.vbe^|\.js^|\.jse^|\.cmd^|\.bat^|\.wsf^|\.exe^)(^?^!.*(\x22\w\W^)^)"^)
echo WScript.StdOut.WriteLine Data
echo Function Extract(Data,Pattern^)
echo    Dim oRE,oMatches,Match,Line
echo    set oRE = New RegExp
echo    oRE.IgnoreCase = True
echo    oRE.Global = True
echo    oRE.Pattern = Pattern
echo    set oMatches = oRE.Execute(Data^)
echo    If not isEmpty(oMatches^) then
echo        For Each Match in oMatches  
echo            Line = Line ^& Trim(Match.Value^) ^& vbcrlf
echo        Next
echo        Extract = Line
echo    End if
echo End Function
)>"%tmp%\%~n0.vbs"
cscript /nologo "%tmp%\%~n0.vbs" < "%~1" > "%~2"
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
exit /b
::---------------------------------------------------------------------------------------------------------------

EDIT on 23/08/2020 @16:45 :

This is a Hybrid code Batch and Powershell that help us to show All No Microsoft Scheduled Tasks.

Show_No-Microsoft_Tasks.bat


<# : Batch portion
@rem # The previous line does nothing in Batch, but begins a multiline comment block
@rem # in PowerShell.  This allows a single script to be executed by both interpreters.
@echo off
Title Get Schedule Tasks with a Hybrid code Batch and Powershell Script by Hackoo 2020
echo(
rem # This a Powershell command executes the hybrid portion at the bottom of this script
rem @for /f "delims=" %%I in ('powershell -noprofile "iex (${%~f0}|out-string)"') do echo %%I
>"%~dpn0.txt"  (
    @for /f "delims=" %%I in ('powershell -noprofile "iex (${%~f0}|out-string)"') do echo %%I
)
REM TimeOut /T 3 /NoBreak>nul
If Exist "%~dpn0.txt" Start "" "%~dpn0.txt" & exit /b
rem # End multi-line PowerShell comment block.  Begin PowerShell scripting.
: end Batch / begin PowerShell hybrid code #>
Function getTasks($path) {
    $out = @()
    # Get root tasks
    $schedule.GetFolder($path).GetTasks(0) | % {
        $xml = [xml]$_.xml
        $out += New-Object psobject -Property @{
            "Name" = $_.Name
            "Path" = $_.Path
            "LastRunTime" = $_.LastRunTime
            "NextRunTime" = $_.NextRunTime
            "Actions" = ($xml.Task.Actions.Exec | % { "$($_.Command) $($_.Arguments)" }) -join "`n"     
        }
    }
    # Get tasks from subfolders
    $schedule.GetFolder($path).GetFolders(0) | % {
        $out += getTasks($_.Path)
    }
    #Output
    $out
}
$tasks = @()
$schedule = New-Object -ComObject "Schedule.Service"
$schedule.Connect() 
# Start inventory
$tasks += getTasks("\")
# Close com
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($schedule) | Out-Null
Remove-Variable schedule

# To show All No Microsoft Scheduled Tasks
$tasks | ? { $_.Path -notmatch "Micro*" }
Read-Host 'Type any key to continue'
$tasks | ? { $_.Path -notmatch "Micro*" } | Out-GridView
Read-Host 'Type any key to continue'

<#
# Output all tasks
#$tasks | Out-GridView
#Read-Host 'Type any key to continue'
# To show only tasks with those extensions in their TaskPath
#$tasks | ? { $_.Actions -match "(\.vbs|\.vbe|\.cmd|\.bat|\.hta)" } | Out-GridView
#Read-Host 'Type any key to continue'
#>
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • hi thanks for advice, i need confirmation, when task scheduler running .bat file and i running your .bat file. i got 2 file with .txt format. first i got with text `C:\Windows\SYSTEM32\cmd.exe /c ""D:\Batch File\LS_RESTORE_MAINDB.bat"` second i got with text `D:\Batch File\LS_RESTORE_MAINDB.bat` this is mean on my windows server currently running LS_RESTORE_MAINDB.bat ? – febry Jul 23 '20 at 02:32
  • @febry Yes The first text file, give you the whole command line and the second file ,give you the path of your batch file that is running and it is extracted from the first one ! – Hackoo Jul 23 '20 at 07:21
  • ok thanks @hackoo, i already understood about that. – febry Jul 23 '20 at 07:29