Scheduled Tasks Audit

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
epoh97
Posts: 102
Last visit: Wed Apr 18, 2018 6:16 am

Scheduled Tasks Audit

Post by epoh97 »

I am trying to audit the use of Scheduled Tasks within my organization. Company policy says we must use a prefix for all scheduled tasks followed by the purpose. "XYZ-{TaskName}"

Since we are unable to use wildcards in retrieving the Tasks, is there a way to use a tool like schtasks and import that as an object into PoSH? Ultimately, I want a list of custom scripts running in the company and the path of the script (so we can back it up).

Thank you
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Scheduled Tasks Audit

Post by jvierra »

Who says you cannot use a wildcard.
  1. D:\scripts> Get-ScheduledTask -TaskName My*
  2.  
  3. TaskPath                                       TaskName                          State
  4. --------                                       --------                          -----
  5. \Mytasks2\                                     MyTask2                           Disabled
  6. \Mytasks2\                                     MyTask3                           Disabled
  7. \Mytasks2\                                     MyTask4                           Disabled
User avatar
epoh97
Posts: 102
Last visit: Wed Apr 18, 2018 6:16 am

Re: Scheduled Tasks Audit

Post by epoh97 »

Sure enough... Thanks!

This is where I got my info...

https://technet.microsoft.com/en-us/lib ... 49808.aspx

-TaskName<String[]>
Specifies an array of one or more names of a scheduled task.

Aliases
none
Required?
false
Position?
1
Default Value
none
Accept Pipeline Input?
True (ByPropertyName)
Accept Wildcard Characters?
false
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Scheduled Tasks Audit

Post by jvierra »

It works for me as you can see above. Have you even tried it?
User avatar
MikeFRobbins
Posts: 8
Last visit: Thu Apr 11, 2019 1:55 pm

Re: Scheduled Tasks Audit

Post by MikeFRobbins »

If you happen to be working on a system with an older version of PowerShell that doesn't include the ScheduledTasks module, you can use schtasks.exe in PowerShell and produce object based output with it.

Code: Select all

schtasks.exe /Query /FO CSV /V |
ConvertFrom-Csv |
Select-Object -Property @{label='ComputerName';expression={$_.hostname}},
                        @{label='Name';expression={$_.taskname -replace '^.*\\'}},
                        @{label='NextRunTime';expression={$_.'next run time'}},
                        Status,
                        @{label='LogonMode';expression={$_.'Logon Mode'}},
                        @{label='LastRunTime';expression={$_.'Last Run Time'}},
                        @{label='LastResult';expression={$_.'last result'}},
                        Author,
                        @{label='TaskToRun';expression={$_.'Task to Run'}},
                        Comment,
                        @{label='State';expression={$_.'Scheduled Task State'}},
                        @{label='RunAsUser';expression={$_.'Run as User'}},
                        @{label='ScheduleType';expression={$_.'Schedule Type'}},
                        @{label='StartTime';expression={$_.'start time'}},
                        @{label='StartDate';expression={$_.'Start Date'}},
                        Days
That code could easily be turned into a function that can be run against numerous machines remotely.
Mike F Robbins
Microsoft MVP, SAPIEN MVP
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Scheduled Tasks Audit

Post by jvierra »

Mike - That is a good way but the OP seems to have the CmdLet and is debating if he can use wildcards with it. SchTasks cannot use wildcards either.
This topic is 7 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked