First off lets get all the set up variables out of the way. A lot of these are the same variables I use in every script and I just copy them in each time so you may not want to use them all.

I like to use Write-Output to add the actions to the logfile (transcript) started in the previous section. I use this instead of comments as it does the same job but also makes reading the logs easier. I also prefer it to Write-Host since the latter is considered bad practise.

First we’re just going to make sure that the folder required for the logs already exists, and create it if not.

Next make a connection to the DC (I prefer doing this than running scripts on the DC itself. Once this is done, run the LDAP query below to get all the servers in our environment.

Then we get into our foreach loop that is going to check through every server in the list. I’ve added a variable I’m calling $Skip. This jut allows me to speed of the execution of the script by skipping any servers that I know are going to fail. So I use Try Catch to test that I can access the path to my scheduled tasks. I’ve already trapped the error I need for unauthorised access and have it in my catch statement. If this error is triggered then the $Skip flag will be set to avoid wasting time on that record.

The next step will only run if the $Skip flag is not set. A list of tasks will be pulled together from the scheduled tasks library.

The script now works through each task in the variable and gets the content. If the task is not a system task it will be added to the hash table I’m using to generate the final list which will be exported using export-csv. I’ve recently changed the way I build these hash tables as I realised I was using an outdated method from Powershell V1 days previously. There’s a great article on the subject here.

Finally the script exports the results to the path created earlier and cleans up after itself.

If you found the script helpful please leave a comment below or add a link to this article. Likewise if you notice any mistakes please share your experience. The entire script is available below. Good luck!