简体   繁体   English

是否可以从批处理文件中获取运行该批处理文件的任务计划程序任务的名称?

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

In a Windows 10 environment, if I don't have access to the task scheduler that is executing my bat file, can I (from within the batch file) know the name of the task that has launched the batch file? 在Windows 10环境中,如果我无权执行执行bat文件的任务计划程序,我(从批处理文件中)可以知道启动了批处理文件的任务的名称吗? Is there a variable that gets set with the task scheduler task name that I can use? 是否有可以使用的任务计划程序任务名称设置的变量?

The obvious solution would be to add a task name parameter when calling the action in the task scheduler, but what if I have no access to the task scheduler? 显而易见的解决方案是在任务计划程序中调用操作时添加任务名称参数,但是如果我无权访问任务计划程序怎么办?

I hope this solution works for you. 希望此解决方案对您有用。

Below you can find: 在下面您可以找到:

a .BAT file and a .VBS that contains the function. .BAT文件和包含该功能的.VBS。

as for the .BAT: 至于.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 

as for the VBS named: current_task_name.vbs 至于命名为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 

regards, jtandrea 问候,jtandrea

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM