Dave H.
Dave H.

Reputation: 1

using math with a powershell function

I'm trying to perform math within a function. outside of the function I'm able to get it to work, but in the function I get "Method invocation failed because [system.int32[]] doesn't contain a method named 'op_division'"

function fix-size { param( [int[]]$fixme ) if ($fixme -lt 1mb) { $fixme1 = ([system.math]::round($fixme / 1kb, 1)) } write-host $fixme1 }

Upvotes: 0

Views: 817

Answers (1)

jbsmith
jbsmith

Reputation: 1666

The '[int[]]' syntax indicates that $fixme is an ARRAY of ints, and you can't perform division on an array.

So you either need to change your parameter definition toparam( [int]$fixme ) OR, if you really want to operate on multiple numbers, you'll need to do more processing inside the function, like so:

function fix-size { 
    param ([int[]]$fixme)
    foreach ($number in $fixme) {
        if ($number -lt 1mb) {
            ([system.math]::round($number / 1kb, 1))
        }
    }
}

Upvotes: 4

Related Questions