Object for use in multiple function

I have a script that I have built a psobject from either properties or calculations from 3 other collections.

psObj4 has 6 property/values from psObj1, 3 property/values calculated from using psObj2 and psObj3.

I want to get a specific number of property/values from psObj4 into psObj5 that match specific criteria.
When I built psObj4, I was using psObj1 that was a sorted by property [Id] so psObj4 is sorted by the same value. I would like to re-sort psObj4 by one of the calculated property/values, then get the top 15.
Script

Function Initialize {
    #Processor Information
    Write-Host "Getting Processor Info"
    $script:processor = Get-CimInstance Win32_Processor
    $script:lp = $processor.NumberOfLogicalProcessors
    $script:cores = $processor.NumberOfCores

    #Operating System Information
    Write-Host "Getting Operating System Info"
    $script:os = Get-CimInstance Win32_OperatingSystem
  
    #Logical Disk Information
    Write-Host "Getting Logical Disk Info"
    $script:disk = Get-CimInstance Win32_LogicalDisk
  
    #Computer System
    Write-Host "Getting Computer System Info"
    $script:cs = Get-CimInstance Win32_ComputerSystem
  
    #System Enclosure
    Write-Host "Getting System Enclosure Info"
    $script:enc = Get-CimInstance Win32_SystemEnclosure | Select SMBIOSAssetTag,Caption,$ChassisTypes
  
    #System BIOS
    Write-Host "Getting BIOS Info"
    $script:bios = Get-CimInstance Win32_Bios
  
    #System Physical Memory
    Write-Host "Getting Physical Memory Info"
    $script:memory = Get-CimInstance Win32_PhysicalMemory
  
    #Disk Partition Information
    Write-Host "Getting Disk Partition Info"
    $script:dpd = Get-Disk
    $script:dpp = Get-Partition
    $script:dpv = Get-Volume
  
    #Networking Information
    Write-Host "Getting Network Adapter Info"
    $script:adapters = Get-NetAdapter
    $script:interfaces = Get-CimInstance CIM_NetworkAdapter
    $script:nrv4 = Get-NetRoute -AddressFamily IPv4
    $script:nrv6 = Get-NetRoute -AddressFamily IPv6
  
    #Network Statistics
    Write-Host "Getting Network Statistics Info"
    netstat -s > C:\Diags\netstat.txt
    $script:netappstats = Get-NetAdapterStatistics
  
    #Services Information
    Write-Host "Getting Services Info"
    $script:services = Get-CimInstance Win32_Service
  
    #Process Information
    Write-Host "Getting Process Info"
    $script:processes = Get-CimInstance Win32_Process | Select -Property * | Sort ProcessId
    $script:csprocs = (Get-Counter "\Process(*)\ID Process" -ErrorAction SilentlyContinue).CounterSamples
    $script:cscpu = (Get-Counter "\Process(*)\% Processor Time" -ErrorAction SilentlyContinue).CounterSamples
  
    #Service Processes
    Write-Host "Getting Processor Info"
    $script:svcprocs = @{([int]-1) = "-1"}
    foreach ($s in $services) {
      if ($s.State -eq "Running") {
        if ($svcprocs[([int]$s.ProcessId)] -eq $null) {
            $svcprocs += @{([int]$s.ProcessId) = $s.Name }
        } else {
            $svcprocs[([int]$s.ProcessId)] += ("/" + $s.Name)
        }
      }
    }
  
    $script:localdatetime = $os.LocalDateTime
    $script:uptime = (Get-Date) - ($os.LastBootUpTime)
}


