Solved

Unable to run an exchange 2010 powershell script from Task Scheduler

Posted on 2016-08-31
11
74 Views
Last Modified: 2016-09-01
Hey guys!  (Please note, I am not a powershell scripter)  I am able to successfully run this command from Exchange Management Shell:

./Get-PublicFolderReplicationReport.ps1 -Subject "Exch Daily PF Replication Report" -AsHTML -To ExchangeAdmins@fabricam.com -From Public.Folder.Repl@fabricam.com -SmtpServer mail.fabricam.com -SendEmail

(note, the folder this is in is: C:\_Utils\ExchScripts\PublicFolderReplication)

Now for the part that always drives me crazy.  Getting it to work in Windows 2008 Scheduler (ugh)

So the bottom line is, i get an error stating an exchange command within the .ps1 file doesn't exist.  It occurred to me that the exchange extensions are not being loaded by just running powershell.exe.  Que a days worth of googling and testing and lots of different advice and different switches, and still it wont work from Task Manager.

Question:  What do I put into Task Manager for

1) Task Manager - Program/Script:
2) Task Manager - Add Arguments (optional):

Here are some of the things I've tried unsuccessfully

1) Task Manager - Program/Script:
C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe
2) Task Manager - Add Arguments (optional):
-version 2.0 -NonInteractive -WindowStyle Hidden -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; 'C:\_Utils\ExchScripts\PublicFolderReplication\Get-PublicFolderReplicationReport.ps1' -Subject "Exch Daily PF Replication Report" -AsHTML -To ExchangeAdmins@fabricam.com  -From Public.Folder.Repl@fabricam.com  -SmtpServer mail.fabricam.com -SendEmail

OR

1) Task Manager - Program/Script:
powershell.exe
2) Task Manager - Add Arguments (optional):
-PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\V14\Bin\ExShell.psc1" -Command ". 'C:\_Utils\ExchScripts\PublicFolderReplication\Get-PublicFolderReplicationReport.ps1 -Subject "Exch Daily PF Replication Report" -AsHTML -To ExchangeAdmins@fabricam.com -From Public.Folder.Repl@fabricam.com -SmtpServer mail.fabricam.com -SendEmail'"

Hell, I dont even know where to put "-ExecutionPolicy Bypass".  Suggestions?
0
Comment
Question by:OasisEE
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 16

Expert Comment

by:Spike99
ID: 41778755
Does this page help?
https://technet.microsoft.com/en-us/library/bb123798(v=exchg.141).aspx

On that page, it says:
If you want to run a script on a scheduled basis using the Windows Task Scheduler service, you can call the Shell and include the script that you want to run as a parameter. If you want to use Exchange cmdlets with your script, you must direct Windows PowerShell to connect to a server running Exchange and load the Exchange cmdlets you have access to. The shortcut you use to open the Shell does this automatically. To do this when you want to run a script that contains Exchange cmdlets, you must direct Windows PowerShell to run the scripts that make this connection. This syntax is required to open Windows PowerShell, connect to an Exchange server, and run your script from the Cmd.exe command.

Then, it gives this example of how to launch the Exchange Management shell & run the script:
PowerShell.exe -command ". 'D:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; <path to your script>"

Open in new window

0
 

Author Comment

by:OasisEE
ID: 41778781
Thanks for the quick reply Alicia W. but i tried the following as per your post and that didnt work. :(

1) Task Manager - Program/Script:
powershell.exe
2) Task Manager - Add Arguments (optional):
 -command ". 'D:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; C:\_Utils\ExchScripts\PublicFolderReplication\Get-PublicFolderReplicationReport.ps1 -Subject "Exch Daily PF Replication Report" -AsHTML -To ExchangeAdmins@Fabricam.com -From Public.Folder.Repl@Fabricam.com -SmtpServer mail.Fabricam.com -SendEmail"
0
 
LVL 16

Expert Comment

by:Spike99
ID: 41778852
Does this page help?
http://blogs.interfacett.com/how-to-run-the-exchange-management-shell-ems-in-powershell

