Hi everyone, I am new to PowerShell and PowerCLI,
I want to create a script that shows all the CPU core usage, and I write something like this :
$cpuUsage = get-vmhost -Name 192.168.1.82 | get-stat -stat 'cpu.usage.average' -Realtime |
Group-Object -Property Instance | Where-Object -Property Name -NE "" | Sort-Object -Property Name
for ($i = 0; $i -lt $cpuUsage.Length; $i++) {
$cpuUsage | Where-Object -Property Name -eq $i |
Select-Object -ExpandProperty Group | Select-Object Timestamp, @{ Name = "instance$i";Expression ={$_.Value}} |
Export-Csv -Path test.csv -Append -Force
}
The results I want are something like this :
Timestamp | instance0 | instance1 | instance2 | instance3 | instance4 | instacne5 |
| 1.23 | 2.1 | 4.3 | 1.7 | 1.65 | 1.32 |
| 1.76 | 2.5 | 2.1 | 3.1 | 1.1 | 1.21 |
| 1.56 | 2.5 | 1.3 | 2.05 | 1.2 | 1.5 |
But the output of my script was like this :
Timestamp | instance0 |
22/4/202214:13:40 | 1.23 |
22/4/202214:13:20 | 1.76 |
22/4/202214:13:00 | 1.56 |
22/4/202214:13:40 | |
22/4/202214:13:20 | |
22/4/202214:13:00 | |
22/4/202214:13:40 | |
22/4/202214:13:20 | |
22/4/202214:13:00 | |
22/4/202214:13:40 | |
22/4/202214:13:20 | |
22/4/202214:13:00 | |
22/4/202214:13:40 | |
22/4/202214:13:20 | |
22/4/202214:13:00 | |
22/4/202214:13:40 | |
22/4/202214:13:20 | |
22/4/202214:13:00 | |
I will be grateful if somebody can help me to improve this script.