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.
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\
Once the script has completed there will be a result csv and if there are any non responding devices there will be a second csv.
The export result will look like the below.
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.
LikeLike
Hi Ray
I think I did use domain name with -contains and it worked fine for me but I will have another check and update the script if I find any issue.
LikeLike
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
LikeLike
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.
LikeLike