It gives instructions on how to manually create a remote session on the exchange (he uses a variable he created "$s" to connect to the exchange server).

So, perhaps you could add a similar variable to your script to manually create the connection to your exchange server.
0
 

Author Comment

by:OasisEE
ID: 41778874
Dont forget Alicia.  My script works fine when run from the Exchange Powershell Command Line.  I am having trouble shoe horning that into Windows Task Scheduler.

What you are suggesting is to make powershell.exe load the exchange commands on the fly.  I just want to get windows task manager to work with powershell + exchange commands loaded + my working .ps1 file.
0
 
LVL 20

Expert Comment

by:jmcg
ID: 41778980
I don't have an Exchange setup to look at right now, but can I suggest looking at the scheduled tasks that Exchange has already set up and imitating what they do. Yes, it's important to load the Exchange modules as part of the scheduled task.
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 40

Expert Comment

by:Subsun
ID: 41779834
Are you scheduling this on a exchange server?

If yes. Use the following command to schedule script. If it's not working run the same command from command prompt and let me know the error..
powershell -command "Add-PSSnapin Microsoft.Exchange.Management.Powershell.Support;Add-PSSnapin  Microsoft.Exchange.Management.PowerShell.E2010;C:\_Utils\ExchScripts\PublicFolderReplication\Get-PublicFolderReplicationReport.ps1 -Subject "Exch Daily PF Replication Report" -AsHTML -To ExchangeAdmins@fabricam.com -From Public.Folder.Repl@fabricam.com -SmtpServer mail.fabricam.com -SendEmail"

Open in new window

0
 

Author Comment

by:OasisEE
ID: 41780232
Hi Subsun!

Thanks for start.  I used what you gave me and I got some weird errors.  Like it complained about not seeing any public folders so i added (as per the script author's website)

-ComputerName EXC026,EXC056 -FolderPath "\" -Recurse

Open in new window


And then the subject of the email was just

Exch

Open in new window


partially pulled from

-Subject "Exch Daily PF Replication Repor"t -AsHTML

Open in new window


So I added Single quotes instead of double quotes

-Subject 'Exch Daily PF Replication Report' -AsHTML

Open in new window


So I opened a regular powershell window and ran the command successfully.  Problem is, it is not working in Task Scheduler even though it says "The operation completed successfully. (0x0).  Is there a way I can see what is going on when it runs in task scheduler?  So we're getting closer but still not quite there...
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 41780257
Make sure you are using an account which has proper exchange admin permission to run this task.

Another option is to put the entire command which is working for you in to a .bat file, run the bat file and see if it works.

If the test is successful then configure the task to trigger the bat file.  by this way you can make sure the task scheduler is not messing up the syntax.
0
 

Author Comment

by:OasisEE
ID: 41780363
Hi Subsun!

Thanks for start.  I used what you gave me and I got some weird errors.  Like it complained about not seeing any public folders so i added (as per the script author's website)

-ComputerName EXC026,EXC056 -FolderPath "\" -Recurse

Open in new window


And then the subject of the email was just

Exch

Open in new window


partially pulled from

-Subject "Exch Daily PF Replication Repor"t -AsHTML

Open in new window


So I added Single quotes instead of double quotes

-Subject 'Exch Daily PF Replication Report' -AsHTML

Open in new window


So I opened a regular powershell window and ran the command successfully.  Problem is, it is not working in Task Scheduler even though it says "The operation completed successfully. (0x0).  Is there a way I can see what is going on when it runs in task scheduler?  So we're getting closer but still not quite there...
0
 

Author Comment

by:OasisEE
ID: 41780366
Ive given up with trying to run powershell through Task Manager.  I have now successfully pulled it off by placing the command in a bat file and just running that.  I'm disgusted with how difficult something like this turned out to be.  Task Manager is garbage.

Thanks everyone for your help.
0
 

Author Closing Comment

by:OasisEE
ID: 41780371
putting the command into a .bat file and running that through task manager is the easiest way to pull this off.  thank you very much!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
Marketers need statistics and metrics like everybody else needs oxygen. In this article we explain how to enable marketing campaign statistics for Microsoft Exchange mail.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now