Pregunta

Here is my code,

$fourCount = 0
$rowCount = 0
foreach($row in $ResultsTable){
  if ($row.'RowType' -eq 6) {
    if ($fourCount -gt 0) {
      #Sort-Object {$row.'CheckDate'} - descending
      $remainAmount = $currentAmount - $checkAmount
      $ResultsTable.Rows[$rowCount - 1].'Final' = $remainAmount
      $currentAmount = @()
      $fourCount = 0
      $remainAmount = 0
    }
    $checkAmount = [int]$row.'amount'
    $rowCount = $rowCount + 1
  } elseif ($row.'RowType' -eq 4) {
    if($row.'current'.Length -eq $NULL) {
      Continue
    } else {
      $currentAmount += [int]$row.'current'
      $rowCount = $rowCount + 1
      $fourCount++
    } 
  } else {
    Continue
  }
}

And the issue I get when I run my code is

Method invocation failed because [System.Object[]] doesn't contain a method named 'op_Subtraction'.

$remainAmount = $currentAmount - <<<< $checkAmount CategoryInfo : InvalidOperation: (op_Subtraction:String)[], RuntimeException
FullyQualifiedErrorId : MethodNotFound

From what I read, I am converting everything into an int but it still does not run. Can anyone tell me why?

¿Fue útil?

Solución

At line 9, you're initializing $currentAmount as an empty array:

$currentAmount = @()

Then later you're accumulating [int] items into that array:

$currentAmount += [int]$row.'current'

So that what you have is an array of [int].

Eventually you try to do a subtraction operation with that array:

$remainAmount = $currentAmount - $checkAmount

and it fails.

the [Type[]] syntax in the error message indicates it is an array, and you can't do a subtraction operation on that.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top