Perhaps a very “simple” issue, but I noticed that when looking for this it took me longer than expected to find the result I had in mind.
The issue at hand
I have a script which requires various parameters to be provided so that I can run it.
When running the command through an active console session it would look like this:
.\Get-MailboxAuditLoggingReport.ps1 -Mailbox ProblemMailbox -Hours 24 -SendEmail -MailFrom email@example.com -MailTo firstname.lastname@example.org -MailServer mail.contoso.com
As you can see, this can be quite the pain, but if I get this configured properly can re-use this at various customers.
Now I won’t bore you with all the Scheduled Tasks Wizard screen, as there is only 1 screen that’s important for all of this:
The Start a Program screen
What you need to fill in to make sure you get PowerShell to run the script you require, using the parameters you want it to use.
Full path to PowerShell executable [while some people have gotten it to work with just PowerShell, this should ALWAYS work]
- Add arguments (optional)
PowerShell1-Command "& <full script path> <parameters + values>"
PowerShell1-Command "& C:\Scripts\Get-MailboxAuditLoggingReport.ps1 -Mailbox ProblemMailbox -Hours 24 -SendEmail -MailFrom email@example.com -MailTo firstname.lastname@example.org -MailServer mail.contoso.com"
- Start in (optional)
While this should be all that’s required to make sure it runs properly, I found a tip that might prove quite useful when troubleshooting issues with Scheduled Tasks:
In the Add arguments (optional) section, if you add
; exit $LASTEXITCODE
after your full script path + parameters, it will properly include the last exit code under the Last Run Results column, making troubleshooting that much easier.
Happy scripting 🙂