Share via


Unexpected Token Error in Powershell

Question

Wednesday, March 25, 2015 2:37 PM

I am getting the error below, and below that is my script. I'm not a pro at powershell and most of this script came from someone else. In ISE, and I don't know that this matters but thought I'd bring it up, the being quote on line 5 for the email address, is colored brown, and the end quote is colored blue like the email. I actually double quoted the email address on line 5 and got pass the error, but each line with something in quotes failed with the same error. Ultimately, the script did not run after trying all in double quotes. Again sorry for being such a noob, I'm sure it's something simple I'm over looking. It seems to be quite a common error but nothing I found is working. Thanks for your help!

Error
At line:5 char:15
+  $Username = <“[email protected]>”
+               ~~~~~~~~~~~~~~~~~~
Unexpected token '[email protected]
$Password = “yourpassword”
$to = <“[email protected]>”
$subject = “Replica' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

Script
if ((Get-VMReplication | select-string -inputobject {$_.Health} -pattern “Warning”) -like “Warning”)
 {
 $SMTPServer = “smtp.gmail.com”
$SMTPPort = “587″
 $Username = <“[email protected]>”
$Password = “yourpassword”
$to = <“[email protected]>”
$subject = “Replica WARNING error on BDR1”
$message = New-Object System.Net.Mail.MailMessage
 $message.subject = $subject
$message.to.add($to)
 $message.from = $username
 $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
 $smtp.EnableSSL = $true
 $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
 $smtp.send($message)
 }
elseif ((Get-VMReplication | select-string -inputobject {$_.Health} -pattern “Critical”) -like “Critical”)
 {
 $SMTPServer = “smtp.gmail.com”
$SMTPPort = “587″
 $Username = <“[email protected]>”
$Password = “yourpassword”
$to = <“[email protected]>”
$subject = “Replica CRITICAL error on BDR1”
$message = New-Object System.Net.Mail.MailMessage
 $message.subject = $subject
 $message.to.add($to)
 $message.from = $username
 $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
 $smtp.EnableSSL = $true
 $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
 $smtp.send($message)
 }

All replies (4)

Wednesday, March 25, 2015 2:43 PM ✅Answered | 3 votes

Hi,

Looks like smart quotes. Manually retype all of your quotes and try again.

EDIT: Also, look into Send-MailMessage:

https://technet.microsoft.com/en-us/library/hh849925.aspx

Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)


Wednesday, March 25, 2015 2:49 PM ✅Answered | 2 votes

Hi mzenzer,

as Mike said, you got styled quotes in your script. You can probably bulk-edit it by pasting this into your console (replace path as needed):

Get-Content "C:\temp\script.ps1" | %{ $_.Replace('“', '"').Replace('”', '"') } | Set-Content "C:\temp\script.ps1"

Cheers,
Fred

There's no place like 127.0.0.1


Wednesday, March 25, 2015 3:38 PM

Thanks guys, that was it! Much appreciated.


Wednesday, March 25, 2015 3:40 PM

Cheers, you're very welcome.

Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)