PowerCLI

 View Only
Expand all | Collapse all

Get CPU Memory Network Disk Usage

LucD

LucDJul 22, 2023 09:37 PM

  • 1.  Get CPU Memory Network Disk Usage

    Posted Jul 22, 2023 08:48 PM

    Hello

    I'm starting working on new script to Get CPU Memory Network Disk Usage. I'm starting with Average and I will add the Max & Min stats.

    My issue is that I'm not able to retreive ESXi name and not able to exclude vCLS from csv report.

     

    Below is the script 

     

    Get-VM | Where-Object {$_.PowerState -eq "PoweredOn" -notcontains "vCLS"} | Select-Object Name, Host, NumCpu, MemoryMB, `
    @{N="CPU Usage (Average), Mhz" ; E={[Math]::Round((($_ | Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddDays(-30) -IntervalMins 5 
        | Measure-Object Value -Average).Average),2)}}, `
    @{N="Memory Usage (Average), %" ; E={[Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 5 
        | Measure-Object Value -Average).Average),2)}} , `
    @{N="Network Usage (Average), KBps" ; E={[Math]::Round((($_ | Get-Stat -Stat net.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 5 
        | Measure-Object Value -Average).Average),2)}} , `
    @{N="Disk Usage (Average), KBps" ; E={[Math]::Round((($_ | Get-Stat -Stat disk.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 5 
        | Measure-Object Value -Average).Average),2)}} |`
    Export-Csv -Path .\AverageUsage.csv


  • 2.  RE: Get CPU Memory Network Disk Usage

    Posted Jul 22, 2023 09:28 PM

    Try like this.

    Btw, calling Get-Stat that many times is rather inefficient. Try to use 1 Get-Stat call and then Group-Object to split the results per VM.
    There are multiple examples of this concept in this community.

    Get-VM | Where-Object {$_.PowerState -eq "PoweredOn" -and $_.Name -notmatch "vCLS"} | 
    Select-Object Name, 
    @{N='Host';E={$_.VMHost.Name}},
    NumCpu, MemoryMB,
    @{N="CPU Usage (Average), Mhz" ; E={[Math]::Round((($_ | Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddDays(-30) -IntervalMins 5 | Measure-Object Value -Average).Average),2)}},
    @{N="Memory Usage (Average), %" ; E={[Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 5 | Measure-Object Value -Average).Average),2)}} ,
    @{N="Network Usage (Average), KBps" ; E={[Math]::Round((($_ | Get-Stat -Stat net.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 5 | Measure-Object Value -Average).Average),2)}} ,
    @{N="Disk Usage (Average), KBps" ; E={[Math]::Round((($_ | Get-Stat -Stat disk.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 5 | Measure-Object Value -Average).Average),2)}} |
    Export-Csv -Path .\AverageUsage.csv





  • 3.  RE: Get CPU Memory Network Disk Usage



  • 4.  RE: Get CPU Memory Network Disk Usage

    Posted Jul 22, 2023 09:37 PM

    Correct



  • 5.  RE: Get CPU Memory Network Disk Usage

    Posted Jul 22, 2023 09:53 PM

    I made it like this I'm getting error.
    without "| Select-Object Name, VMHost, NumCpu, CoresPerSocket, MemoryMB," no error



    $sStat = @{
        Stat = 'cpu.usage.average','mem.usage.average','net.usage.average','disk.usage.average'
        Start = (Get-Date).AddDays(-30)
        Instance = ''
        MaxSamples = [int]::MaxValue
        Entity = Get-VM | Where-Object {$_.PowerState -eq "PoweredOn" -and $_.Name -notmatch "vCLS"} | Select-Object Name, VMHost, NumCpu, CoresPerSocket, MemoryMB,`
        ErrorAction = 'SilentlyContinue'
    }
    Get-Stat  |
    Group-Object -Property {$_.Entity.Name} |
    ForEach-Object -Process {$_.Group | Group-Object -Property Timestamp |
        ForEach-Object -Process {
            New-Object -TypeName PSObject -Property ([ordered]@{
                VM = $_.Group[0].Entity.Name
                Timestamp = $_.Group[0].Timestamp
                CpuAvg = ($_.Group | Where-Object{$_.MetricId -eq 'cpu.usage.average'}).Value
                MemAvg = ($_.Group | Where-Object{$_.MetricId -eq 'mem.usage.average'}).Value
                DiskAvg = ($_.Group | Where-Object{$_.MetricId -eq 'disk.usage.average'}).Value
                NetAvg = ($_.Group | Where-Object{$_.MetricId -eq 'net.usage.average'}).Value
            })
        }
    } | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture

     



  • 6.  RE: Get CPU Memory Network Disk Usage

    Posted Jul 22, 2023 10:42 PM

    The Select-Object is not needed, just use the Get-VM and the Where-clause.



  • 7.  RE: Get CPU Memory Network Disk Usage

    Posted Jul 22, 2023 10:53 PM

    honestly didn't get you  

    you mean something like this?

    Entity = Get-VM | Where-Object {$_.PowerState -eq "PoweredOn" -and $_.Name -notmatch "vCLS"} Name, VMHost, NumCpu, CoresPerSocket, MemoryMB


  • 8.  RE: Get CPU Memory Network Disk Usage

    Posted Jul 22, 2023 11:04 PM

    No, like this

    $sStat = @{
        Stat = 'cpu.usage.average','mem.usage.average','net.usage.average','disk.usage.average'
        Start = (Get-Date).AddDays(-30)
        Instance = ''
        MaxSamples = [int]::MaxValue
        Entity = Get-VM | Where-Object {$_.PowerState -eq "PoweredOn" -and $_.Name -notmatch "vCLS"} 
        ErrorAction = 'SilentlyContinue'
    }


  • 9.  RE: Get CPU Memory Network Disk Usage

    Posted Jul 22, 2023 11:10 PM

    it didn't collect the below details in the csv report

    Name, VMHost, NumCpu, CoresPerSocket, MemoryMB



  • 10.  RE: Get CPU Memory Network Disk Usage

    Posted Jul 22, 2023 11:11 PM

    You have to add those to the New-Object hash table, use the Expression part of the Select-Objact calculated properties.



  • 11.  RE: Get CPU Memory Network Disk Usage

    Posted Jul 22, 2023 11:28 PM

    totaly lost  

    I beleive that I should add what I need in :

     

            New-Object -TypeName PSObject -Property ([ordered]@{
                VM = $_.Group[0].Entity.Name
                Timestamp = $_.Group[0].Timestamp
                Name = $Entity.Name
                'CpuAvg %' = ($_.Group | Where-Object{$_.MetricId -eq 'cpu.usage.average'}).Value
                'CPUReadyS milliseconde' = ($_.Group | Where-Object{$_.MetricId -eq 'cpu.ready.summation'}).Value
                'MemAvg %' = ($_.Group | Where-Object{$_.MetricId -eq 'mem.usage.average'}).Value
     
    Right?


  • 12.  RE: Get CPU Memory Network Disk Usage

    Posted Jul 22, 2023 11:39 PM

    Got It

    VMHost = $_.Group[0].Entity.VMHost


  • 13.  RE: Get CPU Memory Network Disk Usage

    Posted Jul 23, 2023 06:07 AM

    EVen better would be

    VMHost = $_.Group[0].Entity.VMHost.Name

    That way you don't rely on the automatic conversion from a VMHost object to a String



  • 14.  RE: Get CPU Memory Network Disk Usage

    Posted Jul 23, 2023 08:33 AM

    Thanks  it's working.

    I made it like this :

    $sStat = @{
        Stat = 'cpu.usage.average','cpu.ready.summation','mem.usage.average','net.usage.average'
        Start = (Get-Date).AddDays(-30)
        Instance = ''
        MaxSamples = [int]::MaxValue
        Entity = Get-VM | Where-Object {$_.PowerState -eq "PoweredOn" -and $_.Name -notmatch "vCLS"}
        ErrorAction = 'SilentlyContinue'
    }
    Get-Stat @sStat |
    Group-Object -Property {$_.Entity.Name} |
    ForEach-Object -Process {$_.Group | Group-Object -Property Timestamp |
        ForEach-Object -Process {
            New-Object -TypeName PSObject -Property ([ordered]@{
                VM = $_.Group[0].Entity.Name
                Timestamp = $_.Group[0].Timestamp
                VMHost = $_.Group[0].Entity.VMHost.Name
                NumCpu = $_.Group[0].Entity.NumCPU
                CoresPerSocket = $_.Group[0].Entity.CoresPerSocket
                MemoryMB = $_.Group[0].Entity.MemoryMB
                'CpuAvg %' = ($_.Group | Where-Object{$_.MetricId -eq 'cpu.usage.average'}).Value
                'CPUReadyS milliseconde' = ($_.Group | Where-Object{$_.MetricId -eq 'cpu.ready.summation'}).Value
                'MemAvg %' = ($_.Group | Where-Object{$_.MetricId -eq 'mem.usage.average'}).Value
                'NetAvg KBps' = ($_.Group | Where-Object{$_.MetricId -eq 'net.usage.average'}).Value
            })
        }
    } | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture

     

    A quick question, do you have any idea to get the VMHost without FQDN?

    Also I'm not able to find the best way to show  'cpu.ready.summation' in secondes



  • 15.  RE: Get CPU Memory Network Disk Usage
    Best Answer

    Posted Jul 23, 2023 08:49 AM

    Try with

     

    VMHost = $_.Group[0].Entity.VMHost.Name.Split('.')[0]

     

    Since the Value of cpu.ready.summation is in milliseconds, you have to divide by 1000.

     

                'CPUReadyS milliseconde' = [Math]:Round((($_.Group | Where-Object{$_.MetricId -eq 'cpu.ready.summation'}).Value/1000),1)
    

     



  • 16.  RE: Get CPU Memory Network Disk Usage

    Posted Jul 23, 2023 09:06 AM

    Thank you  it's working fine

    regarding there's a Typo issue in Math  

    'CPUReadyS milliseconde' = [Math}:Round((($_.Group | Where-Object{$_.MetricId -eq 'cpu.ready.summation'}).Value/1000),1)

     

    'CPUReadyS Secondes' = [Math]::Round((($_.Group | Where-Object {$_.MetricId -eq 'cpu.ready.summation'}).Value/1000),1)

     

    Many thanks