I work in a Windows environment where Scheduled Tasks have never been documented or kept up with, leaving us with about 150 servers that have a random smattering of tasks. I am attempting to construct something that will query specific information about these tasks and output them in a pretty format that I can sort through.
Here is what I have currently cobbled together:
Import-Module ActiveDirectory
$VerbosePreference = "continue"
$list = (Get-ADComputer -LDAPFilter "(&(objectcategory=computer)(OperatingSystem=*server*))").Name
$logfilepath = "$home\Desktop\TasksLog.txt"
$ErrorActionPreference = "SilentlyContinue"
invoke-command -ComputerName $list -ScriptBlock { $sched = New-Object -Com "Schedule.Service"
$sched.Connect()
$out = @()
$sched.GetFolder("\").GetTasks(0) | % {
$xml = [xml]$_.xml
$out += New-Object psobject -Property @{
"Name" = $_.Name
"Status" = switch($_.State) {0 {"Unknown"} 1 {"Disabled"} 2 {"Queued"} 3 {"Ready"} 4 {"Running"}}
"NextRunTime" = $_.NextRunTime
"LastRunTime" = $_.LastRunTime
"Author" = $xml.Task.Principals.Principal.UserId
}
}
$out | fl Name,Status,NextRunTime,LastRunTime,Author, } | Out-File $logfilepath
The "pretty" part is a work in progress. However, if I am correct, this should query a list of server names from AD and then invoke the command on all of them, then output the results to the designated file. I have tested with one server...
$list = "Server1"
...but am hesitant to attempt to invoke these all at once. I am not even sure if this is the correct/most effective way of passing a list of servers to the invoke-command cmdlet.