Function in Expression field

Hello Community,

i would like to calculate a value ProcTimeTotalMinutes inside an expression field with a function.
The function will be triggered, but the result will not be displayed.

Where is the problem?

Thanks

Function CPUMinutes () {

param ($cputime)

write-host "new param: $cputime" 

$timepieces = $timestring.Split(':')
[int]$hours      = $timepieces[0]
[int]$minutes    = $timepieces[1]
[int]$seconds    = $timepieces[2].Split('.')[0]

[int]$CPUMinutes = ( $hours * 60 ) + $minutes + ( $seconds / 60 )
Return $CPUMinutes

}

Get-Process | Where-Object { $.ProcessName -like “XS*” }| Select-Object SessionId, Id,
ProcessName, @{ Name = 'PagedMemMB'; Expression = { [int] ($_.PagedMemorySize64 / 1mB) } },
@{ Name = ‘NonPagedMemMB’; Expression = { [int] ($
.NonpagedSystemMemorySize64 / 1mB) } }, @{ Name = 'VirtMemMB'; Expression = { [int] ($_.VirtualMemorySize64 / 1mB) } },
@{ Name = ‘ProcTimeTotalMinutes’; Expression = { [int] ( CPUMinutes -cputime $_.TotalProcessorTime ) } }, TotalProcessorTime
| FT

Hi, welcome to the forum :wave:

Firstly, when posting code in the forum, please can you use the preformatted text </> button. It really helps us with readability, and copying and pasting your code (we don’t have to faff about replacing curly quote marks to get things working).

You’re referencing the value passed to your function as $timestring rather than $cputime which is one problem, but the real problem is that you’re passing TotalProcessorTime to your function and trying to handle it as a String but it isn’t, it’s a TimeSpan object which doesn’t have a Split() method.

Instead, you should directly access the Hours,Minutes, and Seconds properties of the TimeSpan object. You could do this to fix it:

function CPUMinutes () {

    param ($cputime)

    Write-Host "new param: $cputime" 

    [int]$hours = $cputime.Hours
    [int]$minutes = $cputime.Minutes
    [int]$seconds = $cputime.Seconds

    [int]$CPUMinutes = ( $hours * 60 ) + $minutes + ( $seconds / 60 )
    $CPUMinutes

}

Personally, I wouldn’t use a function. I’d just define the expression like this:

@{ Name = 'ProcTimeTotalMinutes'; Expression = { [int][math]::Ceiling($_.TotalProcessorTime.TotalMinutes) } }