我正在尝试遵循此示例,以便使用Powershell将图像附加到电子邮件中。以下是代码中行为奇怪的部分:
if ($DirectoryInfo) {
foreach ($element in $DirectoryInfo) {
$failedTest = $element| Select-Object -Expand name
$failedTests += $failedTest
$failedTestLog = "$PathLog$failedTest.log"
$logContent = [IO.File]::ReadAllText($failedTestLog)
$imageDir = "$PathLog$elementFirefox*"
$imageSearch = Get-ChildItem -Path $imageDir -Include *.png -Recurse -Force
$imageFullname = $imageSearch | select FullName | Select-Object -Expand Fullname
$imageFilename = $imageSearch | Select-Object -Expand name
$imageFilename
$imageFullname
# *** THE FOLLOWING LINE CAUSES THE ERROR ***
$attachment = New-Object System.Net.Mail.Attachment –ArgumentList $imageFullname.ToString() # *** CAUSING ERROR ***
#$attachment.ContentDisposition.Inline = $True
#$attachment.ContentDisposition.DispositionType = "Inline"
#$attachment.ContentType.MediaType = "image/jpg"
#$attachment.ContentId = '$imageFilename'
#$msg.Attachments.Add($attachment)
$outputLog += "
********************************************
$failedTest
********************************************
$logContent
"
}
} else {
$outputLog = '** No failed tests **'
}
# Create the Overview report
$outputSummary = ""
foreach ($element in $scenarioInfo) {
if (CheckTest $failedTests $element) {
$outputSummary += "
$element : FAILED" # *** ERROR LINE ***
} Else {
$outputSummary += "
$element : Passed"
}
}
如果我注释掉定义附件的行,代码工作正常。如果我按原样使用代码,则会出现以下错误:
Unexpected token ':' in expression or statement.
At D:TestingDataPowershellLoadRunnerLRmain.ps1:112 char:11
+ $element : <<<< FAILED"
+ CategoryInfo : ParserError: (::String) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
它指的是脚本底部显示"错误行"的行。这到底是怎么回事?这种行为对我来说完全不合逻辑!我不明白一个完全没有效果的语句怎么会在其他地方导致错误! 问题是什么以及如何解决...?
此外,我在违规行中使用$imageFullname
或$imageFullname.ToString()
也没关系。
尝试将"$element : FAILED"
替换为
"$element` : FAILED"
反引号将转义分号;分号在PowerShell中具有特定的含义。(它允许输出子属性:例如$env:username
)
将$outputSummary
定义为数组:
$outputSummary = @()
而不是
$outputSummary = ""