0

I've created numerous scripts in PowerShell that are working as intended if I execute them directly, however, when I try and setup a schedule to run these in Task Scheduler (to run with highest privileges) it doesn't seem to be running anything at all.

I'm running the following in my actions:

powershell.exe -ExecutionPolicy Bypass -File C:\PS\Mailboxes\CheckForwardingList.ps1

I'm getting a "Last Run Result" of 0x0 and the particular purpose of the above script is to generate a TXT file from EXO which it then mails out via SMTP and I've yet to receive any emails and I also don't see any TXT being generated in the folder where the script is located.

I do have two additional scripts setup which aren't running but once I've addressed the issue on the above this should quickly rectify the problems.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Lions
  • 1
  • 1
  • 1
    in the "program/script" section you have powershell.exe and the rest in the "add arguments" section - right? The Identity used to execute the script is Local System? Does the used Identity have write permissions to the directory? – Toni Sep 07 '22 at 12:18
  • @Toni that's correct. Within the "program/script" part it's only "powershell.exe" while the "add arguments" contains "-ExecutionPolicy Bypass -File {pathToScript}". Everything is taking place on our process machine so the sufficient permissions to write to the directory as far as I'm aware - just on lunch right now however so I'll double check on my return but fairly certain everything is in order. – Lions Sep 07 '22 at 12:23
  • In case you run it under the system account, find a trouble shooting tip here: [Scheduled Task Powershell Script - Runs OK as user account, but not as SYSTEM](https://stackoverflow.com/a/51612478/1701026) – iRon Sep 07 '22 at 12:23
  • This particular issue comes up a lot. Do some searching here on SO, put some error checking/logging in your script (or use `Start Transcript`) to log things and find out what exactly is going wrong when run in TS. – Scepticalist Sep 07 '22 at 12:50

1 Answers1

0

I like to test my PowerShell scripts from a command prompt first.

For example a script called C:\Tests\Test-PowerShellScriptsRunning.ps1 that only contains the following one liner helps me to test if scripts can run successfully on a machine

Write-Host -ForegroundColor Yellow "If you see this, then your script is running"

Next, I run this script from a command prompt, to get the syntax right in my scheduled task:

powershell.exe -nologo -file c:\Tests\Test-PowerShellScriptsRunning.ps1

Of course, you can add the -Executionpolicy bypass parameter, but I prefer to test the execution policy first.

However, as you are running a script that connects to ExchangeOnline, I suspect it has to do with the user you are running this task under. Does the script run if you run this task under your credentials or are the credentials stored on the system or in the script?

You might want to check this article to see how you can register an app and authenticate without storing your credentials on the machine to run the script unattended: App-only authentication for unattended scripts in the EXO V2 module

MarcoJ
  • 41
  • 5
  • This doesn't address the question. Scripts will often run just fine interactively but require specific configuration to run as a scheduled task – Scepticalist Sep 07 '22 at 16:52