简体   繁体   English

如何在 powershell 脚本中创建调度程序任务路径的集合

[英]How to create collection of scheduler task path in powershell script

I have a powershell script where I have to enable or disable task.我有一个 powershell 脚本,我必须在其中启用或禁用任务。 In the script following thing I'm doing在下面的脚本中我正在做的事情

  1. read the $TaskPath = '\Microsoft\Windows\PowerShell\ScheduledJobs'阅读 $TaskPath = '\Microsoft\Windows\PowerShell\ScheduledJobs'
  2. Get-ScheduledTask via $TaskPath通过 $TaskPath 获取 ScheduledTask
  3. Get-ScheduledJob Get-ScheduledJob

Now in my project we have many different schedule task which is present in different path like few present at '\Microsoft\Windows\PowerShell\ScheduledJobs' and some task present at root path like '\printtask'.现在在我的项目中,我们有许多不同的计划任务,它们存在于不同的路径中,例如“\Microsoft\Windows\PowerShell\ScheduledJobs”中的少数任务以及根路径中的某些任务,例如“\printtask”。

Now when I pass the taskname to execute in the script and if that task is not present in the $TaskPath = '\Microsoft\Windows\PowerShell\ScheduledJobs' then it will not execute.现在,当我传递要在脚本中执行的任务名称时,如果 $TaskPath = '\Microsoft\Windows\PowerShell\ScheduledJobs' 中不存在该任务,那么它将不会执行。 To solve this issue I have to add another if condition for setting root path.为了解决这个问题,我必须添加另一个 if 条件来设置根路径。

Lets say in the future if we have new task with some different path then I have to again add new if condition.让我们说将来如果我们有一些不同路径的新任务,那么我必须再次添加新的 if 条件。 To avoid that situation I am looking a way to have collection/list of task paths to check, Then we go through that collection/list and find the scheduled job and execute.为了避免这种情况,我正在寻找一种方法来检查任务路径的集合/列表,然后我们通过该集合/列表 go 并找到计划的作业并执行。

If in the future we need to add new task with other path, we can just add that task path to that collection/list and avoid having additional if statement.如果将来我们需要使用其他路径添加新任务,我们可以将该任务路径添加到该集合/列表中,并避免使用额外的 if 语句。

Anyone has any suggestion how I can archive this..?任何人有任何建议我可以如何存档这个..?

在此处输入图像描述

You can store all the TaskPaths in your initial logic by building an array of objects and add all of your Task Paths in there, somewhat like this:您可以通过构建对象数组并将所有任务路径添加到其中,将所有任务路径存储在初始逻辑中,有点像这样:

$TaskPathList = @()
$TaskPathList += New-Object -TypeName PSObject -Property @{TaskPath = $TaskPath1;}
$TaskPathList += New-Object -TypeName PSObject -Property @{TaskPath = $TaskPath2;}

Later on, you can iterate through this collection of TaskPaths to read them do the decision making accordingly:稍后,您可以遍历此 TaskPath 集合以读取它们并做出相应的决策:

$TaskPathList | ForEach-Object {
    if(<Place your condition to check against $_.TaskPath >)
    {
        #Do your stuff
    }
}

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

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