Alex
Alex

Reputation: 44385

How to fix powershell expression which causes error later in the code

I am trying to follow this example in order to attach images to an email with powershell. Here is the part of the code that behaves strange:

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\$element\Firefox\*"
        $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"
    }
}

If I comment out the line which defines the attachment, the code works fine. If I use the code as it is, I get the following error:

Unexpected token ':' in expression or statement.
At D:\Testing\Data\Powershell\LoadRunner\LRmain.ps1:112 char:11
+ $element : <<<<  FAILED"
    + CategoryInfo          : ParserError: (::String) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

which refers to the line at the bottom of the script where it says "ERROR LINE". What the heck is going on? The behavior look completely illogical to me! I don't understand how a statement, which has no effect at all, can cause an error elsewhere! What is the problem and how to fix it...?

Also it does not matter if I use $imageFullname or $imageFullname.ToString() in the offending line.

Upvotes: 0

Views: 40

Answers (2)

Steve B
Steve B

Reputation: 37710

Try to replace "$element : FAILED" by

"$element` : FAILED"

The reverse quote will escape the semicolon; which has a specific meaning in PowerShell. (It allows to output subproperty : $env:username for example)

Upvotes: 1

Avshalom
Avshalom

Reputation: 8889

Define the $outputSummary as Array:

$outputSummary = @() 

instead of

$outputSummary = ""

Upvotes: 0

Related Questions