I've got a Powershell script that I'm trying to set up so it can send an Exchange status email to me everyday. I've got the script working just fine when I run it manually from an EMS console window, but when I try to add it as a scheduled task, I need to add the line Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin at the top. This addition seems to be causing a problem, as when I try to run the script from the task window, I get this error:

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin   
$MailServer = "mailserver",
$MailTo = "me@company.com",
$Mailfrom = "me@company.com",
$Subject = "Exchange System Status " + (Get-Date))
$body = Get-MailboxDatabase -Status | select Name,LastDifferentialBackup,LastFullBackup | 
$body2 = Get-ExchangeServer | where {$_.ServerRole -Match "HubTransport"} | Get-Queue | select Identity,Status,MessageCount,NextHopDomain | Out-String
$email = new-object system.net.mail.mailmessage
$email.from = $Mailfrom
$email.subject = $Subject
$email.isbodyhtml = $False
$email.body = $body,$body2
$client = new-object system.net.mail.smtpclient $mailserver

When I have that PSSnapin line at the top and I run the task, I get this error: Invalid assignment expression. The left hand side of an assignment operator needs to be something that can be assigned to like a variable or a property

Taking the line out and then trying to run the task obviously wouldn't work since it doesnt have the Exchange snap in the default powershell window. I'm calling the script using a batch file in the scheduled task with the command: Powershell -command "& {C:\Scripts\exchemail.ps1 }"


Instead of trying to figure out what's wrong, I'll suggest what works 100% for me.

This script gets mailbox stats, but you can adapt it to do whatever you want.

Contents of Get-MailboxStatistics.ps1:

$FromAddress = "noreply@company.local"
$ToAddress = "sysadmin@company.local"
$MessageSubject = "Exchange Mailbox Size Report"
$MessageBody = "Attached is the current list of mailbox sizes."
$SendingServer = "exchange.company.local"

Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | Select-Object DisplayName, @{Name="Size(MB)";Expression={$_.TotalItemSize.Value.ToMB()}}, ItemCount, LastLogonTime | Export-CSV -path "mailboxstats.csv" -notypeinformation

###Create the mail message and add the statistics text file as an attachment
$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress, $MessageSubject, $MessageBody
$Attachment = New-Object Net.Mail.Attachment("mailboxstats.csv")

###Send the message
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer

This is run by a scheduled batch file containing this line:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile "D:\Exchange\Bin\ExShell.psc1" -Command C:\Scripts\Get-MailboxStatistics.ps1
| improve this answer | |
  • Graeme, after futzing around with what I was doing initially, I used your method, which works great. Thanks, I can see many more potential uses for this combo now! – Agent Aug 18 '09 at 15:20

this script must be missing stuff. You are using Param, which must be the first line in script block. What is likely happening is that powershell is looking at this as if you typed

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin   mailserver","me@company.com","me@company.com",Exchange System Status ...

You are missing a function declaration and braces (if that's what you are trying to do). There is no function in the page you mention as source

| improve this answer | |

Could it be an issue with quotes and/or escaping characters? Maybe the difference is not in the one line added/removed, but in the way you run it?

| improve this answer | |
  • I've tried assigning values to the variables with single quotes instead, but still get the same error. For running it, I'm following the guide at this link, which says to just use a batch file to launch powershell along with the script as the input exchangeshare.wordpress.com/2008/12/08/… – Agent Aug 3 '09 at 21:31
  • What I mean is, don't use Param. Call everything manually as it is easier to trace. – Adam Brand Aug 4 '09 at 15:03

It may not be that one line that is the issue...just when it is commented out the whole thing breaks, so the actual error doesn't get a chance to surface.

Try breaking this script up into multiple lines, assigning variables and properties separately, and you should be able to narrow down the issue.

| improve this answer | |
  • Not sure which comment marks you are referring to, but I just edited the script in the original post since the formatting seemed to be screwed up initially. The error message references line 4, char 10 as the problem, but I'm not even changing anything in that line when switching between manual and scheduled/batch files. – Agent Aug 3 '09 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.