如何在任务计划程序上查看哪个 .bat 文件 运行

How To See Which One .bat file running on task scheduler

我有一个任务调度程序,其中包含 2 个 .bat 文件。

List .bat file on action tab

当任务调度程序 运行 我如何知道哪个 .bat 是 运行?

这是 xml 文件任务计划程序:

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

查看 xml 文件,我在操作标签上有 2 个 .bat

这是.bat文件脚本

sqlcmd -Q "exec SP_LS_RESTORE" -S WIN-9DQ9EVI3R22 -d dbSaaS_HOST_221 -o "D:\Batch File\LS_Restore_out_221.txt"

您可以试试这个批处理文件:

@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
::---------------------------------------------------------------------------------------------------------------

2020 年 8 月 23 日 @16:45 编辑:

这是一个混合代码批处理和 Powershell,可帮助我们显示所有无 Microsoft 计划任务。

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'
#>