Function Return not work properly

What up everyone?

I’ve been trying to get same code I posted here (without the foreach in Columns) and wrapped it around a Function. The function begins from the top all the way down right above $CloudDataGrid, then at the end of the function I return $clouddt | Select-Object * and stored it within a variable to be used by UDTable.

The idea is to use the function multiple times within the same dashboard page to load multiple UDTable. However, the issue I’m facing is for some reason is adding extra rows when it display the table (pic below). Now if I were to remove the function wrapped around the code then it renders correctly.

function ProcessCloudData {
        param (
            [string]$GroupName
        )
        $clouddt = New-Object System.Data.DataTable
        $clouddt.Clear()
        $clouddt.Columns.Add("Device")

        $cloudsensors = $Global:cloudapidata.sensors | Where {$_.group -eq "$($GroupName)"}
        foreach ($cloudsensorrow in $cloudsensors) {
            if (!($clouddt.Columns.Contains($cloudsensorrow.sensor))) {
                $clouddt.Columns.Add("$($cloudsensorrow.sensor)")
            }
        }

        foreach ($cloudsensor in $cloudsensors) {
            $clouddr = $clouddt.NewRow()
            $clouddr["Device"] = $cloudsensor.device

            $clouddevices = $Global:cloudapidata.sensors | Where {$_.group -eq "$($GroupName)" -and $_.device -eq $cloudsensor.device}
            
            if (!($clouddt.Select("Device = '" + $cloudsensor.device + "'"))) {
                foreach ($clouddevice in $clouddevices) {
                    if($clouddevice.status -eq "Up")
                    {
                        $url = "http://localhost:5000/img/up.png"
                    }

                    if($clouddevice.status -eq "Down")
                    {
                        $url = "http://localhost:5000/img/down.png"
                    }

                    if($clouddevice.status -eq "Warning")
                    {
                        $url = 'http://localhost:5000/img/warning.png'
                    }

                    if($clouddevice.status -eq "Paused (paused)")
                    {
                        $url = "http://localhost:5000/img/pause.png"
                    }

                    if($clouddevice.status -eq "Unusual")
                    {
                        $url = "http://localhost:5000/img/unusual.png"
                    }

                    foreach ($col in $clouddt.Columns) {
                        switch ($clouddevice.sensor)
                        {
                            "$($col.ColumnName)"
                            {
                                $clouddr["$($col.ColumnName)"] = $url
                                break
                            }
                        }
                    }
                }
                $clouddt.Rows.Add($clouddr)
            }
        }
        for ($i = 0; $i -lt $clouddt.Rows.Count; $i++)
        {
            foreach ($emptycell in $clouddt.Columns) {
                $cellname = $emptycell.ColumnName
                if ($clouddt.Rows[$i].$cellname.ToString() -eq "")
                {
                    $clouddt.Rows[$i].Ping = "http://localhost:5000/img/na.png"
                }
            }
        }
        return $clouddt | Select-Object *
    }
    # Here for some reason I have to specify the column i want to display data, if not then get reactjs error
    $CloudDataGrid = ProcessCloudData -GroupName "SSL" | Select-Object Device

    New-UDPaper -Elevation 0 -Content {
        New-UDTypography -Text "Last Refresh: $((Get-Date).DateTime)" -Variant body1
    }

    New-UDPaper -Elevation 1 -Content {
        $CloudGridColumns = @(
            New-UDTableColumn -Property 'Device'
        )
        
        New-UDTable -Id 'sslgrid' -Data $CloudDataGrid -Columns $CloudGridColumns -Title 'Services' -ShowSearch -ShowPagination -PageSizeOption @(5,10,20)
    }
Product: PowerShell Universal
Version: 1.5.8
Framework: Latest