I need some help getting a “scheduled task” to kicked off the following exchange report "Get-ExchangeEnviromentReport.ps1 I have this script located on my exchange server in the following directory C:\scripts

Here is what i am doing.
1 created a basic schedule task named "ERR"2. tasks runs as administrator
3. runs whether user is logged on or not
4. Runs with highest privileges
Trigger to run daily at 8am…
Action:
Start a program
Program/scripts:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add arguments:
C:\scripts\Get-ExchangeEnvironmentReport.ps1 -HTMLReport c:\report.html -SendMail:$true -MailFrom:email@domian.com -MailTo:email@domain.com -MailServer:mail-server.domian.com"

I click run to test it it says running, but then nothing shows up in my email. I don’t think it is working right. I can run from my exchange powershell and it works. I am not sure how to get the Exchange powershell path in the Program/Scripts section of the scheduled task. I feel like it has to use that in order to work not the standard powershell. Any help would be appreciated.

5 Spice ups

This should help you can add exchange to your power shell session:

Or this

To insert the PowerShell path in the Program/Scripts section of the scheduled task try ‘Start a Program’ powershell.exe -command “C:\scripts\Get-ExchangeEnvironmentReport.ps1” and add your switches/elements

kinda of confused on these. In program under task scheduler do I just add this?

‘C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1’
Connect-ExchangeServer -auto

the add my arrugments below it?

Jason I will try this now.

Try this in the task scheduler

powershell.exe -executionpolicy unrestricted -file “C:\scripts\Get-ExchangeEnvironmentReport.ps1”

Jason here is what I did…

Program/script:

powershell.exe

Add arguments:

-commnad C:\scripts\Get-ExchangeEnvironmentReport.ps1

with everything else behind it of course. Is that what you meant?

David where do I put this at on my task scheduler?

Yes, that is what I mean.

David were do I put this?
powershell.exe -executionpolicy unrestricted -file “C:\scripts\Get-ExchangeEnvironmentReport.ps1”

Jason I tried that still does’nt work. it wants to run the powershell and I need it to run the exchange powershell.

I tried to run this command that I am running on the script in the powershell and here is what i get.

Exchange Management Shell cannot be loaded
At C:\scripts\Get-ExchangeEnvironmentReport.ps1:825 char:3

  • throw “Exchange Management Shell cannot be loaded”
  • CategoryInfo : OperationStopped: (Exchange Manage…annot be loaded:String) , RuntimeException
  • FullyQualifiedErrorId : Exchange Management Shell cannot be loaded

I think I need to figure out how to get the exchange add in for the powershell what do you think?

Do you have a line to initiate the Exchange Powershell module in your script?
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn;

How do I add the exchange powershell ???

The script I have is one that was downloaded called Get-ExchangeEnvironmentReport.ps1

I don’t think that line is in it. How do I add the exchange powershell to the powershell?

Edit the script in notepad and add that line that I included at the top.

ok did that, now what do I put for program/script? and then the arguments?

Jason didn’t work.

Jason I added the following line at the top of the script.

<#

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn;

.SYNOPSIS
Creates a HTML Report describing the Exchange environment

Steve Goodman

THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE
RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.

Scheduled Tasks:

Program/script:

powershell.exe

Add Arguments:

-command C:\scripts\Get-ExchangeEnvironmentReport.ps1 -HTMLReport c:\report.html -SendMail:$true -MailFrom:ExchangeReport@domain.com -MailTo:email@domain.com -MailServer:mailserver.domain.com

It will need to be placed after the #> which is closing the comment section of the script that you are running.

I put it right below the <# at the very top of the script see below.

<#

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn;

Is this not where it goes?

No, that opens up a lengthy comment section which is effectively ignored until the closing #>
Everything between the <# and #> is ignored and used for notes , explanation, etc.
You’ll want to place the command to open the Exchange add in after all of that but before it starts running anything. Googling the script that you downloaded, I’d place that line just before the param section:

.EXAMPLE 
    Generate the HTML report  
    .\Get-ExchangeEnvironmentReport.ps1 -HTMLReport .\report.html 
     
    #> 
param( 
    [parameter(Position=0,Mandatory=$true,ValueFromPipeline=$false,HelpMessage='Filename to write HTML report to')][string]$HTMLReport, 
    [parameter(Position=1,Mandatory=$false,ValueFromPipeline=$false,HelpMessage='Send Mail ($True/$False)')][bool]$SendMail=$false, 
    [parameter(Position=2,Mandatory=$false,ValueFromPipeline=$false,HelpMessage='Mail From')][string]$MailFrom, 
    [parameter(Position=3,Mandatory=$false,ValueFromPipeline=$false,HelpMessage='Mail To')]$MailTo,