We help IT Professionals succeed at work.

Server 2008/Exchange 2010 Scheduled Task: Exchange Shell Script Not working

Sleestack90
Sleestack90 used Ask the Experts™
on
I have a scheduled task setup to run once a month that use a script to get Mail Box information and push it out to a CSV file on a network share for reporting.

The Script executes correctly inside Power Shell, however when the task runs it simply Opens Notepad and display the script syntax.

Here is the script:

Get-MailboxStatistics -Server SERVERNAME | select-object DisplayName,TotalItemSize,ItemCount,StorageLimitStatus | export-Csv "T:\Rockbridge Network Documentation\50 Reporting\Email & Archiving\Monthly CSV File\data.csv"

Any idea  why this works correctly when ran inside the shell but doesn't run at all when called from outside?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Meir RivkinFull stack Software Engineer

Commented:
what do u mean by "called from outside"?
Just checking did you clicked on "Run with highest privileges?" in Scheduled Task?

or may be you need to

1. Give acess to user to connect to power shell if you are running from different computer
2. check firewall setting if they are preventing

Commented:
You have to call it together with the PowerShell process itself.
So you schedule powershell.exe process and give arguments: -noninteractive -noprofile -file the-script-file.ps1

Commented:
Sorry, I just saw that it is Exchange 2010
So the sheduled program is PowerShell.exe
Arguments:
-command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Get-MailboxStatistics -Server exch1 | select-object DisplayName,TotalItemSize,ItemCount,StorageLimitStatus | export-Csv "c:\data.csv"

Commented:
With your script the arguments:
-command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Get-MailboxStatistics -Server SERVERNAME | select-object DisplayName,TotalItemSize,ItemCount,StorageLimitStatus | export-Csv "T:\Rockbridge Network Documentation\50 Reporting\Email & Archiving\Monthly CSV File\data.csv"

Author

Commented:
So when I open the Task, and then selection the action the following command: "C:\Exchange 2010\scripts\MonthlyMailboxReport.ps1" isn't going to work because I should be calling PowerShell as the path and then the location of the script file as the arguement?

Author

Commented:
@soostibi:testing now

In the Scheduled program box, I litterally type in  Powershell.exe, and then added the information you provided me with from above.

Author

Commented:
It looks like powershell pops up, disappears instantly....and the script doens't seem to run (based on the fact that I don't have a CSV file to show for it).

What do you suspose I am doing wrong now?

Commented:
If you want to run this from a remote machine (not the Exchange box) you should use as argument:

-command 'New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://yourexchangeserverFQDN/PowerShell/ -Authentication Kerberos; $s = Get-PSSession; Import-PSSession -Session $s; Get-MailboxStatistics -Server SERVERNAME | select-object DisplayName,TotalItemSize,ItemCount,StorageLimitStatus | export-Csv "T:\Rockbridge Network Documentation\50 Reporting\Email & Archiving\Monthly CSV File\data.csv"'

Commented:
My comment 33146821 should work from everywhere. In previous comments I made some bugs (missing ", etc.)

Author

Commented:
Ok so, I am running it on the Exchange 2010 box, also I used the command you mentioned in 33146821 and it appeared to open powershell, and then powershell went went and that was it.

I'm still missing something here. Did I need to browse to the actually powershell.exe instead of just typing in powershell.exe and if so where is the powershell.exe located?
Commented:
I switched the order of ' and " and now it seems to work:

-command "New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://yourexchangeserverFQDN/PowerShell/ -Authentication Kerberos; $s = Get-PSSession; Import-PSSession -Session $s; Get-MailboxStatistics -Server SERVERNAME | select-object DisplayName,TotalItemSize,ItemCount,StorageLimitStatus | export-Csv 'T:\Rockbridge Network Documentation\50 Reporting\Email & Archiving\Monthly CSV File\data.csv'"

If it still does not work then try put the full path for the program:
c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Commented:
Of course you should fill in your servername to the  http://yourexchangeserverFQDN/PowerShell/  part, and to the Get-Mailboxstatistics cmdlet.

Author

Commented:
Exactly!