简体   繁体   English

Windows Server 2003上的PowerShell中的Get-ScheduledTask

[英]Get-ScheduledTask in PowerShell on Windows Server 2003

I have the below PowerShell code to validate if a scheduled task exists and to check its state. 我具有下面的PowerShell代码,以验证是否存在计划任务并检查其状态。

$SchTsk = Get-ScheduledTask | Select Name, State | ? {$_.Name -eq $SchTask}
If ($SchTsk -ne $Null)
{
  Write-Host "SchTask $SchTask exists"
  If ($SchTsk.State -eq 3)
  {
    Write-Host "SchTask State: READY!"
  }
}

The code works fine on Windows Server 2008 but does not work on Windows Server 2003. In 2003 I get an error: 该代码在Windows Server 2008上可以正常运行,但在Windows Server 2003上则不能正常工作。2003年,我收到一条错误消息:

New-Object : Cannot load COM type Schedule.Service.

From what I've read it seems that the Schedule.Service COM object does not exist on Server 2003. 从我阅读的内容来看,Schedule.Service COM对象似乎在Server 2003上不存在。

So...is there a work-around for this issue to validate a scheduled task and its state on Server 2003? 那么...是否有解决此问题的方法,以验证计划的任务及其在Server 2003上的状态?

I have PowerShell scripts running on Win2008 and Win2003, and found the command "schtasks" to be good enough for looking up information about scheduled tasks. 我在Win2008和Win2003上运行PowerShell脚本,发现命令“ schtasks”足够用于查找有关计划任务的信息。 This isn't a powershell command, but it's a standard function in Windows, and is compatible with Windows 2003 and 2008. 这不是powershell命令,但这是Windows中的标准功能,并且与Windows 2003和2008兼容。

$scheduledTasks = schtasks /query /v /fo csv | ConvertFrom-Csv
#this will return an array of scheduled tasks with all info available for the task

To check if a scheduled task is ready on 2003, you'll need to make sure "Scheduled Task State" is "Enabled", and Status is blank. 要检查计划任务在2003年是否准备就绪,您需要确保“计划任务状态”为“启用”,并且“状态”为空白。

On 2008 and above, Status will return enabled, disabled, running, etc. 在2008年及更高版本上,状态将返回启用,禁用,正在运行等。

The following is a sample PowerShell script that reads from the COM object mentioned above and outputs some Task Schedule Information: 以下是示例PowerShell脚本,该脚本从上述COM对象读取并输出一些任务计划信息:

#Connecting to COM Object 
$schedService = New-Object -ComObject Schedule.Service 
$schedService.Connect($Computer) 

# Get Scheduled Tasks on Root Folder (Task Scheduler Library)
$folder = $SchedService.GetFolder("") 
$tasks = $folder.GetTasks("") 

# Output Task Details
$tasks | % { 
  "-" * 40
  "Task " + $_.Name + ":"
  "-" * 40
  $_.Definition.Actions
}

If all you want to do is gather the basic properties of a task so you know it's name state and next run time you can use schtasks with the following methods: 如果您要做的只是收集任务的基本属性,以便了解其名称状态和下次运行时间,则可以通过以下方法使用schtasks:

function New-TaskInfo()
{
    param ($TaskPath, $TaskName, $NextRunTime, $Status);

    $task = new-object PSObject;

    $task | add-member -type NoteProperty -Name Path-Value $TaskPath;
    $task | add-member -type NoteProperty -Name Name -Value $TaskName;
    $task | add-member -type NoteProperty -Name NextRunTime -Value $NextRunTime;
    $task | add-member -type NoteProperty -Name Status -Value $Status;

    return $task;
}


function Get-ScheduledTaskInfo
{
    $tasks = @();
    $queryOutput = schtasks /QUERY /FO CSV
    foreach($line in $queryOutput) 
    {
        $columns = $line.Split(','); 
        $taskPath = $columns[0].Replace('"', '');
        if($taskPath -eq "TaskName")
        {
            #Ignore headder lines
            continue;
        }
        $taskName = [System.IO.Path]::GetFileName($taskPath);
        $nextRunTime = $columns[1].Replace('"', '');
        $status = $columns[2].Replace('"', '');
        $task = New-TaskInfo -TaskPath $taskPath -TaskName $taskName -NextRunTime $nextRunTime -Status $status;
        Write-Host -ForegroundColor Green "Add Task $task";
        $tasks += $task;
    }
    return $tasks;
}

If you then want to perform an action for a specific task you can use schtasks directly specifying data stored in the objects collected earlier. 如果随后要对特定任务执行操作,则可以使用schtasks直接指定存储在先前收集的对象中的数据。

$task = Get-ScheduledTaskInfo | Where-Object {$_.Name -eq 'TaskName'}
if($task.Status -eq 'Ready')
{
    Write-Host -ForegroundColor Green "Task" $task.Name "Is" $task.Status;

    #End the target task
    schtasks /END /TN $task.Path;
}

You'll find all the informations you need about the missing COM object in Working with scheduled tasks from Windows PowerShell . Windows PowerShell中使用计划的任务中,您将找到有关缺少的COM对象所需的所有信息。

Using Windows Server 2012 you can use Scheduled Tasks Cmdlets in Windows PowerShell . 使用Windows Server 2012,您可以在Windows PowerShell中使用计划任务Cmdlet

@ameer deen.. The code you have given returns the tasks at the root level only. @ameer deen。您提供的代码仅返回根级别的任务。 Windows 2008 onwards several modifications are made to task manager and one of them is folder structure. Windows 2008及更高版本对任务管理器进行了一些修改,其中之一是文件夹结构。 To list all the tasks in side sub folders as well, we need to query the subfolders and iterate through them. 要同时列出侧面子文件夹中的所有任务,我们需要查询子文件夹并对其进行遍历。

You can find the code sample for querying all(including the ones in subfolders) scheduled tasks at http://techibee.com/powershell/powershell-get-scheduled-tasks-list-from-windows-72008-computers/1647 您可以在http://techibee.com/powershell/powershell-get-scheduled-tasks-list-from-windows-72008-computers/1647中找到查询所有(包括子文件夹中的)计划任务的代码示例。

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

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