VMware Cloud Community
panktambe
Contributor
Contributor

Need help with this Script.

Hi,

I've been using this script on powerCLI 4.1, trying to run it on 5.5 now and there is no output.(not on CSV or Command prompt) when I remove the pipe to export .

Connect-VIServer -Server  -Protocol https -User  -Password

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

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

$folders = Get-Folder -Location (Get-Folder -Name vm -Location Datacenters)

# $folders = Get-Folder -Name Folder1, Folder2, Folder3

&{foreach($folder in $folders){

    $vms = Get-VM -Location $folder

    if($vms){

      $stats = Get-Stat -Entity $vms -Stat $metrics -Start $start -ErrorAction SilentlyContinue

      if($stats){

        $stats | Group-Object -Property {$_.Entity.Name} | %{

          $cpuValues = $_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Measure-Object -Property Value -Average -Maximum -Minimum

   $memValues = $_.Group | where {$_.MetricId -eq "mem.usage.average"} | Measure-Object -Property Value -Average -Maximum -Minimum

          New-Object PSObject -Property @{

            Folder = $folder.Name

        VM = $_.Values[0]

        CpuAvg = $cpuValues.Average

        CpuMin = $cpuValues.Minimum

          CpuMax = $cpuValues.Maximum

  MemAvg = $memValues.Average

  MemMin = $memValues.Minimum

  MemMax = $memValues.Maximum

        }

      }

    }

  }}} | Export-Csv C:\AllVMs.csv -NoTypeInformation -UseCulture

Reply
0 Kudos
9 Replies
LucD
Leadership
Leadership

Is there any value in the $folders variable before the ForEach loop starts ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
panktambe
Contributor
Contributor

Thanks for your reply  ...

If I understood your question correctly ... I have like 20 odd Folders on vCenter...and it was able to see all of them when the loop began .

Reply
0 Kudos
LucD
Leadership
Leadership

I mean check in a debugger, place a breakpoint on the foreach line.

When the script hist the breakpoint, check the content of the $folders varaible.

See How to Debug Scripts in Windows PowerShell ISE for some more info on this.

Or just place a 'Write-Output $folders' line before the foreach line.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

panktambe
Contributor
Contributor

Thanks I tried that...and I see the output too. what else can be checked.

Reply
0 Kudos
LucD
Leadership
Leadership

There are several things that can go wrong.

Did you check that there is statistical data from 30 days ago for the VMs ?

You can check from the vSphere Client under the Performance tab.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
panktambe
Contributor
Contributor

we recently built a new Vcenter for 5.5 that was 25 days ago still shouldn't it show information for the past 25 days?

Reply
0 Kudos
LucD
Leadership
Leadership

No, unfortunately the statistics retrieval checks to see if there is data available for your start date.

If there isn't, the current VM is skippen and nothing is returned I'm afraid.

Try setting the Start to today minus 25 days.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
panktambe
Contributor
Contributor

I tried for the last 10 days(just changed the value to -10) still no output....it generates the CSV just doesn't write anything.

Reply
0 Kudos
LucD
Leadership
Leadership

It could be that the aggregation is not running.

Do you actually see 25 days of performance data under the Performance tab for a VM ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos