VMware Cloud Community
VJDGiant
Contributor
Contributor

Please fix this code issue. vcpu is throwing error.

Import-Module VMware.VimAutomation.Core
$Vcenter= "vcenter1"

foreach ($vmhost in $Vcenter)
{
Connect-VIServer $Vmhost
$esxhost = Get-vmhost | Select Name,
@{N="CPU Cores Total";E={[math]::Round($_.NumCpu)}},
@{N="vCPUs Provisioned";E={[math]::Round($HostvCPU | foreach ($vms in ($esxhost | get-vm)){$HostvCPU+=$vms.Numcpu})}}, `
@{N="CPU Average Usage %" ; E={[Math]::Round((($_ | Get-Stat -Stat cpu.usage.average -Start (Get-Date).AddDays(-7) -IntervalMins 5 | Measure-Object Value -Average).Average),2)}}, `
@{N='Memory Total Capacity GB';E={[math]::Round($_.MemoryTotalGB,2)}}, `
@{N="Memory Provisioned (Average) GB" ; E={[Math]::Round(((($_ | Get-Stat -Stat mem.consumed.average -Start (Get-Date).AddDays(-7) -IntervalMins 5 | Measure-Object Value -Average).Average))/(1024*1024),2)}} , `
@{N="Memory Active (Average) GB" ; E={[Math]::Round(((($_ | Get-Stat -Stat mem.active.average -Start (Get-Date).AddDays(-7) -IntervalMins 5 | Measure-Object Value -Average).Average))/(1024*1024),2)}} |
Export-Csv .\report.csv -NoTypeInformation -UseCulture
}

Moderator edit by wila: Moved post from VMware code discussions to PowerCLI discussions

Labels (1)
0 Kudos
8 Replies
LucD
Leadership
Leadership

It would be helpful if you showed the error message


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

0 Kudos
VJDGiant
Contributor
Contributor

Hi LucD,

 

The problem is with this line.

@{N="vCPUs Provisioned";E={[math]::Round($HostvCPU | foreach ($vms in ($esxhost | get-vm)){$HostvCPU+=$vms.Numcpu})}}, `

We need total vCPU count for all VMs running on each ESX host. Everything else is working fine.

 

Thanks,

Vijay

0 Kudos
LucD
Leadership
Leadership

But what is the error you mentioned?


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

Tags (1)
0 Kudos
VJDGiant
Contributor
Contributor

Syntax error. I am not able to include 'for each' statement correctly inside expression. 

0 Kudos
LucD
Leadership
Leadership

Really?
I just ask for the full error message


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

0 Kudos
VJDGiant
Contributor
Contributor

Sorry, here it is.

 

At C:\Scripts\Weekly Report_2.ps1:9 char:51
+ @{N="vCPUs Provisioned";E={[math]::Round($HostvCPU | foreach ($vms in ...
+ ~
Missing ')' in method call.
At C:\Scripts\Weekly Report_2.ps1:9 char:68
+ ... Us Provisioned";E={[math]::Round($HostvCPU | foreach ($vms in ($esxho ...
+ ~~
Unexpected token 'in' in expression or statement.
At C:\Scripts\Weekly Report_2.ps1:9 char:67
+ ... vCPUs Provisioned";E={[math]::Round($HostvCPU | foreach ($vms in ($es ...
+ ~
Missing closing ')' in expression.
At C:\Scripts\Weekly Report_2.ps1:9 char:27
+ @{N="vCPUs Provisioned";E={[math]::Round($HostvCPU | foreach ($vms in ...
+ ~
Missing closing '}' in statement block or type definition.
At C:\Scripts\Weekly Report_2.ps1:9 char:90
+ ... math]::Round($HostvCPU | foreach ($vms in ($esxhost | get-vm)){$Hostv ...
+ ~
The hash literal was incomplete.
At C:\Scripts\Weekly Report_2.ps1:5 char:1
+ {
+ ~
Missing closing '}' in statement block or type definition.
At C:\Scripts\Weekly Report_2.ps1:9 char:90
+ ... ath]::Round($HostvCPU | foreach ($vms in ($esxhost | get-vm)){$HostvC ...
+ ~
Unexpected token ')' in expression or statement.
At C:\Scripts\Weekly Report_2.ps1:9 char:115
+ ... | foreach ($vms in ($esxhost | get-vm)){$HostvCPU+=$vms.Numcpu})}}, `
+ ~
Unexpected token ')' in expression or statement.
At C:\Scripts\Weekly Report_2.ps1:9 char:116
+ ... | foreach ($vms in ($esxhost | get-vm)){$HostvCPU+=$vms.Numcpu})}}, `
+ ~
Unexpected token '}' in expression or statement.
At C:\Scripts\Weekly Report_2.ps1:9 char:117
+ ... | foreach ($vms in ($esxhost | get-vm)){$HostvCPU+=$vms.Numcpu})}}, `
+ ~
Unexpected token '}' in expression or statement.
Not all parse errors were reported. Correct the reported errors and try again.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndParenthesisInMethodCall

0 Kudos
LucD
Leadership
Leadership

I'm not sure where you got that code from, but there are a few issues with it.

Try something like this instead

Import-Module VMware.VimAutomation.Core
$Vcenter= "vcenter1"
Connect-VIServer $Vcenter

Get-VMHost |
Select Name,
@{N="CPU Cores Total";E={[math]::Round($_.NumCpu)}},
@{N="vCPUs Provisioned";E={[math]::Round(((Get-VM -Location $_).NumCpu | Measure-Object -Sum).Sum)}},
@{N="CPU Average Usage %" ; E={[Math]::Round((($_ | Get-Stat -Stat cpu.usage.average -Start (Get-Date).AddDays(-7) -IntervalMins 5 | Measure-Object Value -Average).Average),2)}},
@{N='Memory Total Capacity GB';E={[math]::Round($_.MemoryTotalGB,2)}},
@{N="Memory Provisioned (Average) GB" ; E={[Math]::Round(((($_ | Get-Stat -Stat mem.consumed.average -Start (Get-Date).AddDays(-7) -IntervalMins 5 | Measure-Object Value -Average).Average))/(1024*1024),2)}} ,
@{N="Memory Active (Average) GB" ; E={[Math]::Round(((($_ | Get-Stat -Stat mem.active.average -Start (Get-Date).AddDays(-7) -IntervalMins 5 | Measure-Object Value -Average).Average))/(1024*1024),2)}} |
Export-Csv .\report.csv -NoTypeInformation -UseCulture


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

0 Kudos
VJDGiant
Contributor
Contributor

Thanks LucD. It works correctly now.

0 Kudos