VMware Cloud Community
kwg66
Hot Shot
Hot Shot
Jump to solution

power cli script not working in 5.5 \ was working prior

I am trying to perform actions on VMs that reside in a csv file.    Here is what I have:

connect-viserver $vcenter

foreach ($_.name in (Import-Csv "$ENV:USERPROFILE\Desktop\vm_list.csv" -UseCulture) ) {

$VM = Get-VM $_.name

Get-VM $VM | Where {$_.PowerState -eq "PoweredOn"} |

Select Name, Host, NumCpu, MemoryMB,

@{N="Cpu.UsageMhz.Average";E={[Math]::Round((($_ |Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddHours(-24)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}},

@{N="Mem.Usage.Average";E={[Math]::Round((($_ |Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-24)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}} `

| Export-Csv c:\Temp\stats.csv

}

NOTE - if I get rid of the whole "foreach" thing the script runs fine from Get-VM and down by removing the $VM variable)

But I want it to get the perf data for the VMs on my list, not just from all of them via the GET-VM against the entire vcEnter.

Any ideas why this is failing?   in 5.0 I ran a script that leveraged the "foreach" exactly as you see it above and it worked great.  Now that we are at 5.5 it is not longer working. 

Output showing errors that have stumped me:

At C:\Users\kwg\Desktop\test2.ps1:8 char:12

+ foreach ($_.name in (Import-Csv "$ENV:USERPROFILE\Desktop\vm_ip.csv"

-UseCulture ...

+            ~

Missing 'in' after variable in foreach loop.  

At C:\Users\kwg\Desktop\test2.ps1:8 char:83

+ ... " -UseCulture) ) {

+                    ~

Unexpected token ')' in expression or statement.

    + CategoryInfo          : ParserError: (:) [], ParseException

    + FullyQualifiedErrorId : MissingInInForeach

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Not sure why you are using the pipeline variable ($_) in the foreach loop ?

There is no need to use a ForEach loop in fact, you can pass multiple names to the Name parameter on the Get-VM cmdlet.

And then you use the pipeline to it's fullest.

I would do that as

connect-viserver $vcenter

Get-VM -Name (Import-Csv "$ENV:USERPROFILE\Desktop\vm_list.csv" -UseCulture | %{$_.Name}) |

Where {$_.PowerState -eq "PoweredOn"} |

Select Name, Host, NumCpu, MemoryMB,

@{N="Cpu.UsageMhz.Average";E={[Math]::Round((($_ |Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddHours(-24)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}},

@{N="Mem.Usage.Average";E={[Math]::Round((($_ |Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-24)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}} |

Export-Csv c:\Temp\stats.csv


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

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

Not sure why you are using the pipeline variable ($_) in the foreach loop ?

There is no need to use a ForEach loop in fact, you can pass multiple names to the Name parameter on the Get-VM cmdlet.

And then you use the pipeline to it's fullest.

I would do that as

connect-viserver $vcenter

Get-VM -Name (Import-Csv "$ENV:USERPROFILE\Desktop\vm_list.csv" -UseCulture | %{$_.Name}) |

Where {$_.PowerState -eq "PoweredOn"} |

Select Name, Host, NumCpu, MemoryMB,

@{N="Cpu.UsageMhz.Average";E={[Math]::Round((($_ |Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddHours(-24)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}},

@{N="Mem.Usage.Average";E={[Math]::Round((($_ |Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-24)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}} |

Export-Csv c:\Temp\stats.csv


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

0 Kudos
kwg66
Hot Shot
Hot Shot
Jump to solution

Thanks LucD.  Much appreciated, your solution worked first time without a single edit.   Very sweet.

You asked:   Not sure why you are using the pipeline variable ($_) in the foreach loop ?

My Answer:   Amateur lacking in the scripting logic required \ simpleton Smiley Happy 

For my knowledge, can you explain or point me to something that talks about this piece?:  | %{$_.Name}) |

This stumps me a bit..

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I see :smileygrin:

Sure, the Import-Csv cmdlet imports the CSV (duh).

It will place each line in the CSV, excluding the header line, as an object on the pipeline.

This object in the pipeline can be accessed through the $_ variable.

For each column in the CSV file, the object will have a property.

Name

VM1

VM2

For this specific CSV file the pipeline variable ($_) will be an object with one property, and that property is called Name

In the ForEach loop the script extracts the value of the Name property ($_.Name).

At the end we will have an array of string, and this array is what we pass on the Name parameter of the Get-VM cmdlet.


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

0 Kudos
kwg66
Hot Shot
Hot Shot
Jump to solution

thanks LucD.

I was just reading over your basics part 1 and 2 of the Get-stats CMD and am finding it useful.   The only question I have on it is the MaxSample parameter.   I'm curious to understand how this is best leveraged when seeking different time durations.   In the script as is posted it looks for the past 24 hours, and I am wondering about how MaxSamples (12) would come into play after extending the history to a month.  want to make sure I understand this correctly...  I would think that it refers to how many samples to use in the calculation, but the way the script is shown above am I to assume that within the 24 hour period there would only be 12 samples in the calculation?  that doesn't make sense.   vCenter rolls up a days worth of data into 5 min intervals right?   5 min \ 24 hours seems like way more samples than 12..   Please correct my logic as it seems wrong to me.. 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Your assumption is correct if the day coincides with the last 24 hours.

From day -1 onwards, there will be 30 minute intervals.

The Start/Finish are combined with MaxSamples.

MaxSamples indicates how many samples you want returned, independent if the specified Start-Finish hasn't been completely covered yet.

Also note that the collection starts at the Start timestamp.

By specifying MaxSamples the returned data might indeed stop way sooner than when the Finish timestamp is reached.


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

0 Kudos