none
Powershell Scheduled Tasks to see services account running them RRS feed

  • Question

  • Hello,

    Basically the name says it all I've recently started working with PowerShell and I've been looking for some advice / help on being able to remotely view all the scheduled tasks ran on servers and machines across AD or at least a specified list of servers imported from a CSV or text file.  So far I was able to find and modify a bit close to my likings, but it isn't what I'm precisely looking for.  If there is any advice that someone can help me on that would be greatly appreciated. 

    There is one Script that I found in the download area that gave me an idea on what I am looking for and I have commented on it as there is one other looking something very similar if not identical to what I am trying to accomplish.

    thank you,

    John

    Tuesday, December 19, 2017 4:29 PM

Answers

All replies

  • Hi John,

    We'd be happy to assist you in working out some kinks that you have in a script you've already written, but this forum isn't intended for scripting requests - only for assistance and questions.

    Give it a good, solid try and post your script along with any errors that you are seeing and we'll see what we can find.


    Jeremy Corbello | https://www.jeremycorbello.com

    Tuesday, December 19, 2017 4:40 PM
  • Not a problem,  I may have worded it incorrectly.  I will post it here below there are a few issues I am having here I'll post the entire thing below here. and following that I will post my concerns and issues.

    function Get-AllTaskSubFolders {
        [cmdletbinding()]
        param (
            $FolderRef = $Schedule.getfolder("\")
        )
        if ($FolderRef.Path -eq '\') {
            $FolderRef
        }
        if (-not $RootFolder) {
            $ArrFolders = @()
            if(($Folders = $folderRef.getfolders(1))) {
                $Folders | ForEach-Object {
                    $ArrFolders += $_
                    if($_.getfolders(1)) {
                        Get-AllTaskSubFolders -FolderRef $_
                    }
                }
            }
            $ArrFolders
        }
    }

    function Get-TaskTrigger {
        [cmdletbinding()]
        param (
            $Task
        )
        $Triggers = ([xml]$Task.xml).task.Triggers
        if ($Triggers) {
            $Triggers | Get-Member -MemberType Property | ForEach-Object {
                $Triggers.($_.Name)
            }
        }
    }
    #endregion Functions


    try {
     $Schedule = New-Object -ComObject 'Schedule.Service'
    } catch {
     Write-Warning "Schedule.Service COM Object not found, this script requires this object"
     return
    }

    $Schedule.connect($ComputerName)
    $AllFolders = Get-AllTaskSubFolders

    foreach ($Folder in $AllFolders) {
        if (($Tasks = $Folder.GetTasks(1))) {
            $Tasks | Foreach-Object {
             New-Object -TypeName PSCustomObject -Property @{
                 'Name' = $_.name
                    'Path' = $_.path
                    'State' = switch ($_.State) {
                        0 {'Unknown'}
                        1 {'Disabled'}
                        2 {'Queued'}
                        3 {'Ready'}
                        4 {'Running'}
                        Default {'Unknown'}
                    }
                    'Enabled' = $_.enabled
                    'LastRunTime' = $_.lastruntime
                    'LastTaskResult' = $_.lasttaskresult
                    'NumberOfMissedRuns' = $_.numberofmissedruns
                    'NextRunTime' = $_.nextruntime
                    'Author' =  ([xml]$_.xml).Task.RegistrationInfo.Author
                    'UserId' = ([xml]$_.xml).Task.Principals.Principal.UserID
                    'Description' = ([xml]$_.xml).Task.RegistrationInfo.Description
                    'Trigger' = Get-TaskTrigger -Task $_
                    'ComputerName' = $Schedule.TargetServer
                }
            }
       
        }

    Write($data) | Out-File c:\test2.csv -Append


    }

    the last line there where i am requesting it to output into a file i am unable to get it to output correctly it just shows a long list into the script window.  it also doesn't show the correct location of the scheduled tasks nor does it show who is running the task  the author.  Currently what it is showing me is the active tasks.

    Thank you,

    John

    Tuesday, December 19, 2017 5:24 PM
  • Use this instead:

    IT Pro Today - How-To: Use PowerShell to Report on Scheduled Tasks


    -- Bill Stewart [Bill_Stewart]

    Tuesday, December 19, 2017 5:50 PM
    Moderator
  • This:

    Write($data) | Out-File c:\test2.csv -Append

    is what?

    Should be:

    $data| Out-File c:\test2.csv -Append

    What is "$data"  Where are you getting it from?


    \_(ツ)_/

    Tuesday, December 19, 2017 10:33 PM