PowerCLI

 View Only
Expand all | Collapse all

Can we pull the lists of VM having Memory reservation and CPU ?

  • 1.  Can we pull the lists of VM having Memory reservation and CPU ?

    Posted Dec 21, 2018 05:02 AM

    Hi Everyone,

    Can we pull the lists of VM having Memory reservation and CPU ? Also, how much Memory in GB having reservations and vCPU.

    thanks

    vmk



  • 2.  RE: Can we pull the lists of VM having Memory reservation and CPU ?

    Posted Dec 21, 2018 05:20 AM

    A quick small post to list the memory limits on the VM and set it back to unlimited using a PowerCLI

    To list the VM’s which have memory limit configured

        Get-VM | Get-VMResourceConfiguration | where {$_.MemLimitMB -ne ‘-1’}  | Select VM,MemLimitMB

    And to set it to unlimited

        Get-VM | Get-VMResourceConfiguration | where {$_.MemLimitMB -ne ‘-1’}  | Set-VMResourceConfiguration -MemLimitMB $null

    For CPU the attributes to be changed to CpuLimitMhz



  • 3.  RE: Can we pull the lists of VM having Memory reservation and CPU ?

    Posted Dec 21, 2018 05:25 AM

    Thank you. Since we have a large environment so trying to pull the lists for around 15 VM's which is having reservations.



  • 4.  RE: Can we pull the lists of VM having Memory reservation and CPU ?

    Posted Dec 21, 2018 06:14 AM

    Try like this

    Get-VM -PipelineVariable vm |

    Get-VMResourceConfiguration |

    where{$_.CpuReservationMhz -ne 0 -or $_.MemReservationMb -ne 0} |

    select @{N='VM';E={$vm.Name}},

        CpuReservationMhz,MemReservationGB



  • 5.  RE: Can we pull the lists of VM having Memory reservation and CPU ?

    Posted Dec 21, 2018 10:25 AM

    LucD,

    Can we pull reservation at Cluster and Total memory  reserved ( Sum)

    Thanks

    vmk



  • 6.  RE: Can we pull the lists of VM having Memory reservation and CPU ?
    Best Answer

    Posted Dec 21, 2018 10:34 AM

    Try like this

    Get-Cluster -PipelineVariable cluster |

    ForEach-Object -Process {

        $report = Get-VM -Location $cluster -PipelineVariable vm |

        Get-VMResourceConfiguration |

        where{$_.CpuReservationMhz -ne 0 -or $_.MemReservationMb -ne 0} |

        select @{N='Cluster';E={$cluster.Name}},

            @{N='VM';E={$vm.Name}},

            CpuReservationMhz,MemReservationGB

       

        $report += "" | Select @{N='Cluster';E={$cluster.Name}},

            @{N='VM';E={'Total'}},

            @{N='CpuReservationMhz';E={($report | Measure-Object -Property CpuReservationMhz -Sum).Sum}},

            @{N='MemReservationGB';E={($report | Measure-Object -Property MemReservationGB -Sum).Sum}}

        $report

    }



  • 7.  RE: Can we pull the lists of VM having Memory reservation and CPU ?

    Posted Dec 21, 2018 05:58 PM

    LucD,

    Sorry, to say some goof up happened, it's not picking the right cluster name. For every VM's showing in almost all the cluster though the VM is mapped to single cluster.

    Thanks

    vmk



  • 8.  RE: Can we pull the lists of VM having Memory reservation and CPU ?

    Posted Dec 21, 2018 06:32 PM

    Yes, you are right, I forgot the Location parameter on the Get-VM cmdlet.
    I updated the code above.



  • 9.  RE: Can we pull the lists of VM having Memory reservation and CPU ?

    Posted Dec 22, 2018 03:29 AM

    Ok LucD,

    I ran once again the scripts but it throws an error

    PS C:\temp> .\Memory_reservation.ps1

    Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.

    At C:\temp\Memory_reservation.ps1:23 char:5

    +     $report += "" | Select @{N='Cluster';E={$cluster.Name}},

    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException

        + FullyQualifiedErrorId : MethodNotFound

    Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.

    At C:\temp\Memory_reservation.ps1:23 char:5

    +     $report += "" | Select @{N='Cluster';E={$cluster.Name}},

    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException

        + FullyQualifiedErrorId : MethodNotFound

    Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.

    At C:\temp\Memory_reservation.ps1:23 char:5

    +     $report += "" | Select @{N='Cluster';E={$cluster.Name}},

    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException

        + FullyQualifiedErrorId : MethodNotFound

    thanks

    vmk



  • 10.  RE: Can we pull the lists of VM having Memory reservation and CPU ?

    Posted Dec 22, 2018 03:36 AM

    Even output are jumbled up.:smileysad:



  • 11.  RE: Can we pull the lists of VM having Memory reservation and CPU ?

    Posted Dec 22, 2018 09:00 AM

    Which PowerShell version are you using?

    Do a $PSVersionTable

    As an alternative, try like this

    Get-Cluster -PipelineVariable cluster |

    ForEach-Object -Process {

        $report = @()

        $report += Get-VM -Location $cluster -PipelineVariable vm |

        Get-VMResourceConfiguration |

        where{$_.CpuReservationMhz -ne 0 -or $_.MemReservationMb -ne 0} |

        select @{N='Cluster';E={$cluster.Name}},

            @{N='VM';E={$vm.Name}},

            CpuReservationMhz,MemReservationGB

        $report += "" | Select @{N='Cluster';E={$cluster.Name}},

            @{N='VM';E={'Total'}},

            @{N='CpuReservationMhz';E={($report | Measure-Object -Property CpuReservationMhz -Sum).Sum}},

            @{N='MemReservationGB';E={($report | Measure-Object -Property MemReservationGB -Sum).Sum}}

        $report

    }



  • 12.  RE: Can we pull the lists of VM having Memory reservation and CPU ?

    Posted Dec 23, 2018 05:57 AM

    LucD,

    I'm using power Cli version 11. Will check belwo script and update you.

    thanks

    vmk



  • 13.  RE: Can we pull the lists of VM having Memory reservation and CPU ?

    Posted Dec 23, 2018 07:00 AM

    I meant the PowerShell version (check by displaying what is in $PSVersionTable).



  • 14.  RE: Can we pull the lists of VM having Memory reservation and CPU ?

    Posted Dec 23, 2018 02:02 PM

    PS C:\> $PSVersionTable

    Name                           Value

    ----                           -----

    PSVersion                      5.1.14409.1018

    PSEdition                      Desktop

    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}

    BuildVersion                   10.0.14409.1018

    CLRVersion                     4.0.30319.42000

    WSManStackVersion              3.0

    PSRemotingProtocolVersion      2.3

    SerializationVersion           1.1.0.1



  • 15.  RE: Can we pull the lists of VM having Memory reservation and CPU ?

    Posted Dec 23, 2018 02:06 PM

    PS C:\> $PSVersionTable

    Name                           Value

    ----                           -----

    PSVersion                      5.1.14409.1018

    PSEdition                      Desktop

    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}

    BuildVersion                   10.0.14409.1018

    CLRVersion                     4.0.30319.42000

    WSManStackVersion              3.0

    PSRemotingProtocolVersion      2.3

    SerializationVersion           1.1.0.1



  • 16.  RE: Can we pull the lists of VM having Memory reservation and CPU ?

    Posted Dec 23, 2018 02:07 PM

    LucD,

    After running the alternative script, please find the output

    PS C:\temp> .\Memory_reservation.ps1

    Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.

    At C:\temp\Memory_reservation.ps1:22 char:5

    +     $report += "" | Select @{N='Cluster';E={$cluster.Name}},

    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException

        + FullyQualifiedErrorId : MethodNotFound

    Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.

    At C:\temp\Memory_reservation.ps1:22 char:5

    +     $report += "" | Select @{N='Cluster';E={$cluster.Name}},

    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException

        + FullyQualifiedErrorId : MethodNotFound

    Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.

    At C:\temp\Memory_reservation.ps1:22 char:5

    +     $report += "" | Select @{N='Cluster';E={$cluster.Name}},

    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException

        + FullyQualifiedErrorId : MethodNotFound



  • 17.  RE: Can we pull the lists of VM having Memory reservation and CPU ?

    Posted Dec 23, 2018 08:29 PM

    I made a small change to the latest version of the script.
    Can you give that one a try?



  • 18.  RE: Can we pull the lists of VM having Memory reservation and CPU ?

    Posted Dec 24, 2018 04:55 AM

    Yes, it's working now. No error this time.



  • 19.  RE: Can we pull the lists of VM having Memory reservation and CPU ?

    Posted Dec 24, 2018 06:40 AM

    I suspect it was due to the fact that you had a cluster with 1 VM that has reservations.
    That way the array $report became a scalar instead of an array, and then the += to add the Total wasn't working anymore.

    By changing the = on the Select to a +=, we made sure that $report stayed an array.



  • 20.  RE: Can we pull the lists of VM having Memory reservation and CPU ?

    Posted Dec 24, 2018 04:05 PM

    Thank you LucD !!