sjtmal
Contributor
Contributor

Power cli script is not working for multiple VM

Jump to solution

Hi,

Please find the script to get cpu and memory usage of VM.

Attached script is working fine for single VM. We are getting output for only one vm, even there are multiple vm in source file. Output file is also attached.

0 Kudos
1 Solution

Accepted Solutions
jrmunday
Commander
Commander

I'm sure there are more elegant ways of doing this, but this should work for you;

$metrics = 'cpu.usage.average', 'mem.usage.average'

$start = (Get-Date).AddDays(-1)

$vmList = Get-Content -Path C:\_temp\VMs.txt

$MyCol = @()

$Results = @()

foreach ($vm in $vmList

{

    Get-Stat -Entity $vm -Stat $metrics -Start $start |

    Group-Object -Property Entity |

    ForEach-Object -Process {

        $MyCol = '' | Select-Object -Property VM, Date, CPU, Memory

       

        $MyCol.VM     = $_.Group[0].Entity.Name

        $MyCol.Date   = $_.Group[0].Timestamp.Date

        $MyCol.CPU    = $_.Group |

        Where-Object -FilterScript {

            $_.MetricId -eq $metrics[0]

        } |

        Measure-Object -Average -Property Value |

        Select-Object -ExpandProperty Average

        $MyCol.Memory = $_.Group |

        Where-Object -FilterScript {

            $_.MetricId -eq $metrics[1]

        } |

        Measure-Object -Average -Property Value |

        Select-Object -ExpandProperty Average

    }

    $Results += $MyCol

}

$Results | Export-Csv -Path C:\_temp\vm-resource-report.csv -NoTypeInformation -UseCulture

vExpert 2014 - 2018 | VCP6-DCV | http://www.jonmunday.net | @JonMunday77

View solution in original post

0 Kudos
2 Replies
jrmunday
Commander
Commander

I'm sure there are more elegant ways of doing this, but this should work for you;

$metrics = 'cpu.usage.average', 'mem.usage.average'

$start = (Get-Date).AddDays(-1)

$vmList = Get-Content -Path C:\_temp\VMs.txt

$MyCol = @()

$Results = @()

foreach ($vm in $vmList

{

    Get-Stat -Entity $vm -Stat $metrics -Start $start |

    Group-Object -Property Entity |

    ForEach-Object -Process {

        $MyCol = '' | Select-Object -Property VM, Date, CPU, Memory

       

        $MyCol.VM     = $_.Group[0].Entity.Name

        $MyCol.Date   = $_.Group[0].Timestamp.Date

        $MyCol.CPU    = $_.Group |

        Where-Object -FilterScript {

            $_.MetricId -eq $metrics[0]

        } |

        Measure-Object -Average -Property Value |

        Select-Object -ExpandProperty Average

        $MyCol.Memory = $_.Group |

        Where-Object -FilterScript {

            $_.MetricId -eq $metrics[1]

        } |

        Measure-Object -Average -Property Value |

        Select-Object -ExpandProperty Average

    }

    $Results += $MyCol

}

$Results | Export-Csv -Path C:\_temp\vm-resource-report.csv -NoTypeInformation -UseCulture

vExpert 2014 - 2018 | VCP6-DCV | http://www.jonmunday.net | @JonMunday77

View solution in original post

0 Kudos
sjtmal
Contributor
Contributor

Thanks Jon. It is working as expected.

Though I would like to do it in more elegant way:) Please suggest.

0 Kudos