需要关于Powershell错误的帮助“赋值运算符的左侧…”

我有一个Powershell脚本,我试图设置,所以它可以每天发送一个Exchange状态电子邮件给我。 当我从EMS控制台窗口手动运行脚本时,脚本工作得很好,但是当我尝试将其添加为计划任务时,我需要添加行Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin在顶部。 这个添加似乎是导致一个问题,因为当我尝试从任务窗口运行脚本,我得到这个错误:

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin param( $MailServer = "mailserver", $MailTo = "[email protected]", $Mailfrom = "[email protected]", $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) 

当我有顶部PSSnapin行,我运行任务,我得到这个错误: 无效的分配expression式。 赋值运算符的左侧需要是可以赋值给variables或属性的东西

取出线,然后尝试运行该任务显然不会工作,因为它没有在默认的PowerShell窗口Exchange交换。 我使用以下命令在计划任务中使用batch file调用脚本: Powershell -command“&{C:\ Scripts \ exchemail.ps1}”

与其试图弄清楚什么是错误的,我会build议什么对我有效。

这个脚本获取邮箱统计信息,但是您可以调整它来做任何你想要的。

Get-MailboxStatistics.ps1的内容:

 $FromAddress = "[email protected]" $ToAddress = "[email protected]" $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) 

这由包含以下行的计划batch file运行:

 C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile "D:\Exchange\Bin\ExShell.psc1" -Command C:\Scripts\Get-MailboxStatistics.ps1 

这个脚本一定是丢失的东西。 您正在使用Param,它必须是脚本块的第一行。 可能发生的事情是,PowerShell正在看这个,就像你input

 Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin mailserver","[email protected]","[email protected]",Exchange System Status ... 

你缺less一个函数声明和大括号(如果这就是你想要做的)。 您提到的网页中没有任何function

这可能是引号和/或转义字符的问题吗? 也许差异不在一行中添加/删除,但在你运行它的方式吗?

也许不是那一行就是这个问题……只是当注释掉整件事情时,实际的错误就没有机会出现。

尝试将这个脚本分成多行,分别分配variables和属性,你应该能够缩小这个问题的范围。