Hey guys,

I am pulling my hair out on this one. I have a powershell script that i wrote that works perfectly when i run it manually or even from cmd.exe. I can get it to run multiple ways but i cannot get task scheduler to run it properly. I have tried several ways inside of task scheduler to do it but nothing works. Running Windows server 2008 and Exchange 2007

Here is the script:

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin

$DT = (Get-Date).AddDays(-7).ToString(“d”)

Get-Mailbox -identity myinbox | Export-mailbox -IncludeFolders “\Deleted Items” -StartDate “01/01/2013” -EndDate “$DT” -DeleteContent:$force -Confirm:$false

This is what i have under “action” in task scheduler:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file “C:\script\DeletedItemsPurge.ps1”

I have plugged that into a batch file as well. Ran the batch file and it worked perfectly. I know i have the commands right, but something is not functioning properly when i plug it into task scheduler. Any help would be appreciated!

13 Spice ups

First thing that comes to mind is the permissions the task runs as. Is the task running as the same account you tested the script with? Is it set to run if you are logged in or not?

1.png

2 Spice ups

Did you specify to start it in "C:", I’ve had issues with that before with task scheduler. Even though the script is located in C:, try specifying it in the optional field

1 Spice up

I have tried it with multiple accounts. I tested it with my domain admin account and tried to use that account in the task scheduler to no avail. It is set to run wether user is logged in or not and set to run with highest privileges.

1 Spice up

Good point Sam. Also did you put the arguments in the arguments box, with just the .exe in the Program/script box?

2.png

3 Spice ups

When you add it into task scheduler, do you have it running only when user is logged on try changing it to Run whether user is logged on or not. Also you should select to have it run with the highest permissions. One more thing that I had an issue with is that with the ps1 file it was not set to open with powershell. I changed that and it worked perfectly. Last thing to check, does the computer go to sleep? If so make sure under conditions you have to wake the computer to run this task. Good luck!

1 Spice up

You might find this useful:

http://community.spiceworks.com/how_to/show/17736-run-powershell-scripts-from-task-scheduler

6 Spice ups

Did you manually run the script from the same machine that you are trying to schedule from?

Yes Cduff i have tried it with everything in the program box and the arguments in the arguments box.

Sam i just added that to the task and it still didnt work.

rdilts - It is set to run wether user is logged in or not, and with highest privileges. now when you say the ps1 file was not set to open with powershell what exactly do you mean? From what i understand starting the program powershell in task scheduler should then run whatever argument you put in, inside of powershell. And this computer never goes to sleep.

Martin thank you for that link! That was the first link i found which led me to this site! And i found a couple of your posts which helped me write the script initially.

Yahtzee yes it has been tested and scheduled on the same machine i want it to run on.

And 6 replies in ~20 minutes = member for life.

Welcome to the community!

Is it giving any errors in Last Run Result?

Unfortunately no it is saying it completed successfully.

I’m on Exchange 2010, so I cannot verify. But doesn’t this command:

Export-mailbox -IncludeFolders "\Deleted Items" -StartDate "01/01/2013" -EndDate "$DT" -DeleteContent:$force -Confirm:$false

need a -TargetMailbox or -PSTFolderPath?

jackcarpenter wrote:

This is what i have under “action” in task scheduler:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file “C:\script\DeletedItemsPurge.ps1”

Try using C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe without the ‘-file’ under start program; and use C:\script\DeletedItemsPurge.ps1 under arguments.

What do you have your Execution Policy set to?

@martin9700 's post should be helpful. I tend to opt out for the easy if I need to get it done quickly, as you mentioned if your BAT file can trigger the PS script without trouble then I create a Task Schedule that triggers the BAT file to in turn run the PS script. Not pretty but it works. I am hoping that newer versions of Task Scheduler are going to be friendlier with Power Shell…

I pipe get-mailbox “mailboxname” | to export-mailbox. The command itself works in powershell. Im trying to get our exchange admins to move to 2010! There is so much more i can do on 2010! But thats still a work in progress :slight_smile:

Same issue there paul :frowning: it starts, then says its completed after 10ish seconds.

Can you navigate to C:\script and run the powershell script by ./DeletedItemsPurge.ps1?

Thanks Red pill (also my favorite subreddit) It does the same thing when i use task scheduler to call the batch file. Trying that was my last idea before i posted here. Ive been trying different ways for about a week now. But i refuse to let a piece of silicon best me.

1 Spice up

yes paul i can do that and it runs as it should.