Gather Scheduled Task information through powershell v2

This topic contains 5 replies, has 4 voices, and was last updated by  egv 2 weeks, 6 days ago.

  • Author
    Posts
  • #84824

    egv
    Participant

    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

  • #84833

    Richard Siddaway
    Moderator

    -File was introduced in PowerShell v3 or v4 and isn't available in PowerShell v2. For PowerShell v2 if you just want to return files you have to use
    Get-ChildItem -Path C:\Scripts\ -Recurse | where {!$_.IsPSContainer}

    instead of

    Get-ChildItem -Path C:\Scripts\ -Recurse -File

    change the -Path to match your needs

    • #84904

      egv
      Participant

      Unfortunately upgrading PS version is not an option right now.

      The suggestion posted by Richard helped to solve the -file issue. Thanks for that.

      It seems Test-connection can't be used either, so I had to skip that check too.

      Well, my script is working now... not the way I wanted, but it does the job.

      Thank you all.

  • #84853

    postanote
    Participant

    Just curious.
    Any reason you are still on PoSHv2?

    I know of a few still there, but based on the state of things, and the fact that v5.1 has been out for a while a v6 is in the pipe for release. I just always found it interesting that folks are not at a minimum of v3/v4, especially those most risk / operational management focused. Well, that and folks scanning the net for PoSH examples and trying to use them as them failing, because of being on legacy v1/v2.

    I know folks have ITIL/CMDB things. SOP's that must be followed, but staying on very old version of any software is, well, we all know, risky. MS has already announced v2 depreciation.

    So, again, just curious.

    • #84866

      Lawrence Gulliver
      Participant

      Not sure about OP but we have an environment of some 300+ servers running Windows 2008 R2, most of them were set up before I started implementing PowerShell management so were only on v2. I got sick of it but it took maybe a week to nail down a reliable process for upgrading (had to convert to EXEs, the update installer didn't always work) and then maybe two months to distribute it.

      Overall it was a huge project, mainly due to the high impact of any downtime on the servers and that the installation required a reboot, so while it was certainly worth it I can understand why in many environments you would be stuck on v2. Things would just be so much easier if Microsoft allowed distribution of WMF through WSUS but natively they don't, and I couldn't get a custom update working.

  • #84895

    postanote
    Participant

    Thanks for getting back with your state of things.

    Agreed with the, 'if Microsoft allowed distribution of WMF through WSUS' statement but I'd qualify that with it should have been this way since v1. However, historically even with regular WSUS/WU stuff, reboots are often required, and distributing via WSUS / WU of WMF would be the same thing. However, at least it would be there and folks would not have to do this manually. There are of course other ways of centrally distributing packages like WMF. SCCM, GPO, logon scripts etc.

    All that being said, you can now distribute WMF / PowerShell via WSUS (since 2016) and MSDN talks to that here:

    'blogs.msdn.microsoft.com/powershell/2016/03/21/windows-management-framework-wmf-5-0-rtm-is-now-available-via-the-microsoft-update-catalog'

    Yep is v5, but that is even better. This can be installed on any supported Windwos OS version, but you need to be aware of one this:

    Exchange Server Supportability Matrix
    'technet.microsoft.com/en-us/library/ff728623%28v=exchg.150%29.aspx'
    Specifically, this section:

    Microsoft .NET Framework
    The following table identifies the version of the Microsoft .NET Framework that can be used together with each version of Exchange. Supported versions are identified by an X character.

You must be logged in to reply to this topic.