Function DiagProcesses {
    $rptProcs=@()
    foreach ($p in $processes) {
    $prpProc=$null  
    $id = $p.ProcessId
      if ($svcprocs[($id)] -ne $null) {
        $procName = "SVC:" + $svcprocs[($p.Id)]
      } else {
        $procName = $p.Name
      }
      $ppath = ($csprocs | Where RawValue -eq $p.id) | Select -ExpandProperty Path
      $ppath = $ppath -replace "\\id process$","\% Processor Time"
      $cv = ($cscpu | Where Path -eq $ppath).CookedValue
      $pctcpu = [Decimal]::Round(($cv/$lp),2)
      $pws     = "{0}/{1}" -f ($p.WorkingSetSize / 1KB), ($p.PeakWorkingSetSize)
      $pvmem   = "{0}/{1}" -f ($p.VirtualSize / 1KB), ($p.PeakVirtualSize / 1KB)
      $ppgmem  = "{0}/{1}" -f ($p.PageFileUsage), ($p.PeakPageFileUsage)
      If ($ppgmem -eq "/") { $ppgmem = "-" }
      #$pnpgmem = "{0}" -f ($p.PrivatePageCount / 1KB)
      $procdata = [pscustomobject]@{
        Id = ($p.ProcessId)
        Name = ($procname)
        'Handle Count' = ($p.HandleCount)
        CPU = ($pctcpu)
        WS = ($pws)
        VM = ($pvmem)
        PM = ($ppgmem)
        SI = ($p.SessionId)
        CommandLine = ($p.CommandLine)
        }
      $script:rptProcs += $procdata
    }
}

  function ClientCPU {
    $usersessions = qwinsta /counter
  
    $infocpu = [psCustomObject]@{
      Name = $processor.Name
      Description = $processor.Description
      Cores = $processor.NumberOfCores
      'Logical Processors' = $processor.NumberOfLogicalProcessors
    }
    $statcpu = [psCustomObject]@{
      Uptime = $uptime.Days
      Users = ($usersessions -match 'Active').Length
      Processes = $processes.Count
      Load = $processor.LoadPercentage
    }
  
    $ptotal = $cscpu | Where Path -Match '(_total)'
    $pidle = $cscpu | Where Path -Match '(idle)'
    $cvcpu = ($cscpu | Where Path -eq $ptotal).CookedValue
    $cvid = ($cscpu | Where Path -eq $pidle).CookedValue
    $pctTotal = [Decimal]::Round(($cv/$lp),2)
    $pctIdle = [Decimal]::Round(($cv/$lp),2)
    $state = [psCustomObject]@{
      '_Total' = $pctTotal
      Idle = $pctIdle
    }
  
    $tpProcs = $rptProcs | Where CPU -gt 0 | Sort CPU | Select -First 15
    $tpProcs  
  }

Initialize
DiagProcesses
ClientCPU - At this point $psObj5 contains nothing

I have tried:

  1. Creating psObj4 as a blank Object with $script:psObj4=@()
  2. Creating psObj4 as a blank Object with $global:psObj4=@()
  3. returning $psObj4 from Function1 (seems to display $psObj4 but I don’t want it to)
  4. $psObj4 = Function1

If I do: $psObj4 | ft -a at the end of function2, it shows all the data that I want.
I just can’t seem to figure out how/why when I use $psObt4 in Function3, I get no data.

Edited as requested.

Scott,
Welcome to the forum. :wave:t3:

Before we proceed … Please go back, edit your question once again and fix the formatting of your code.

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

Not quite. Instead of pseudo code you may post your real code. This may be easier to understand.

If I understand your pseudo code right I’d recommend not to access variables inside your function from a scope ouside your function. If you need the values from a variable inside your function you may provide it to it as parameter. :point_up:t3:

Edit post for exact code

In my search, I choose not to go with Get-Process and using the CPU there because it the amount of processor time that the process has used on all processors, in seconds. by using the Get-Counter, I know that it may not be competely accurate, but for now, I am only interested the process information at the moment the script runs and grabs the information.

The reason for using objects is that I intend to build out a HTML report via the ConvertTo-HTML and I am passing the objects to be converted to HTML tables

I actually don’t know where to start. IMHO Your approach is very weird. The first function only outputs that it does something instead of actually outputting any values.

In your second and third function you access the variables you’ve set in your first function. :man_shrugging:t3: Why did you actually use 3 different functions? And where do you define $psObj5

Of course. You never defined it. At least not in the code you show. :man_shrugging:t3:

I think you’re overcomplicating this a lot. What is it actually what you’re trying to do?

You did not share this part. And it actually does not explain why you’re doing it the way you do. :man_shrugging:t3: