0

I am trying to create a powershell script (version 2.0) to send inline images, as explained here. But something does not seem to work, or is maybe outdated or incorrect.

The following is a complete test script

$msg = new-object Net.Mail.MailMessage

$image = "testImage.png"
$imageFull = "D:\Testing\testImage.png"

$attachment = New-Object System.Net.Mail.Attachment  –ArgumentList $imageFull

$body=@"
<html>
<body>
<img src="cid:$image">
</body>
</html>
"@

which only uses an example image, and creates the following error:

The string starting:
At D:\Testing\Data\Powershell\LoadRunner\LRtestError.ps1:14 char:1
+  <<<< "@
is missing the terminator: ".
At D:\Testing\Data\Powershell\LoadRunner\LRtestError.ps1:16 char:1
+  <<<<
    + CategoryInfo          : ParserError: (@

:String) [], ParseException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

What is going on? Why is this error created? How to fix it?

NOTE:

  • If you comment out the System.Net.Mail.Attachment line, no error is created!!
  • Used OS: Windows Server 2008 R2 Standard (64-bit)
  • Full version table:

    $psversiontable:
    
    Name                           Value
    ----                           -----
    CLRVersion                     2.0.50727.5485
    BuildVersion                   6.1.7601.17514
    PSVersion                      2.0
    WSManStackVersion              2.0
    PSCompatibleVersions           {1.0, 2.0}
    SerializationVersion           1.1.0.1
    

    PSRemotingProtocolVersion 2.1

3
  • I ran your script on a server with the same version of powershell as yours and it ran without any problem. How do you execute the script? Commented May 30, 2016 at 14:46
  • On a powershell commandline (Windows PowerShell (x86)) as administrator. I change into the directory containinf the script and then I start it as .\testscript.ps1. Commented May 30, 2016 at 14:51
  • Something is fishy. The error states that the string starting in line 14 has no terminator (your script seems to have two more lines though). This means the quotation mark before the @ on line 14 is a opening mark and not a closing one. Thus I guess you have somewhere in the actual script one surplus quotation mark. Most likely in the line 6 with System.Net.Mail.Attachment. This would also explain why omitting this line results in a faultless execution. Commented May 31, 2016 at 12:30

1 Answer 1

1

Make sure that @" are the first characters, and "@ are last characters on the line. And that assigning the variable has it's own line $body=@". The code runs fine for me.

More information about these "Here-Strings" can be found here

$msg = new-object Net.Mail.MailMessage

$image = "testImage.png"
$imageFull = "D:\Testing\testImage.png"

$attachment = New-Object System.Net.Mail.Attachment –ArgumentList $imageFull

$body=@"
<html>
<body>
<img src="cid:$image">
</body>
</html>
"@
Sign up to request clarification or add additional context in comments.

10 Comments

@" must be last, and "@ must be first (not last)
I guess "@ must be first and last. I get errors trying inserting anything before or after.
You can have statements following the terminator, no problem: gist.github.com/IISResetMe/…
I get the exact same error. It looks like the same code. The code still fails for me.
And why does the line before play an important role? What is going on in the attachment line?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.