{"id":252,"date":"2013-06-20T15:27:29","date_gmt":"2013-06-20T19:27:29","guid":{"rendered":"http:\/\/devopsny.com\/blog\/?p=252"},"modified":"2013-06-20T15:27:29","modified_gmt":"2013-06-20T19:27:29","slug":"enumerating-scheduled-tasks-using-powershell","status":"publish","type":"post","link":"https:\/\/devopsny.com\/blog\/2013\/06\/20\/enumerating-scheduled-tasks-using-powershell\/","title":{"rendered":"Enumerating Scheduled Tasks using Powershell"},"content":{"rendered":"<p>I need to get a list of Scheduled tasks from a Windows server out to a third party. So, I was searching for a way to do this via a script. This is the solution I came up with:<\/p>\n<pre name=\"code\" class=\"vb\">\r\nparam([String]$schedPath=\"\\\")\r\nfunction getTasks($path) {\r\n    $out = @()\r\n\r\n    # Get root tasks\r\n    $schedule.GetFolder($path).GetTasks(0) | % {\r\n        $xml = [xml]$_.xml\r\n        $out += New-Object psobject -Property @{\r\n            \"Name\" = $_.Name\r\n            \"Path\" = $_.Path\r\n            \"LastRunTime\" = $_.LastRunTime\r\n            \"NextRunTime\" = $_.NextRunTime\r\n            \"Actions\" = ($xml.Task.Actions.Exec | % { \"$($_.Command) $($_.Arguments)\" }) -join \"`n\"\r\n        }\r\n    }\r\n\r\n    # Get tasks from subfolders\r\n    $schedule.GetFolder($path).GetFolders(0) | % {\r\n        $out += getTasks($_.Path)\r\n    }\r\n\r\n    #Output\r\n    $out\r\n}\r\n\r\n$tasks = @()\r\n\r\n$schedule = New-Object -ComObject \"Schedule.Service\"\r\n$schedule.Connect() \r\n\r\n# Start inventory\r\n$tasks += getTasks($schedPath)\r\n\r\n# Close com\r\n[System.Runtime.Interopservices.Marshal]::ReleaseComObject($schedule) | Out-Null\r\nRemove-Variable schedule\r\n\r\n# Output all tasks\r\n$tasks\r\n<\/pre>\n<p>To execute the script, from the Powershell command prompt, I use the following string:<\/p>\n<pre>\r\n.\\GetScheduledTasks.ps1 \\Folder\\TasksForMe | Format-Table -Wrap -Property Name,NextRunTime,Actions > FolderTasksForMe.txt\r\n<\/pre>\n<p>Powershell is not one of my strong points. So, there is probably a lot more to do with this.<\/p>\n<p>Thanks to these two posts:<\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/15439542\/how-to-use-powershell-to-inventory-scheduled-tasks\" title=\"how to use PowerShell to inventory Scheduled Tasks\" target=\"_blank\">how to use PowerShell to inventory Scheduled Tasks<\/a><\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/5592531\/how-to-pass-an-argument-to-a-powershell-script\" title=\"How to pass an argument to a PowerShell script?\" target=\"_blank\">How to pass an argument to a PowerShell script?<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I need to get a list of Scheduled tasks from a Windows server out to a third party. So, I was searching for a way to do this via a script. This is the solution I came up with: param([String]$schedPath=&#8221;\\&#8221;) function getTasks($path) { $out = @() # Get root tasks $schedule.GetFolder($path).GetTasks(0) | % { $xml [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[145,144,11],"tags":[146,147,148],"class_list":["post-252","post","type-post","status-publish","format-standard","hentry","category-powershell","category-system-administration","category-windows-server-2008","tag-poweshell","tag-sa","tag-systemadministration"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3fcrv-44","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/devopsny.com\/blog\/wp-json\/wp\/v2\/posts\/252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devopsny.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devopsny.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devopsny.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/devopsny.com\/blog\/wp-json\/wp\/v2\/comments?post=252"}],"version-history":[{"count":7,"href":"https:\/\/devopsny.com\/blog\/wp-json\/wp\/v2\/posts\/252\/revisions"}],"predecessor-version":[{"id":259,"href":"https:\/\/devopsny.com\/blog\/wp-json\/wp\/v2\/posts\/252\/revisions\/259"}],"wp:attachment":[{"href":"https:\/\/devopsny.com\/blog\/wp-json\/wp\/v2\/media?parent=252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devopsny.com\/blog\/wp-json\/wp\/v2\/categories?post=252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devopsny.com\/blog\/wp-json\/wp\/v2\/tags?post=252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}