Solved

Scheduled Task Exchange 2010 powershell fails to execute properly

Posted on 2013-01-17
8
810 Views
Last Modified: 2013-01-23
I have a wonderful script from Exchange Server Pro that I'e modified a bit to meet my needs. This script is typically run using parameters to provide instructions for its execution. It looks like this:

.\Get-MailboxReport.ps1 -customAttribute1 hcca -filename hcca.csv

Open in new window

I've created a second powershell script that contains a number of lines like above to create separate .csv files for each of our shared services members. The script also emails the appropriate .csv to each client.

This second script works perfectly but I need to schedule this as a task. So I created a .bat file with these contents:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; C:\Scripts\Exchange2010\Get-MailboxReport\GenAllReportsAndMail.ps1"

Open in new window

If I run the .bat file interactively from the Exchange Management Shell it executes perfectly. When it is run from the task scheduler the script runs but does not seem to execute the parameters after the Get-MailboxReport.ps1 on any of the lines.

The script which normally takes 90 minutes to complete finishes in 2 minutes without recording any errors but none of the .csv files are actually created.

Any ideas how I can make this work as a scheduled task?
0
Comment
Question by:hcca
8 Comments
 
LVL 5

Expert Comment

by:coraxal
ID: 38790490
Assuming you're using the Task Scheduler on Win 2k8 or 7...I've been able to schedule tasks with the following settings:

Actions tab ->

Program/Script -> "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
Add arguments (optional)  -> "C:\scripts\myscript.bat"
Start in (optional) -> "C:\scripts\"

The .bat file looks something like this:

Powershell -command " & { C:\scripts\thescript.ps1 } "
0
 
LVL 63

Accepted Solution

by:
Simon Butler (Sembee) earned 500 total points
ID: 38793323
Does the script that you are running have this line at the top:

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

Try running it from a regular PowerShell session to see if it works.

Simon.
0
 

Author Comment

by:hcca
ID: 38793469
Each of the powershell scripts involved have this line at the top. There are three scripts involved.

1. Get-MailboxReport.ps1 which requires command line switches when run.
2. GenAllReportsAndMail.ps1 which runs Get-MailboxReport.ps1 13 times with appropriate switches
3. GenAllReportAndMail.bat which is used by the task scheduler to run GenAllReportsAndMail.ps1

I can manually run the .bat file from a PowerShell session and it works perfectly. When it runs as a scheduled task the system thinks it successfully completed but it appears that the command switches for the 13 times that the Get-MailboxReport.ps1 script are not being run. Without those switches no files are generated.

Is this any more clear?
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 63

Expert Comment

by:Simon Butler (Sembee)
ID: 38793499
When you setup the scheduled task, did you set it to run under a specific account?

Simon.
0
 

Author Comment

by:hcca
ID: 38793508
When I scheduled the task I was logged in as the domain administrator. I told the task to run even if not logged in and to run with highest privileged and supplied the password when prompted to finish the task creation.
0
 
LVL 5

Expert Comment

by:basil2912
ID: 38793931
Hello,

Try to have Shell connect to exchange first:

powershell.exe -command ". '[exchange install folder]\v14\bin\remoteExchange.ps1';connect-exchangeserver -auto; [path]\GenAllReportAndMail.bat"
0
 

Author Comment

by:hcca
ID: 38793945
As indicated in my original post, I am doing this with this command in my bat file.

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; C:\Scripts\Exchange2010\Get-MailboxReport\GenAllReportsAndMail.ps1"
0
 

Author Closing Comment

by:hcca
ID: 38812349
I actually found that the presence of this line:
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
in the root script was breaking things. I'm not sure why but by starting there and manually running the script the error was seen and fixed.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In-place Upgrading Dirsync to Azure AD Connect
A list of top three free exchange EDB viewers that helps the user to extract a mailbox from an unmounted .edb file and get a clear preview of all emails & other items with just a single click on mailboxes.
To show how to generate a certificate request in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Servers >> Certificates…
This video discusses moving either the default database or any database to a new volume.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question