I finally wrote a script that suits my needs. This script will 'scan' all the servers listed in AD, searching in the c:\Windows\System32\tasks folder for xml files. Then it will write the value of the UserID xml node of each file, in the final CSV file.
Not yet perfect but totally working to list all tasks of all servers, and log which user account is used to run them.
<#
.Synopsis
PowerShell script to list all Scheduled Tasks and the User ID
.DESCRIPTION
This script scan the content of the c:\Windows\System32\tasks and search the UserID XML value.
The output of the script is a comma-separated log file containing the Computername, Task name, UserID.
#>
Import-Module ActiveDirectory
$VerbosePreference = "continue"
$list = (Get-ADComputer -LDAPFilter "(&(objectcategory=computer)(OperatingSystem=*server*))").Name
Write-Verbose -Message "Trying to query $($list.count) servers found in AD"
$logfilepath = "$home\Desktop\TasksLog.csv"
$ErrorActionPreference = "SilentlyContinue"
foreach ($computername in $list)
{
$path = "\\" + $computername + "\c$\Windows\System32\Tasks"
$tasks = Get-ChildItem -Path $path -File
if ($tasks)
{
Write-Verbose -Message "I found $($tasks.count) tasks for $computername"
}
foreach ($item in $tasks)
{
$AbsolutePath = $path + "\" + $item.Name
$task = [xml] (Get-Content $AbsolutePath)
[STRING]$check = $task.Task.Principals.Principal.UserId
if ($task.Task.Principals.Principal.UserId)
{
Write-Verbose -Message "Writing the log file with values for $computername"
Add-content -path $logfilepath -Value "$computername,$item,$check"
}
}
}
The output is a comma-separated file generated on your desktop, like this one :
> SRV028,CCleanerSkipUAC,administrator
> SRV029,GoogleUpdateTaskMachineCore,System
> SRV030,GoogleUpdateTaskMachineUA,System
> SRV021,BackupMailboxes,DOMAIN\administrator
> SRV021,Compress&Archive,DOMAIN\sysScheduler
What's the local system using? (OS & PowerShell version) – Iszi – 2014-09-15T12:29:10.840
@root : As Iszi said, this Cmdlet is supported on Windows Server 2012 or Windows 8. Please read carefully the question before commenting/answering. – Ob1lan – 2014-09-15T13:01:06.073
1@Iszi The local system is Windows 7 with PowerShell v3 – Ob1lan – 2014-09-15T13:01:33.850
@Ob1lan I've removed my comment for being inaccurate/unhelpful – root – 2014-09-15T13:02:27.850
@root If you're running Win7 SP1, you may want to consider upgrading to PowerShell 4.0. That may help expand your options a bit, although it still won't bring you up to the level of a Win 8.1 system. I'm also not 100% sure how PowerShell works when the version on the local system is different than the one on the remote system. I've posted a question for that here.
– Iszi – 2014-09-15T13:12:25.893@Iszi I'm not sure upgrading to PowerShell v4 on the local machine will do the trick in this situation. Indeed, it will not make the Get-Scheduled task usable of remote systems, as far as I understand. I think both systems should recognize it. I'll give a try asap. – Ob1lan – 2014-09-15T13:18:07.670
Ack! @root - pardon the mis-directed mention above. – Iszi – 2014-09-15T13:18:42.703
@Ob1lan It won't bring you
Get-ScheduledTasks
, but it may have other features and functionality which could be useful - if not for this particular script, then possibly for others. Again though, I'm not sure how PowerShell handles things when the remote version is different from the local one. Depending on the commands and features you end up needing, you may or may not be able to use them against your Server 2003 systems which only support up to PowerShell 2.0. – Iszi – 2014-09-15T13:21:35.290@Ob1lan By the way, if you're not already familiar with
ForEach-Object
, you'll want to start learning about that. Whatever ends up being the answer, it will probably have something along the lines of: 1. Make a list of server names in a text file (e.g.: Servers.txt), one name/ip per line. 2.Get-Content Servers.txt | ForEach-Object { ... }
– Iszi – 2014-09-15T14:19:51.640