1 2 Previous Next 17 Replies Latest reply on Aug 13, 2019 7:02 AM by wfrancoti

    Script to get VM Maximum CPU and Memory

    wfrancoti Novice

      Hello everyone,

       

      I am running a project to create a specific cluster for Windows 2008 Servers of my environment. To rightsize the cluster once I don´t have any capacity performance tool installed I am looking for who could help me to modify the script below to get maximum cpu and memory ONLY for my windows 2008 servers into a specific vcenter and the export to csv that contain DataCenter name and Cluster Name.

       

      $clusterName = 'cluster'

      $now = Get-Date

      $start = $now.AddDays(-1)

      $stat = 'cpu.usagemhz.average', 'mem.usage.average'

       

      $vms = Get-cluster -Name $clusterName | Get-VM

       

      Get-Stat -Entity $vms -Stat $stat -Start $start -Instance '*' -ErrorAction SilentlyContinue |

      Group-Object -Property { $_.Entity.Name } |

      ForEach-Object -Process {

         $maxCpu = $_.Group | where { $_.MetricId -eq 'cpu.usagemhz.average' } |

         Sort-Object -Property value -Descending | select -first 1

         $maxMem = $_.Group | where { $_.MetricId -eq 'mem.usage.average' } |

         Sort-Object -Property value -Descending | select -first 1

         New-Object PSObject -Property ([ordered]@{

         VM = $_.Name

         StartInterval = $start

         EndInterval = $now

         MaxCpuTimestamp = $maxCpu.Timestamp

         PeakCpuMhz = [math]::Round($maxCpu.Value, 1)

         MaxMemTimestamp = $maxMem.Timestamp

         PeakMemGB = [math]::Round($maxMem.Entity.MemoryGB * $maxMem.Value / 100, 1)

         })

      }

       

       

      Thanks.

        • 1. Re: Script to get VM Maximum CPU and Memory
          LucD Guru
          Community WarriorsUser ModeratorsvExpert

          Try something like this.

          Make sure to connect to the desired vCenter before running the script.

           

          $now = Get-Date

          $start = $now.AddDays(-1)

          $stat = 'cpu.usage.average', 'mem.usage.average'

          $vms = Get-VM | where { $_.ExtensionData.Config.GuestFullName -match "Windows Server 2008" }

          Get-Stat -Entity $vms -Stat $stat -Start $start -Instance '*' -ErrorAction SilentlyContinue |

          Group-Object -Property { $_.Entity.Name } |

          ForEach-Object -Process {

             $maxCpu = $_.Group | where { $_.MetricId -eq 'cpu.usage.average' } |

             Sort-Object -Property value -Descending | select -first 1

             $maxMem = $_.Group | where { $_.MetricId -eq 'mem.usage.average' } |

             Sort-Object -Property value -Descending | select -first 1

             New-Object PSObject -Property ([ordered]@{

             Datacenter = (Get-Datacenter -VM $_.Name).Name

             Cluster = (Get-Cluster -VM $_.Name).Name

             VM = $_.Name

             StartInterval = $start

             EndInterval = $now

             MaxCpuTimestamp = $maxCpu.Timestamp

             PeakCpuMhz = [math]::Round($maxCpu.Value, 1)

             MaxMemTimestamp = $maxMem.Timestamp

             PeakMemGB = [math]::Round($maxMem.Entity.MemoryGB * $maxMem.Value / 100, 1)

             })

          } |

          Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture

          • 2. Re: Script to get VM Maximum CPU and Memory
            wfrancoti Novice

            Hello LucD, thanks for answer. I ran the script but for some reason that I could not identify the report is being created in blank.

            Yes, I am connected to desired vcenter server.

            Thanks.

            • 3. Re: Script to get VM Maximum CPU and Memory
              LucD Guru
              User ModeratorsCommunity WarriorsvExpert

              Does this return any VMs?


              Get-VM | where { $_.ExtensionData.Config.GuestFullName -match "Windows Server 2008" }

              • 4. Re: Script to get VM Maximum CPU and Memory
                wfrancoti Novice

                Yes, it is returning

                • 5. Re: Script to get VM Maximum CPU and Memory
                  LucD Guru
                  User ModeratorsCommunity WarriorsvExpert

                  Ok, the next step is to verify that the following actually returns statistical data.

                  Make sure that the $start and $stat variables are set correctly before.

                   

                  $vms = Get-VM | where { $_.ExtensionData.Config.GuestFullName -match "Windows Server 2008" }

                  Get-Stat -Entity $vms -Stat $stat -Start $start -Instance '*' -ErrorAction SilentlyContinue

                  • 6. Re: Script to get VM Maximum CPU and Memory
                    wfrancoti Novice

                    It returned the statistical data it seems everything is OK but for some reason now it is not creating the file, till I understand the file is created when we started the script, right? Or Should I need wait a time to have it created in the directory?

                     

                    Here is the script I am running. As you can see I just changed the start variable from day to months.

                     

                    $now = Get-Date

                    $start = $now.AddMonths(-3)

                    $stat = 'cpu.usage.average', 'mem.usage.average'

                    $vms = Get-VM | where { $_.ExtensionData.Config.GuestFullName -match "Windows Server 2008" }

                    Get-Stat -Entity $vms -Stat $stat -Start $start -Instance '*' -ErrorAction SilentlyContinue |

                    Group-Object -Property { $_.Entity.Name } |

                    ForEach-Object -Process {

                       $maxCpu = $_.Group | where { $_.MetricId -eq 'cpu.usage.average' } |

                       Sort-Object -Property value -Descending | select -first 1

                       $maxMem = $_.Group | where { $_.MetricId -eq 'mem.usage.average' } |

                       Sort-Object -Property value -Descending | select -first 1

                       New-Object PSObject -Property ([ordered]@{

                       Datacenter = (Get-Datacenter -VM $_.Name).Name

                       Cluster = (Get-Cluster -VM $_.Name).Name

                       VM = $_.Name

                       StartInterval = $start

                       EndInterval = $now

                       MaxCpuTimestamp = $maxCpu.Timestamp

                       PeakCpuMhz = [math]::Round($maxCpu.Value, 1)

                       MaxMemTimestamp = $maxMem.Timestamp

                       PeakMemGB = [math]::Round($maxMem.Entity.MemoryGB * $maxMem.Value / 100, 1)

                       })

                    } |

                    Export-Csv -Path C:\temp\report.csv -NoTypeInformation -UseCulture

                     

                    Thanks

                    • 7. Re: Script to get VM Maximum CPU and Memory
                      LucD Guru
                      User ModeratorsvExpertCommunity Warriors

                      The file should get created the moment you run the script.

                      The file will be empty, until there is statistical data to be written to it, at the end of the ForEach loop.

                      Is the file not there at all, or is it empty?

                      • 8. Re: Script to get VM Maximum CPU and Memory
                        wfrancoti Novice

                        It is not there.

                        • 9. Re: Script to get VM Maximum CPU and Memory
                          LucD Guru
                          User ModeratorsvExpertCommunity Warriors

                          I just ran your script, and I get a file with the results in there.


                          How did you run the script? From a .ps1 file?
                          If yes, can you attach the file?

                          Were there any errors when you ran the script?
                          You are connected to a vCenter I assume?

                          • 10. Re: Script to get VM Maximum CPU and Memory
                            wfrancoti Novice

                            Yes, I am connected in the vcenter server and yes I am running it from a .ps1. no errors on the screen.

                            • 11. Re: Script to get VM Maximum CPU and Memory
                              LucD Guru
                              User ModeratorsCommunity WarriorsvExpert

                              I just ran your attached script, and it works perfectly for me.

                              Even if there is no statistical data for the VMs selected, it should create an empty file.

                              If you are not allowed to create a file in that location, you should have gotten an error message saying that.

                               

                              So the following doesn't return anything?

                               

                              Get-ChildItem -Path C:\Temp -Filter *.csv

                              • 12. Re: Script to get VM Maximum CPU and Memory
                                wfrancoti Novice

                                I just ran the script again and then decided wait for 10-15 min and then I could see the file there but now I am also viewing the errors below:

                                 

                                PS C:\> C:\Users\test\Desktop\w2k8_cpu_mem_peak.ps1

                                Get-Datacenter : 07/08/2019 13:28:47    Get-Datacenter          Could not find VirtualMachine with name 'XXXX7002'.

                                At C:\Users\test\Desktop\w2k8_cpu_mem_peak.ps1:13 char:18

                                +    Datacenter = (Get-Datacenter -VM $_.Name).Name

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

                                    + CategoryInfo          : ObjectNotFound: (XXXX7002:String) [Get-Datacenter], VimException

                                    + FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_ObjectNotFound,VMware.VimAutomation.ViCore.Cmdle

                                   ts.Commands.GetDatacenter

                                 

                                Get-Datacenter : 07/08/2019 13:28:47    Get-Datacenter          VirtualMachine parameter: Could not find any of the objects

                                specified by name.

                                At C:\Users\test\Desktop\w2k8_cpu_mem_peak.ps1:13 char:18

                                +    Datacenter = (Get-Datacenter -VM $_.Name).Name

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

                                    + CategoryInfo          : ObjectNotFound: (VMware.VimAutom...ualMachine[] VM:RuntimePropertyInfo) [Get-Datacenter]

                                   , ObnRecordProcessingFailedException

                                    + FullyQualifiedErrorId : Core_ObnSelector_SetNewParameterValue_ObjectNotFoundCritical,VMware.VimAutomation.ViCore

                                   .Cmdlets.Commands.GetDatacenter

                                 

                                Get-Cluster : 07/08/2019 13:29:04       Get-Cluster             Could not find VirtualMachine with name 'XXXXX7002'.

                                At C:\Users\test\Desktop\w2k8_cpu_mem_peak.ps1:14 char:15

                                +    Cluster = (Get-Cluster -VM $_.Name).Name

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

                                    + CategoryInfo          : ObjectNotFound: (XXXX7002:String) [Get-Cluster], VimException

                                    + FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_ObjectNotFound,VMware.VimAutomation.ViCore.Cmdle

                                   ts.Commands.GetCluster

                                Thanks

                                • 13. Re: Script to get VM Maximum CPU and Memory
                                  LucD Guru
                                  User ModeratorsCommunity WarriorsvExpert

                                  And is there a VM with that name in your environment?
                                  Did you try

                                   

                                  Get-Datacenter -VM XXXX7002

                                  • 14. Re: Script to get VM Maximum CPU and Memory
                                    wfrancoti Novice

                                    You´re right, there is no any vm with this name and now I was checking the file and it is being populated.

                                     

                                    Thanks for you help again. I really appreciated that.

                                     

                                    Have a great week.

                                    1 2 Previous Next