Check Scheduled task for run as account PowerShell

Recently I needed to check for all scheduled task running on servers using a certain account as the password needed to be updated.  To search for these scheduled task I used schtask.exe

https://docs.microsoft.com/en-us/windows/win32/taskschd/schtasks

I wanted to integrate this with PowerShell so that I could do some connection tests, loop through a list of devices and output the results to CSV.  Below is the script I created it has three mandatory parameters for the computer list, run as account and export path.

The link to the script is below.

https://github.com/TheSleepyAdmin/Scripts/blob/master/General/Scheduled%20task/Check_RunAsAccount.ps1

To run the script supple the three parameter like below. I used Check_RunAsAccount as the scrip name.

.\Check_RunAsAccount.ps1 -CompList D:\Scripts\Task_Scheduler\Complist.txt -RunAsAccount test1 -ExportLocation D:\Scripts\Task_Scheduler\

SCk1SCk4Once the script has completed there will be a result csv and if there are any non responding devices there will be a second csv. SCk5The export result will look like the below. SCk2

 

4 thoughts on “Check Scheduled task for run as account PowerShell

  1. Thanks for this – in my case I had some tasks running with the domain specified which were not being found (in domain\username format). I changed your -contains to -match to sort this out.

    Like

  2. First off: High praise for using Test-Connection. I hate example code that presumes everything works.
    My issue is that schtasks.exe is not returning clean data. I am getting hundreds of ‘task xml contains a value which is incorrectly formatted’. I had to set $ErrorActionPreference = ‘SilentlyContinue’
    Any thoughts on other ways to retrieve ‘Run As User’ in PowerShell?
    Thanks

    Like

    1. Hi Carl

      If you have WinRM enabled and can do remote CIM session you could use , (Get-ScheduledTask -CimSession ComputerName -TaskName “Task Name”).Principal.UserId

      It is a bit slow running is my only issue with it. To get all task and there run as account might take a while. Would also need to get all task and then loop through each one to pull the required info.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s