Hi ALL ,
I am new to powercli and trying to create a script to list all the clusters in a datacentre and all hosts within a cluster and to calculate min , max and avg cpu and ram utilization per host and per cluster . So far I have tried the below but I am unable to publish the results out from my script.
$Function =@()
ForEach ($DataCenter in Get-Datacenter)
{
ForEach ($cluster in ($DataCenter | Get-Cluster ) ) ------------------------------------------------ Need help to publish the information here and to confirm if it is correct or not .
{
ForEach ($hosts in ($cluster | Get-VMHost))
{
ForEach ($vms in ($hosts | Get-VM)) ----------- Not sure if I am calling the functions here correctly
{
$allvms = @()
$allhosts = @()
$hosts = Get-VMHost
$vms = Get-Vm
foreach($vms in $hosts){
$hoststat = "" | Select HostName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin
$hoststat.HostName = $vmHost.name
$statcpu = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-30) -Finish (Get-Date)-MaxSamples 10000 -stat cpu.usage.average
$statmem = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-30) -Finish (Get-Date)-MaxSamples 10000 -stat mem.usage.average
$cpu = $statcpu | Measure-Object -Property value -Average -Maximum -Minimum
$mem = $statmem | Measure-Object -Property value -Average -Maximum -Minimum
$hoststat.CPUMax = $cpu.Maximum
$hoststat.CPUAvg = $cpu.Average
$hoststat.CPUMin = $cpu.Minimum
$hoststat.MemMax = $mem.Maximum
$hoststat.MemAvg = $mem.Average
$hoststat.MemMin = $mem.Minimum
$allhosts += $hoststat
}
}
}
}
}
$Function | Select HostName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin | Export-Csv "c:\Function.csv" -noTypeInformation
Any help on this is much appreciated.
The [ordered] cast was introduced in PowerShell v3.
For PowerShell v2, you could use
$vms = Get-VM
$stat = 'cpu.usage.average','mem.usage.average'
$start = (Get-Date).AddDays(-31)
$report = Get-Stat -Entity $vms -Start $start -Stat $stat -ErrorAction SilentlyContinue |
Group-Object -Property {$_.Entity.Name} | %{
$cpu = $_.Group | where{$_.MetricId -eq 'cpu.usage.average'} | Measure-Object -Property Value -Average -Maximum -Minimum
$mem = $_.Group | where{$_.MetricId -eq 'mem.usage.average'} | Measure-Object -Property Value -Average -Maximum -Minimum
New-Object PSObject -Property @{
Datacenter = Get-Datacenter -VM $_.Group[0].Entity | Select -ExpandProperty Name
Cluster = Get-Cluster -VM $_.Group[0].Entity | Select -ExpandProperty Name
VMHost = $_.Group[0].Entity.Host.Name
Name = $_.Group[0].Entity.Name
CpuMin = $cpu.Minimum
CpuAvg = $cpu.Average
CpuMax = $cpu.Maximum
MemMin = $mem.Minimum
MemAvg = $mem.Average
MemMax = $mem.Maximum
}
}
$report | Sort-Object -Property Datacenter,Cluster,VMHost,Name |
Export-Csv report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
$Function =@()
ForEach ($DataCenter in Get-Datacenter)
{
write-host $DataCenter
ForEach ($cluster in ($DataCenter | Get-Cluster ) )
{
write-host $cluster
ForEach ($hosts in ($cluster | Get-VMHost))
{
Write-Host $hosts
ForEach ($vms in ($hosts | Get-VM))
{
write-host $vms
}
}
}
}
$Function
This bit works if you run it. You'll get each datacenter, and within it each cluster, each host, and each vm. Haven't tried your statistics function.
However:
If you're after the hosts, don't get the VM's (and don't get the hosts and VM's a second time either). At least try to break up your script in parts that you understand. Try to get what you want with a single machine.
Hope it helps a little
Try like this
$vms = Get-VM
$stat = 'cpu.usage.average','mem.usage.average'
$start = (Get-Date).AddDays(-31)
$report = Get-Stat -Entity $vms -Start $start -Stat $stat -ErrorAction SilentlyContinue |
Group-Object -Property {$_.Entity.Name} | %{
$cpu = $_.Group | where{$_.MetricId -eq 'cpu.usage.average'} | Measure-Object -Property Value -Average -Maximum -Minimum
$mem = $_.Group | where{$_.MetricId -eq 'mem.usage.average'} | Measure-Object -Property Value -Average -Maximum -Minimum
$rec = [ordered] @{
Datacenter = Get-Datacenter -VM $_.Group[0].Entity | Select -ExpandProperty Name
Cluster = Get-Cluster -VM $_.Group[0].Entity | Select -ExpandProperty Name
VMHost = $_.Group[0].Entity.Host.Name
Name = $_.Group[0].Entity.Name
CpuMin = $cpu.Minimum
CpuAvg = $cpu.Average
CpuMax = $cpu.Maximum
MemMin = $mem.Minimum
MemAvg = $mem.Average
MemMax = $mem.Maximum
}
New-Object PSObject -Property $rec
}
$report | Sort-Object -Property Datacenter,Cluster,VMHost,Name |
Export-Csv report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc,
I think he wants host utilization, not VM utilization. By the way, how do you get those colors in the post like they are in the ISE?
I use an ISE addon for that, see Windows PowerShell V3 ISE: Copy As HTML Add-On
Switch the VMTN editor to HTML and then copy
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Fantastic, thanks!
Hi LucD ,
Thanks for replying back . I will run the script and share the results.
Thanks again for your great help.
Hi Akos ,
Thanks for your reply and I will share the results once its finished .
Thanks.
Hi Lucd,
The script failed and return this result.
+ New-Object PSObject -Property <<<< $rec
+ CategoryInfo : InvalidData: (:) [New-Object], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.NewOb
jectCommand
Unable to find type [ordered]: make sure that the assembly containing this type is loaded.
At line:5 char:21
+ $rec = [ordered] <<<< @{
+ CategoryInfo : InvalidOperation: (ordered:String) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
New-Object : Cannot validate argument on parameter 'Property'. The argument is null or empty. Suppl
y an argument that is not null or empty and then try the command again.
At line:17 char:33
+ New-Object PSObject -Property <<<< $rec
+ CategoryInfo : InvalidData: (:) [New-Object], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.NewOb
jectCommand
can we do something about it ?.
Thanks.
Which PowerShell version are you using ?
You can check by displaying the content of $PSVersionTable
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Lucd,
Here is the version details.
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> $PSVersionTable
Name Value
---- -----
CLRVersion 2.0.50727.5485
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
The [ordered] cast was introduced in PowerShell v3.
For PowerShell v2, you could use
$vms = Get-VM
$stat = 'cpu.usage.average','mem.usage.average'
$start = (Get-Date).AddDays(-31)
$report = Get-Stat -Entity $vms -Start $start -Stat $stat -ErrorAction SilentlyContinue |
Group-Object -Property {$_.Entity.Name} | %{
$cpu = $_.Group | where{$_.MetricId -eq 'cpu.usage.average'} | Measure-Object -Property Value -Average -Maximum -Minimum
$mem = $_.Group | where{$_.MetricId -eq 'mem.usage.average'} | Measure-Object -Property Value -Average -Maximum -Minimum
New-Object PSObject -Property @{
Datacenter = Get-Datacenter -VM $_.Group[0].Entity | Select -ExpandProperty Name
Cluster = Get-Cluster -VM $_.Group[0].Entity | Select -ExpandProperty Name
VMHost = $_.Group[0].Entity.Host.Name
Name = $_.Group[0].Entity.Name
CpuMin = $cpu.Minimum
CpuAvg = $cpu.Average
CpuMax = $cpu.Maximum
MemMin = $mem.Minimum
MemAvg = $mem.Average
MemMax = $mem.Maximum
}
}
$report | Sort-Object -Property Datacenter,Cluster,VMHost,Name |
Export-Csv report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference