Mike Pedrick
Mike Pedrick

Reputation: 21

MS Exchange 2010: cmdlet Not Executing Correctly as a Scheduled Task

I've been using a Scheduled Task and the New-MailboxExportRequest cmdlet to export my organization's mailboxes to .PST archive files each night for awhile now. Unfortunately, the ExportRequests are not removed after the archive operation completes and when Exchange has decided it's seen enough Requests, it stops processing my backups.

The following works well for cleaning up the Requests when run at the Exchange Management Shell:

Get-MailboxExportRequest -Status Completed | Remove-MailboxExportRequest

HOWEVER, I have not been successful in getting this cmdlet to run correctly as a Scheduled Task.

I am using the following syntax:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Get-MailboxExportRequest -Status Completed | Remove-MailboxExportRequest"

When I manually execute the Task, it hangs at 'The task is currently running. (0x41301)' until I end the task. Please note that I am using an account with the proper level of permissions, UAC is disabled, and 'Run with highest privileges' is enabled on the Task. I am therefore inclined to believe that the problem lies with my syntax.

Any and all help is HUGELY appreciated.

EDIT:

Found my solution via an unrelated scripting question; the cmdlet asks for confirmation in normal usage. Therefore, I modified my script to the following:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Get-MailboxExportRequest -Status Completed | Remove-MailboxExportRequest -Confirm:$false"

My Scheduled Task runs as intended and does what it's designed to do. Thanks for the space.

Upvotes: 2

Views: 3728

Answers (1)

JT.
JT.

Reputation: 469

The general way to fix this issue is to pass the switch '-noninteractive' to the powershell.exe. This will ensure that the powershell process will never block on user input for any command.

Note that this is a general recommendation for running PowerShell in a scheduled task, and works in conjunction with the author's fix.

See 'powershell /?' from a command prompt.

Upvotes: 1

Related Questions