如何解决这个powershell ParserError?

时间:2016-05-30 10:55:14

标签: powershell

我正在尝试创建一个PowerShell脚本(版本2.0)来发送内嵌图像,如explained 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>
"@

仅使用示例图像,并创建以下错误:

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

发生了什么事?为什么会出现此错误?如何解决?

注意:

  • 如果您注释掉System.Net.Mail.Attachment行,则不会创建错误!!
  • 使用的操作系统:Windows Server 2008 R2标准版(64位)
  • 完整版表:

    $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

1 个答案:

答案 0 :(得分:1)

确保@"是最后一个字符,"@是该行的第一个字符。并且分配变量具有它自己的行$body=@"。代码运行正常。

有关这些&#34; Here-Strings&#34;的更多信息可以找到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>
"@