Trying To pass server name to endpoint but no success

    $Cache:WebInstances | ForEach-Object {

                    $Cache:Server = $_
                    

                        New-UdColumn -Size 6 -Content {
                            New-UdMonitor -Title "CPU (% processor time)" -Type Line -DataPointHistory 20 -RefreshInterval 5 -ChartBackgroundColor '#80FF6B63' -ChartBorderColor '#FFFF6B63'  -Endpoint { 
    						    try {
    								Get-Counter '\Processor(_Total)\% Processor Time' -ComputerName $Cache:Server -ErrorAction SilentlyContinue | Select-Object -ExpandProperty CounterSamples | Select-Object -ExpandProperty CookedValue | Out-UDMonitorData
    							}
                                catch {
    								0 | Out-UDMonitorData
    							} 
                            } 
                        } 
                        New-UdColumn -Size 6 -Content {
                            New-UdMonitor -Title "Memory (% in use)" -Type Line -DataPointHistory 20 -RefreshInterval 5 -ChartBackgroundColor '#8028E842' -ChartBorderColor '#FF28E842'  -Endpoint { 
    							try {
    								Get-Counter '\memory\% committed bytes in use' -ComputerName $Cache:Server -ErrorAction SilentlyContinue | Select-Object -ExpandProperty CounterSamples | Select-Object -ExpandProperty CookedValue | Out-UDMonitorData
    							}
                                catch {
    								0 | Out-UDMonitorData
    							}
                            }  
                        }
                     }

am trying above code but it does not seem to be working, the computer name is always empty any idea why?

Why are you dumping the computer name into a cache? What happens if you just pass $_ directly into -ComputerName?

You could also try and use server = _, no need for a Cache.

tried so many ways didn’t work, computername always empty

Are you working with a single server name or looping through a list of servers? Depending on the number of servers, we have a few options.

am working with 10 servers thats the reason why i tried to do foreach-object cause the servers names might change from time to time.

Had almost the same issue on one of my projects. I used a CSV to hold the server names and looped on them.

Try this…

To create the data—

foreach ($Server in Masterlist){ Set-Item -PSPath "Cache:($server.name)name" -Value $server.name
}

This creates a variable for each server called “XXXXXXname” “YYYYYYYname” “ZZZZZname”

Then to recall the data –

foreach ($Server in $Masterlist) {
Get-Counter ‘\Processor(_Total)% Processor Time’ -ComputerName $$(Get-Item -pspath cache:$($server.name)name) -ErrorAction SilentlyContinue…

Then computername variable would be “XXXXXXXname”

Let me know how this works for you.