Mar*_*een 10 powershell scheduled-tasks
有没有人有一个链接或脚本使用PowerShell来清点服务器上的计划任务,包括Action?
我能够获得Scheduled Service com对象和我称之为"顶级"属性(name,state,lastruntime),但是也希望从Schedule Tasks的"Actions"部分获取信息(实质上是名称)计划任务及其命令行).
例如:
$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
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码片段用于列出任务; 但是,动作上的循环似乎没有做任何事情,没有错误,没有任何输出.
任何帮助,将不胜感激.
Fro*_* F. 13
这可能与目前的答案非常相似,但我写了一个快速的脚本来帮助你.当前脚本的问题是Actions任务中没有属性.您需要从comobject提供的xml任务定义中提取它.以下脚本将返回一个对象数组,每个计划任务一个.如果操作要运行一个或多个命令,则包括操作.这只是为了让你前进,所以你需要修改它以包含更多的属性,如果你需要它们.
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
Run Code Online (Sandbox Code Playgroud)
防爆.输出
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
Run Code Online (Sandbox Code Playgroud)