Hi,
I am trying to get the VM Performance Stats for 7 days using 15 mins interval using below, but I am getting the error.
Please help!!
# Define variables and date range
$startDate = (Get-Date).AddDays(-7)
$endDate = Get-Date
$interval = "15m"
$allVMs = @()
# Get the list of VMs from the specified folder
$vms = Get-Folder "Performance" | Get-VM
foreach ($vm in $vms) {
# Extract the vCenter server hostname
$vCenterHost = [uri]$vm.ExtensionData.Client.ServiceUrl | ForEach-Object {$_.Host}
# Initialize a custom object to store VM stats
$vmStats = [PSCustomObject]@{
vCenter = $vCenterHost
Folder = $vm.Folder.Name
VmName = $vm.Name
MemMax = $null
MemAvg = $null
MemMin = $null
CPUMax = $null
CPUAvg = $null
CPUMin = $null
}
# Retrieve CPU statistics
#$statCPU = Get-Stat -Entity $vm -Start $startDate -Finish $endDate -MaxSamples 10000 -Stat cpu.usage.average
$statCPU = Get-Stat -Entity $vm -Start $startDate -Finish $endDate -Interval $interval -Stat cpu.usage.average
if ($statCPU) {
$cpuStats = $statCPU | Measure-Object -Property Value -Average -Maximum -Minimum
$vmStats.CPUMax = [math]::Round($cpuStats.Maximum, 3)
$vmStats.CPUAvg = [math]::Round($cpuStats.Average, 3)
$vmStats.CPUMin = [math]::Round($cpuStats.Minimum, 3)
}
# Retrieve Memory statistics
#$statMEM = Get-Stat -Entity $vm -Start $startDate -Finish $endDate -MaxSamples 10000 -Stat mem.usage.average
$statMEM = Get-Stat -Entity $vm -Start $startDate -Finish $endDate -Interval $interval -Stat mem.usage.average
if ($statMEM) {
$memStats = $statMEM | Measure-Object -Property Value -Average -Maximum -Minimum
$vmStats.MemMax = [math]::Round($memStats.Maximum, 3)
$vmStats.MemAvg = [math]::Round($memStats.Average, 3)
$vmStats.MemMin = [math]::Round($memStats.Minimum, 3)
}
# Add the VM stats to the collection
$allVMs += $vmStats
}
# Export the collected VM stats to CSV
$allVMs | Select-Object vCenter, Folder, VmName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin | Export-Csv "D:\reports\All_VMs_15m.csv" -NoTypeInformation