I am trying to update my reporting script so that it filter the statistics that come back to just 5am - 7pm M-F but i cant figure out how.
cls $Global:ErrorActionPreference="SilentlyContinue" $vctr = @("ap-vmvc-p001","ap-vmvc-p01") $output = @() Foreach ($vc in $vctr) { Connect-VIServer $VC $RootFolders = Get-Folder datacenters \ | Get-Folder -NoRecursion \ | Sort-Object $_.name Foreach ($RF in $RootFolders){
$CurrentRootFolder = $RF \ | Get-Folder -NoRecursion \ | Sort-Object $_.name
Foreach ($SF in $CurrentRootFolder){
"`n" + $RF.Name + "`t" + $sf.Name trap{"`tNo Clusters";continue} $Clusters = $SF \ | Get-Cluster trap $clusters = $clusters \ | Sort-Object $_.name
Foreach ( $Cluster in $Clusters){ write-host $cluster.name
$out = "" \ | select-object Vcenter, Site, Farm, Cluster, HostCount, VMCount, CpuTotalGhz, CpuAvg, CpuMax, CpuMin, MemTotalGB, MemAvg, MemMax, MemMin; $out.HostCount = ($cluster \ | Get-view ).summary.Numhosts $out.VMCount = ($cluster \ | Get-Vm).count
if ($out.HostCount -gt 0 -and $out.VMCount -gt 0) { $out.Vcenter = $VC $out.Site = $RF.Name $out.Farm = $SF.name $out.Cluster = $cluster.Name
$Out.CpuTotalGhz= ($cluster \ | Get-view ).summary.totalcpu / 1000 | Get-view ).summary.totalmemory/1024/1024/1024,2)
$statCPU = Get-stat -entity $cluster -stat cpu.usagemhz.average -start (get-date).adddays(-30) \ | where { $_.value -gt 3} $statMem = Get-stat -entity $cluster -stat mem.usage.average -start (get-date).adddays(-30) \ | where { $_.value -gt 3} | measure-object -property value -min ).minimum/1000)/$out.cputotalGhz)*100,2) | measure-object -property value -max ).maximum/1000)/$out.cputotalGhz)*100,2) | measure-object -property value -average ).average/1000)/$out.cputotalGhz)*100,2) | measure-object -property value -max ).maximum ,2) | measure-object -property value -min ).minimum,2) | measure-object -property value -average ).average,2)
if ($out.CpuMin -lt 0) {$out.CpuMin = 0} if ($out.MemMin -lt 0) {$out.MemMin = 0} if ($out.VMCount -eq "") {$out.VMCount = 0}
$out $output = $output + $out } } } } } $output \ | Sort-object Site, Farm, CLuster\ | Export-Csv -Force -noTypeInformation c:\Cluster_Utilization_30day.csv |
Truth is a three edged sword
The Get-Stat cmdlet doesn't allow to specify non-continuous time ranges.
What you need to do is to filter out the statistical data that has a time stamp outside the window you want to cover.
This can be done with the Where-Object cmdlet.
If you change the lines containing the Get-Stat cmdlet like below it should only give you the statistical data inside the 05:00-19:00 window.
... $statCPU = Get-stat -entity $cluster -stat cpu.usagemhz.average -start (*get-date*).adddays(-30) | where { $_.value -gt 3} | where {(5..18) -contains $_.Timestamp.Hour} $statMem = Get-stat -entity $cluster -stat mem.usage.average -start (*get-date*).adddays(-30) | where { $_.value -gt 3} | where {(5..18) -contains $_.Timestamp.Hour} ...
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference