New-UDChartJS from Pipeline Output data of a script

Hello,

I created a page that includes a number of charts showing the available hard drive space of each of our servers disks. Everything works great but I’ve hardcoded that in the page itself which slows down load times. I’d rather call the script that is run on a schedule and display the data the same way but I’m not sure how to write that from the pipeline output.

Current Code in the page…

New-UDGrid -Container -Content {
        $serverList = (Get-ADComputer -Filter * -SearchBase "Me Domain OU" | Select-Object -ExpandProperty Name)
        foreach ($server in $serverList) {
            New-UDGrid -Item -ExtraSmallSize 3 -Content {
                New-UDCard -Title $server -Content {
                    $GraphPrep = @(
                        [PsCustomObject]@{ Name = $server; Disk = (get-ciminstance –query "select * from Win32_LogicalDisk" –computer $server).deviceid; TotalDiskSpace = ((get-ciminstance –query "select * from Win32_LogicalDisk" –computer $server).size) | ForEach-Object {[math]::round($_/1GB, 2)}; FreeDiskSpace = ((get-ciminstance –query "select * from Win32_LogicalDisk" –computer $server).FreeSpace) | ForEach-Object {[math]::round($_/1GB, 2)} }
                    )            
                    $availableDiskSpace = New-UDChartJSDataset -DataProperty TotalDiskSpace -Label 'Total (GBs)' -BackgroundColor blue
                    $freeDiskSpace = New-UDChartJSDataset -DataProperty FreeDiskSpace -Label 'Free (GBs)' -BackgroundColor red

                    $Options = @{
                        Type          = 'bar'
                        Data          = $GraphPrep
                        Dataset       = @($availableDiskSpace, $freeDiskSpace)
                        LabelProperty = "Disk"
                        Options = @{
                            scales = @{
                                xAxes = @(
                                @{
                                    stacked = $true
                                }
                            )
                            yAxes = @(
                                @{
                                    stacked = $true
                                }
                            )
                            }
                        }
                    } 
                    New-UDChartJS @Options
                }
            }
        }
    }

Script I’ve created in the PSU Admin Console…

$serverList = (Get-ADComputer -Filter * -SearchBase "My Domain OU" | Select-Object -ExpandProperty Name)
$graphPrep = foreach ($server in $serverList) {
    @(
        [PSCustomObject]@{ Name = $server; Disk = (get-ciminstance –query "select * from Win32_LogicalDisk" –computer $server).deviceid; TotalDiskSpace = ((get-ciminstance –query "select * from Win32_LogicalDisk" –computer $server).size) | ForEach-Object {[math]::round($_/1GB, 2)}; FreeDiskSpace = ((get-ciminstance –query "select * from Win32_LogicalDisk" –computer $server).FreeSpace) | ForEach-Object {[math]::round($_/1GB, 2)} }
    )
}
$GraphPrep

I know it would be something like this to get the data but not sure what to call for each server…

    $Job3 = Get-UAScript -Name 'Test.ps1' | Get-UAJob -OrderDirection Descending -First 1
    $jobData3 = (Get-UAJobPipelineOutput -Job $Job3)
}

Thank you!

I didn’t test this but since you have an array stored you should be able to return that array and iterate it.

    $Job3 = Get-UAScript -Name 'Test.ps1' | Get-UAJob -OrderDirection Descending -First 1
    $jobData3 = (Get-UAJobPipelineOutput -Job $Job3)
        foreach ($data in $jobData3) {
            New-UDGrid -Item -ExtraSmallSize 3 -Content {
                New-UDCard -Title $data.Name -Content {
                    $GraphPrep = $data  
                    $availableDiskSpace = New-UDChartJSDataset -DataProperty TotalDiskSpace -Label 'Total (GBs)' -BackgroundColor blue
                    $freeDiskSpace = New-UDChartJSDataset -DataProperty FreeDiskSpace -Label 'Free (GBs)' -BackgroundColor red

                    $Options = @{
                        Type          = 'bar'
                        Data          = $GraphPrep
                        Dataset       = @($availableDiskSpace, $freeDiskSpace)
                        LabelProperty = "Disk"
                        Options = @{
                            scales = @{
                                xAxes = @(
                                @{
                                    stacked = $true
                                }
                            )
                            yAxes = @(
                                @{
                                    stacked = $true
                                }
                            )
                            }
                        }
                    } 
                    New-UDChartJS @Options
                }
            }
        }
    }

That’s the ticket, thanks Adam!