如何使用 PowerShell 清点计划任务 [英] how to use PowerShell to inventory Scheduled Tasks

查看:41
本文介绍了如何使用 PowerShell 清点计划任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人拥有使用 PowerShell 清点服务器上的计划任务(包括操作)的链接或脚本?

Does anyone have a link or script that uses PowerShell to inventory the Scheduled Tasks on a server, including the Action?

我能够获取计划服务 com 对象以及我称之为顶级"属性(名称、状态、上次运行时间),但还想从计划任务的操作"部分获取信息(本质上是,计划任务的名称及其命令行).

I am able to get the Scheduled Service com object and what I would call "top level" properties (name, state, lastruntime), but would like to also get information from the "Actions" part of the Schedule Tasks (essentially, the name of Scheduled Task and its commandline).

例如:

$schedule = new-object -com("Schedule.Service") 
$schedule.connect() 
$tasks = $schedule.getfolder("\").gettasks(0)

$tasks | select Name, LastRunTime

foreach ($t in $tasks)
{
foreach ($a in $t.Actions)
{
    $a.Path
}
}

上面的代码片段用于列出任务;但是 Actions 上的循环似乎根本没有做任何事情,没有错误,也没有任何输出.

The above snippet of code works in terms of listing the tasks; but the loop on the Actions simply does not seem to do anything, no error, no output whatsoever.

任何帮助将不胜感激.

推荐答案

这可能与当前的答案非常相似,但我写了一个快速脚本来帮助您前进.您当前脚本的问题在于任务中没有 Actions 属性.您需要从 comobject 提供的 xml 任务定义中提取它.以下脚本将返回一组对象,每个计划任务一个.它包括操作如果操作是运行一个或多个命令.这只是为了让您继续前进,因此您需要修改它以包含更多需要的属性.

This is probably very similar to current answers, but I wrote a quick script to get you going. The problem with your current script is that there is no Actions property in a task. You need to extract it from the xml task-definition that the comobject provides. The following script will return an array of objects, one per scheduled task. It includes the action if the action is to run one or more command. It's just to get you going, so you need to modify it to include more of the properties if you need them.

function getTasks($path) {
    $out = @()

    # Get root tasks
    $schedule.GetFolder($path).GetTasks(0) | % {
        $xml = [xml]$_.xml
        $out += New-Object psobject -Property @{
            "Name" = $_.Name
            "Path" = $_.Path
            "LastRunTime" = $_.LastRunTime
            "NextRunTime" = $_.NextRunTime
            "Actions" = ($xml.Task.Actions.Exec | % { "$($_.Command) $($_.Arguments)" }) -join "`n"
        }
    }

    # Get tasks from subfolders
    $schedule.GetFolder($path).GetFolders(0) | % {
        $out += getTasks($_.Path)
    }

    #Output
    $out
}

$tasks = @()

$schedule = New-Object -ComObject "Schedule.Service"
$schedule.Connect() 

# Start inventory
$tasks += getTasks("\")

# Close com
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($schedule) | Out-Null
Remove-Variable schedule

# Output all tasks
$tasks

例如.输出

PS > .\Untitled1.ps1 | ? { $_.Name -eq "test" }


Actions     : notepad.exe c:\test.txt
              calc.exe 
Path        : \test
Name        : test
LastRunTime : 30.12.1899 00:00:00
NextRunTime : 17.03.2013 13:36:38

这篇关于如何使用 PowerShell 清点计划任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