簡體   English   中英

是否可以從批處理文件中獲取運行該批處理文件的任務計划程序任務的名稱?

[英]Is it possible, from within a batch file, to get the name of the task scheduler task running the batch file?

在Windows 10環境中,如果我無權執行執行bat文件的任務計划程序,我(從批處理文件中)可以知道啟動了批處理文件的任務的名稱嗎? 是否有可以使用的任務計划程序任務名稱設置的變量?

顯而易見的解決方案是在任務計划程序中調用操作時添加任務名稱參數,但是如果我無權訪問任務計划程序怎么辦?

希望此解決方案對您有用。

在下面您可以找到:

.BAT文件和包含該功能的.VBS。

至於.BAT:

 @ECHO OFF SET DT=%DATE% %TIME% SET DATETIME=%DT:~0,19% FOR /F "usebackq tokens=*" %%r in (`CSCRIPT "C:\\<your_folder>\\current_task_name.vbs" "%~dpnx0" "%DATETIME%"`) DO SET current_task_name=%%r ECHO script was executed by: %current_task_name% PAUSE 

至於命名為VBS的: current_task_name.vbs

 getMyTask WScript.Arguments.Item(0), WScript.Arguments.Item(1) Function getMyTask(Cmd, StartDateTime) ' ---------------------------------------------------------------------- ' Script to list all Scheduled tasks and their available properties ' Written for TaskSchedule 2.0 ' ---------------------------------------------------------------------- Dim objTaskService, objTaskFolder, colTasks Dim objTask ' Create the TaskobjTaskService object and connect Set objTaskService = CreateObject("Schedule.Service") Call objTaskService.Connect ' Get the task folder that contains the tasks. Set objTaskFolder = objTaskService.GetFolder("\\") ' Get all of the tasks (Enumeration of 0 Shows all including Hidden. 1 will not show hidden) Set colTasks = objTaskFolder.GetTasks(0) If colTasks.Count = 0 Then wscript.echo "No registered tasks." Else For Each objTask In colTasks With objTask 'check if the time of execution is in the same range of the current task (~ 2 secs. after) If DateDiff("s", .LastRunTime, StartDateTime) <= 2 Then Set objTaskDefinition = .Definition With objTaskDefinition Set colActions = objTaskDefinition.Actions For Each objTaskAction In colActions With objTaskAction Select Case .Type Case 0 '= Execute / Command Line Operation 'If .Path of TaskAction object is equal to the batch file name provided 'it returns its task name back If .Path = Cmd Then wscript.echo objTask.Name wscript.quit End If End Select End With Next End With End If End With Next End If End Function 

問候,jtandrea

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM