PowerCLI

 View Only
  • 1.  Get VM Stats outputs by different criterias

    Posted May 11, 2021 05:50 PM

    Hi there, first thanks if you are reading this and are trying to help.
    I'm trying to write a script which generates the outputs shown below in the tabels.
    I want to grab following info's in the first table: 

    VM Count / Used Memory / Used Cores / allocated Discsapce

    First I try to get the infos on datacenter level as we have 4 different datacenters. 

    Summary by Datacenter

     

    Datacenter:

    Servers:

    Memory:

    Cores:

    Disc allocated (GB):

    PROD

    2590

    114091

    16988

    333550

    TEST

    317

    7680

    1040

    21416

    INT

    215

    3093

    860

    42441

    DEV

    64

    2768

    414

    17864

     

    Here I would like to get the stats from all the VMs which are running on all clusters.
    Additionally I wanted to make a summary of all blades which are running in all clusters.

    Summary by Type

    Type:

    Servers:

    Memory:

    Cores:

    Disc allocated (GB):

    Normal VMs

    2919

    38710

    10972

    419247

    ESXi Hosts

    202

    89543

    6804

    0

     

    In the last table im trying to output the same infos from all vms as in the last tabels. But this time sorted by operating system.

    Summary by Operatingsystem

    Operatingsystem:

    Servers:

    Memory:

    Cores:

    Disc allocated (GB):

    Windows

    1760

    21674

    7708

    227709

    RedHat

    461

    14876

    2897

    89155

    Others

    201

    1245

    5098

    89155

     

    In the moment i have this script so far: 


    Connect-VIServer -Server ****

    $totalvm = 0
    $totalstorage = 0
    $totalsockets = 0
    $totalcorepersocket = 0
    $totalcores = 0
    $totalmemory = 0

    Foreach ($vm in (Get-VM)) {


    $totalstorage += $vm.UsedSpaceGB
    $totalsockets += $vm.CoresPerSocket
    $totalcorepersocket += $vm.NumCpu
    $totalmemory += $vm.MemoryGB


    }
    $totalcores += $totalsockets * $totalcorepersocket
    $totalvm= (Get-VM).count


    $allexport= 0
    $allexport = @(

    [pscustomobject]@{TotalVMCount=$totalvm;Memory=$totalmemory;Cores=$totalcorepersocket;DiscallocatedGB=[math]::Round($totalstorage)}

    )


    $allexport | Out-File C:\scripts\LegacyReporting\allexport.txt


    #____________Windows ________
    $wintotalvm = 0
    $wintotalstorage = 0
    $wintotalsockets = 0
    $wintotalcorepersocket = 0
    $wintotalcores = 0
    $wintotalmemory = 0

    Foreach ($winvm in (Get-VM).where{$_.Guest.OSFullName -match 'Windows'}) {

    Write-Host $winvm
    $wintotalstorage += $winvm.UsedSpaceGB
    $wintotalsockets += $winvm.CoresPerSocket
    $wintotalcorepersocket += $winvm.NumCpu
    $wintotalmemory += $winvm.MemoryGB


    }

    $wintotalcores += $wintotalsockets * $wintotalcorepersocket
    $wintotalvm= ((Get-VM).where{$_.Guest.OSFullName -match 'Windows'}).count


    $winexport = 0
    $winexport = @(

    [pscustomobject]@{TotalVMCount=$wintotalvm;Memory=$wintotalmemory;Cores=$wintotalcorepersocket;DiscallocatedGB=[math]::Round($wintotalstorage)}

    )

    $winexport | Out-File C:\scripts\LegacyReporting\winswisstxttestexport.txt

    #____________Linux________

    $lintotalvm = 0
    $lintotalstorage = 0
    $lintotalsockets = 0
    $lintotalcorepersocket = 0
    $lintotalcores = 0
    $lintotalmemory = 0

    Foreach ($linvm in (Get-VM).where{$_.Guest.OSFullName -match 'Centos'}) {

    Write-Host $linvm
    $lintotalstorage += $winvm.UsedSpaceGB
    $lintotalsockets += $winvm.CoresPerSocket
    $lintotalcorepersocket += $linvm.NumCpu
    $lintotalmemory += $linvm.MemoryGB


    }

    $lintotalcores += $lintotalsockets * $lintotalcorepersocket
    $lintotalvm= ((Get-VM).where{$_.Guest.OSFullName -match 'Centos'}).count


    $linexport = 0
    $linexport = @(

    [pscustomobject]@{TotalVMCount=$lintotalvm;Memory=$lintotalmemory;Cores=$lintotalcorepersocket;DiscallocatedGB=[math]::Round($lintotalstorage)}

    )

    $linexport | Out-File C:\scripts\LegacyReporting\linswisstxttestexport.txt

    #>

    #____________Other________

    $ototalvm = 0
    $ototalstorage = 0
    $ototalsockets = 0
    $ototalcorepersocket = 0
    $ototalcores = 0
    $ototalmemory = 0

    Foreach ($ovm in (Get-VM).where{$_.Guest.OSFullName -match 'Other'}) {

    Write-Host $ovm
    $ototalstorage += $ovm.UsedSpaceGB
    $ototalsockets += $ovm.CoresPerSocket
    $ototalcorepersocket += $ovm.NumCpu
    $ototalmemory += $ovm.MemoryGB


    }

    $ototalcores += $ototalsockets * $ototalcorepersocket
    $ototalvm= ((Get-VM).where{$_.Guest.OSFullName -match 'Other'}).count


    $oexport = 0
    $oexport = @(

    [pscustomobject]@{TotalVMCount=$ototalvm;Memory=$ototalmemory;Cores=$ototalcorepersocket;DiscallocatedGB=[math]::Round($ototalstorage)}

    )

    $oexport | Out-File C:\scripts\LegacyReporting\otestexport.txt

     

    I was able to "find out" how to get different stats by operating system but I dont think this is the slimmest way by any chance.
    For the table summary by datacenter, I can't wrap my head around a script which would result in the output needed.

    Does anyone one have any suggestions for me how I could solve my situation?

    Thanks a lot



  • 2.  RE: Get VM Stats outputs by different criterias

    Posted May 11, 2021 06:02 PM

    You are looping over all the VMs in your Center with

    Foreach ($vm in (Get-VM)) {
    
    }

     

    You could add an additional loop over all Datacenters

    foreach ($dc in Get-Datacenter) {
        foreach($vm in Get-VM -Location $dc){
            
        }
    }

     Then you could get summaries per Datacenter.