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 get this error: The term ‘get-adcomputer’ is not recognized as the name of a cmdlet, function, script file, or operable program. the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\folder\Search-Tasks-IDs.ps1:1 char:29 + $Computers = (get-adcomputer <<<< -filter {operatingsystem -like "server"}).name + CategoryInfo : ObjectNotFound: (get-adcomputer:String) , CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
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: (
[Get-ChildItem], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Get-Content : Access to the path 'C:' 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