Gather Scheduled Task information through powershell v2

I have a script which gathers Scheduled tasks information searching the AD environment. This script runs perfect on Windows Server 2012 servers (Powershell v4) but I can’t make it work on Windows Server 2008 servers (running PS v2). I mention the PS version because I have executed the script on the 2012 server forcing PS to v2 execution, and it didn’t work either. So, apparently, it only works on PS v4 (maybe v3, I can’t tell… but for sure it won’t on PS v2).

I need some help to find out how to modify this script to make it compatible with PS v2. Thanks in advance.

Here the script:

$Computers = (get-adcomputer -filter {operatingsystem -like "*server*"}).name
$ErrorActionPreference = "SilentlyContinue"
$Report = @()
foreach ($Computer in $Computers)
{
    if (test-connection $Computer -quiet -count 1)
    {
        #Computer is online
        $path = "\\" + $Computer + "\c$\Windows\System32\Tasks"
        $tasks = Get-ChildItem -recurse -Path $path -File
        foreach ($task in $tasks)
        {
            $Details = "" | select ComputerName, Task, User, Enabled, Application, Arguments
            $AbsolutePath = $task.directory.fullname + "\" + $task.Name
            $TaskInfo = [xml](Get-Content $AbsolutePath)
            $Details.ComputerName = $Computer
            $Details.Task = $task.name
            $Details.User = $TaskInfo.task.principals.principal.userid
            $Details.Enabled = $TaskInfo.task.settings.enabled
            $Details.Application = $TaskInfo.task.actions.exec.command
            $Details.Arguments = $TaskInfo.task.actions.exec.Arguments

            $Report += $Details
        }
    }
    else
    {
        #Computer is offline
    }
}
$Report |Export-Csv -Path 'c:ScheduledTasks.csv' -NoTypeInformation

First of all - please format your code as code here in the forum.
I’d rather update the Powershell version on the affected servers than spending a lot of effort to make the script compatible with older and deprecated versions. I’d think it will pay off for you in the future.

It would be easier if it could be included in Windows Update rather than having to be downloaded manually but it’s definitely worth updating to PS4. I had a lot of dodgy stuff to get scripts working in PS2.

If you can’t update as can sometimes be the case, we’re at least going to know what the error is or what’s happening, though at least part of your problem will be that Test-Connection is only available in PowerShell 3.0 and up.

Hi there…

Updating PS version is not an option right now, that’s why I’m asking for help on the code.

I have a script which gathers Scheduled tasks information searching the AD environment. This script runs perfect on Windows Server 2012 servers (Powershell v4) but I can’t make it work on Windows Server 2008 servers (running PS v2). I mention the PS version because I have executed the script on the 2012 server forcing PS to v2 execution, and it didn’t work either. So, apparently, it only works on PS v4 (maybe v3, I can’t tell… but for sure it won’t on PS v2).

I need some help to find out how to modify this script to make it compatible with PS v2. Thanks in advance.

Here the script:

$Computers = (get-adcomputer -filter {operatingsystem -like "*server*"}).name
$ErrorActionPreference = "SilentlyContinue"
$Report = @()
foreach ($Computer in $Computers)
{
    if (test-connection $Computer -quiet -count 1)
    {
        #Computer is online
        $path = "\\" + $Computer + "\c$\Windows\System32\Tasks"
        $tasks = Get-ChildItem -recurse -Path $path -File
        foreach ($task in $tasks)
        {
            $Details = "" | select ComputerName, Task, User, Enabled, Application, Arguments
            $AbsolutePath = $task.directory.fullname + "\" + $task.Name
            $TaskInfo = [xml](Get-Content $AbsolutePath)
            $Details.ComputerName = $Computer
            $Details.Task = $task.name
            $Details.User = $TaskInfo.task.principals.principal.userid
            $Details.Enabled = $TaskInfo.task.settings.enabled
            $Details.Application = $TaskInfo.task.actions.exec.command
            $Details.Arguments = $TaskInfo.task.actions.exec.Arguments

            $Report += $Details
        }
    }
    else
    {
        #Computer is offline
    }
}
$Report |Export-Csv -Path 'c:ScheduledTasks.csv' -NoTypeInformation

I have a new version to skip the “get-adcomputer” issue, searching for the servers names in a predefined list:

$list = Get-Content C:servers.txt
Write-Verbose  -Message "Trying to query $($list.count) servers found in 'servers.txt'"
$ErrorActionPreference = "SilentlyContinue"
$Report = @()

foreach ($server in $list) 
{
    if (Test-Connection -ComputerName $server -Count 1 -Quiet)
    {
        #Computer is online
        $path = "\\" + $server + "\c$\Windows\System32\Tasks"
        $tasks = Get-ChildItem -recurse -Path $path -File
        foreach ($task in $tasks)
        {
            if ($tasks)
            {
                Write-Verbose -Message "I found $($tasks.count) tasks for $server"
            }

            $Details = "" | select ComputerName, Task, User, Enabled, Application, Arguments
            $AbsolutePath = $task.directory.fullname + "\" + $task.Name
            $TaskInfo = [xml](Get-Content $AbsolutePath)
            $Details.ComputerName = $server
            $Details.Task = $task.name
            $Details.User = $TaskInfo.task.principals.principal.userid
            $Details.Enabled = $TaskInfo.task.settings.enabled
            $Details.Application = $TaskInfo.task.actions.exec.command
            $Details.Arguments = $TaskInfo.task.actions.exec.Arguments

            $Report += $Details
        }
    }
    else
    {
        #Computer is offline
    }
}
$Report |Export-Csv -Path 'c:ScheduledTasks.csv' -NoTypeInformation

This one runs and creates the csv file, but it only populates the headers (“ComputerName”,“Task”,“User”,“Enabled”,“Application”,“Arguments”) and the servername.

If I remove this line $ErrorActionPreference = “SilentlyContinue” the script output shows this error:

Get-ChildItem : A parameter cannot be found that matches parameter name ‘File’. At C:\folder\Search-Tasks-IDs_new.ps1:25 char:58 + $tasks = Get-ChildItem -recurse -Path $path -File <<<< + CategoryInfo : InvalidArgument: (:slight_smile: [Get-ChildItem], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-Content : Access to the path 'C:&#039; is denied. At C:\folder\Search-Tasks-IDs_new.ps1:35 char:42 + $TaskInfo = [xml](Get-Content <<<< $AbsolutePath) + CategoryInfo : PermissionDenied: (C::String) [Get-Content], UnauthorizedAccessException + FullyQualifiedErrorId : GetContentReaderUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetContentCommand

Windows Management Framework (WMF) 5.0 RTM is now available via the Microsoft Update Catalog