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
param(
$MailServer = "mailserver",
$MailTo = "me@company.com",
$Mailfrom = "me@company.com",
$Subject = "Exchange System Status " + (Get-Date))
$body = Get-MailboxDatabase -Status | select Name,LastDifferentialBackup,LastFullBackup |
Out-String
$body2 = Get-ExchangeServer | where {$_.ServerRole -Match "HubTransport"} | Get-Queue | select Identity,Status,MessageCount,NextHopDomain | Out-String
$email = new-object system.net.mail.mailmessage
$email.to.add($MailTo)
$email.from = $Mailfrom
$email.subject = $Subject
$email.isbodyhtml = $False
$email.body = $body,$body2
$client = new-object system.net.mail.smtpclient $mailserver
$client.send($email)
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 }"
-
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.
Agent : 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.From Adam Brand -
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?
Agent : 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 http://exchangeshare.wordpress.com/2008/12/08/how-to-schedule-powershell-script-for-an-exchange-task/Adam Brand : What I mean is, don't use Param. Call everything manually as it is easier to trace.From Marie Fischer -
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") $SMTPMessage.Attachments.Add($Attachment) ###Send the message $SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer $SMTPClient.Send($SMTPMessage)
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
Agent : 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!From Graeme Donaldson -
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
From Jim B
0 comments:
Post a Comment