Need to Task Schedule powershell script that sends Email

by baba20009 at 2012-10-24 08:14:05

I followed up this link and I am able to email the report OK in exchange powershell, but I cannot get it to task schedule. We have exchange 2007 sp3.
http://www.stevieg.org/2011/06/exchange … nt-report/

Here is the script that I need to place in Task scheduler:

in exchange powershell, i type:

Cd \”program files\Microsoft\exchange server\scripts”
.\Get-ExchangeEnvironmentReport -HTMLReport c:\Report.html -SendMail:$true -MailFrom:test1@domain.com -MailTo:test2@domain.com -MailServer:hub1.domain.com

Thanks
by DonJ at 2012-10-24 12:50:17
So, you would put those commands into a text file that has a .ps1 filename extension. In Task Scheduler, you would schedule


PowerShell.exe -File <filename>.ps1


Obviously, the task will need to run under a credential that has permission to run all of those commands.

If you’ve done this and it isn’t working, it would be helpful if you could share whatever error messages or status messages you’ve got. Look into the Event Log after running the task.
by Pat Richard at 2012-10-24 13:16:34
I wrote a blog post about this a while back, since it can be confusing. Take a look at http://www.ehloworld.com/133 and let me know if you still have questions.
by baba20009 at 2012-10-24 14:47:14
I looked at your link yesterday spent couple hours tested using your scripts and the scheduler task would not run.

I also used this suggesd scripts in Steve Goodman’s link and couldn’t get the scheduler to run:


To make the scheduled task working (without security warning)
1. Set-executionpolicy unrestricted
2. Make a new copy of your great script, as yours is/will be tagged “from internet”

Scheduled Task :

Command : C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Argument : -command “. ‘D:\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1?; Connect-ExchangeServer -auto;C:\Get-ExchangeEnvironmentReport.ps1 -HTMLReport c:\ExchReport.html -SendMail:1 -MailFrom:exchange.reports@xxx.com -MailTo:czi@xxx.ch,GR_DISTRI_IT@xxx.com -MailServer:xxx-cas.xxx.com”

Note that I had to change -SendMail:$True to -SendMail:1 (at least while testing/executing from CMD)
***
by DonJ at 2012-10-24 15:18:40
You can’t directly run a .PS1. Windows doesn’t consider it an executable file type.

Consider putting the ENTIRE set of commands into a .PS1, and then schedule PowerShell.exe as I suggested above.
by baba20009 at 2012-10-24 16:04:36
I just ran this command to produce the error:
.\Get-ExchangeEnvironmentReport -HTMLReport c:\report.html -SendMail:$true -MailFrom:you@example.com -MailTo:you@example.com -MailServer:smtp.example.com -ScheduleAs:DOMAIN\user

I got the report OK through email but no schedule task created. Here was the error message:
"Attempting to schedule: powershell -c &quot;pushd c:\program files\microsoft\exchange server\scripts; C:\program files\microsoft\exchange server\scripts\Get-Exchange… (above scripts)
ERROR: Value for ’ /TR’ option cannot be more than 261 character<s>. "

yesterday when I was manually creating the schedule task, application nor system eventS had any EVENTS.
by DonJ at 2012-10-24 16:30:33
Yup. That’s because the total length of the command is too long. That’s why folks generally put stuff into a batch file (or a .PS1, in the case of PowerShell), and run that. As I’ve suggested. Twice.
by DonJ at 2012-10-24 16:32:49
BTW, what you want to do is what’s described at http://www.ehloworld.com/133. Only instead of using -command to run a really long command, you put the long command into a .PS1 file, and use -File to reference that file. If you’re using -command (or -c, if you’re abbreviating), you have to fall under the maximum path limit, which is why you’re getting that error from your previous post.

At http://www.ehloworld.com/133, he was able to use -command because his total command-line length fell under the limit.
by baba20009 at 2012-10-26 10:44:42
I did putting in a .ps1 file and it did not work. No error produced. No event to view. can you type in what I should put under:

Program/script:

Add arguments:

Thanks
by Zushakon at 2012-11-06 05:16:45
[quote="baba20009"]I did putting in a .ps1 file and it did not work. No error produced. No event to view. can you type in what I should put under:

Program/script:

Add arguments:

Thanks[/quote]

Change to when User is logged and see if it works then.
by Pat Richard at 2012-11-16 10:41:15
Anything new on this? We’re here to help get it working.