简体   繁体   English

Powershell计划任务调用者

[英]powershell scheduled task caller

I have several hundred scheduled tasks. 我有几百个预定任务。 Im looking for a way to get the name of the task that called a powershell script when the script has been executed by the task scheduler. 我正在寻找一种方法,以在任务调度程序执行脚本后获取调用Powershell脚本的任务名称。 So for instance I could include that name in an alert I am sending so we know not only which server but which specific task 例如,我可以在发送的警报中包含该名称,这样我们不仅知道哪个服务器,还知道哪个特定任务

You can use the get-scheduledtask cmdlet. 您可以使用get-scheduledtask cmdlet。 You find this cmdlet in the ScheduledTasks module. 您可以在ScheduledTasks模块中找到此cmdlet。

Each returned task has an Actions Property. 每个返回的任务都有一个动作属性。

Eg 例如

(Get-ScheduledTask)[0].Actions

To add on to Peter Schneider helpful answer which will give you details on the first task in the list. 要在Peter Schneider上添加有用的答案,该答案将为您提供列表中第一个任务的详细信息。 So, more specifically, to get them all, try this... 因此,更具体地说,要全部使用,请尝试...

(((Get-ScheduledTask).Actions) | Select-Object *) -match 'powershell' | Select-Object -Property Id,Execute,Arguments | Format-Table -Wrap

Id                 Execute        Arguments
--                 -------        ---------
StartPowerShellJob powershell.exe -NoLogo -NonInteractive -WindowStyle Hidden -Command "Import-Module PSScheduledJob; …

Note, the '*' gest all properties and you may not want to do that, so, just explicitly select the ones you'd want 注意,“ *”代表所有属性,您可能不想这样做,因此,只需显式选择所需的属性即可。

(((Get-ScheduledTask).Actions) | Select-Object -Property Id,Execute,Arguments) -match 'powershell' | Format-Table -Wrap

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

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