0

Trying to parse a text file using PowerShell and create a list of sections present in the file. While executing the below code snippet, error is thrown when the object is added to the list.

Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.

The first object gets added to the list but second object onward error is thrown. Interestingly, the error does not occur if the program runs in debug mode.

function Process-Master-File {
    [CmdletBinding()]
    Param (
        [Parameter(ValueFromPipeline = $true)][String[]]$PipeValue
    )
    Begin { 
        #create new section object
        $codeSection = New-Object -TypeName psobject
        $codeSection | Add-Member -MemberType NoteProperty -Name Name -Value $null
        $codeSection | Add-Member -MemberType NoteProperty -Name SuppresionGroup -Value $null
        $codeSection | Add-Member -MemberType NoteProperty -Name Mandatory -Value $False
        $codeSection | Add-Member -MemberType NoteProperty -Name SectionBody -Value $null
        [string]$out = ""
    }
    Process {
        # Grab a line from the pipe
        [string]$line = $_

        # Find positions
        try {
            $pos = 0
            $pos = $line.IndexOf('@Section')
            if ($pos[0] -ne -1) { 
                if ($objTemp.Name) {
                    $objTemp.SectionBody = $section
                    $codeSectionList += $objTemp # Error is thrown here
                    $section = ""
                }

                $objTemp = $codeSection | Select-Object *
                $objTemp.Name = $line.Substring($line.LastIndexOf(':') + 1).TrimEnd().TrimStart()
                $objTemp.SuppresionGroup = $line.Substring($line.IndexOf('@SG') + 3, ($line.LastIndexOf(':') - $line.IndexOf('@SG') - 3)).TrimEnd().TrimStart()
                if ($line.IndexOf('@Mandatory') -ne -1) {
                    $objTemp.Mandatory = $True
                }
                $section = $line
                Write-Verbose $line
            }
            else {
                $section += $line
            }
        }
        Catch {
            Write-Host "An error occurred while processing file:"
            Write-Host $_
        }
    }
    End {
        $codeSectionList
    }
}
dgellow
  • 692
  • 1
  • 11
  • 18
  • `$codeSectionList` variable is used but never defined (and `$objTemp` as well). – JosefZ Apr 11 '20 at 14:32
  • PLEASE, post the _full text of the error_. PoSh usually has some useful info in that text. when you do, add it to your Question & wrap it in code formatting so that everyone can see it & read it easily. [*grin*] – Lee_Dailey Apr 11 '20 at 20:55

1 Answers1

0

You have to initialize $codesectionlist as an array. Otherwise, you're trying to add one pscustomobject to another one.

$codesectionlist += [pscustomobject]@{name='Joe'}
$codesectionlist += [pscustomobject]@{name='John'}

InvalidOperation: Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.

$codesectionlist.gettype().fullname
System.Management.Automation.PSCustomObject


$codesectionlist = @()
$codesectionlist += [pscustomobject]@{name='Joe'} 
$codesectionlist += [pscustomobject]@{name='John'}

$codesectionlist.gettype().fullname
System.Object[]

The global variable might have been in included in the question. The script uses its own script scope, with the += operator. Debugging is like dot sourcing the script.

cat script.ps1
$a += 1
$a += 2
$a

$global:a = @()

.\script
3

. .\script
1
2

Weirdly, $a = $a + 1 works differently than $a += 1 in terms of scope.

cat ./script.ps1
$a
$a = $a + 1
$a = $a + 2
$a

$global:a = @()

./script
1
2

Actually I used a script instead of a function, but everything still applies.

Oh that's right. This is a bug I posted myself last December: +=, functions, and global variables #11297

js2010
  • 23,033
  • 6
  • 64
  • 66
  • Thanks @js2010 your solution provided me an indication of where I was going wrong. Actually I had initialized $codesectionlist as an array else where in the code using _$global:codesectionlist = @()_ I moved this section to the Begin block. It still did not work. Then I removed Global keyword and it worked! **Still not sure why global keyword was causing the issue or why running the old script with a debug point would result in no error!** – Madhur Sharma Apr 12 '20 at 10:23