1 2 3 Previous Next 32 Replies Latest reply on Feb 25, 2020 3:19 AM by ehsanijavad Go to original post
      • 15. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
        MrJohnson Enthusiast

        Sorry I see what your talking about... I just added the | symbol.

        • 16. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
          LucD Guru
          vExpertCommunity WarriorsUser Moderators

          Strange, when I copy/paste it works.

          But yes, that should be a pipe symbol (vertical bar), same as on the 1st line

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 17. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
            MrJohnson Enthusiast

            It was a user error on my end.  You had it right the first time. Seeing that I have to capture each host in the vCluster one at a time I just need to try and figure out how to append to the same report.csv file. 

            • 18. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
              LucD Guru
              Community WarriorsUser ModeratorsvExpert

              That is quite easy, and one of the nice features of PowerShell.

               

              Get-Cluster -Name MyCluster |

              Get-VMHost|

              Select Name,

                  @{N='CPU GHz Capacity';E={[math]::Round($_.CpuTotalMhz/1000,2)}},

                  @{N='CPU GHz Used';E={[math]::Round($_.CpuUsageMhz/1000,2)}},

                  @{N='CPU GHz Free';E={[math]::Round(($_.CpuTotalMhz - $_.CpuUsageMhz)/1000,2)}},

                  @{N='Memory Capacity GB';E={[math]::Round($_.MemoryTotalGB,2)}},

                  @{N='Memory Used GB';E={[math]::Round($_.MemoryUsageGB,2)}},

                  @{N='Memory Free GB';E={[math]::Round(($_.MemoryTotalGB - $_.MemoryUsageGB),2)}} |

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

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              1 person found this helpful
              • 19. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
                MrJohnson Enthusiast

                Can I ask one last question?  How can I add a second and 3rd vCluster? 

                 

                 

                WoW, I can't tell you how much I appreciate you're help.

                • 20. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
                  MrJohnson Enthusiast

                  This almost works for me in adding the storage as well.  The only problem is it only exports the storage info and not the CPU and Mem to the report.csv, and I can figure out how to get all the vClusters in vCenter in the same script.

                   

                   

                  Get-Cluster -Name vCluster |

                  Get-VMHost|

                  Select Name,

                      @{N='CPU GHz Capacity';E={[math]::Round($_.CpuTotalMhz/1000,2)}},

                      @{N='CPU GHz Used';E={[math]::Round($_.CpuUsageMhz/1000,2)}},

                      @{N='CPU GHz Free';E={[math]::Round(($_.CpuTotalMhz - $_.CpuUsageMhz)/1000,2)}},

                      @{N='Memory Capacity GB';E={[math]::Round($_.MemoryTotalGB,2)}},

                      @{N='Memory Used GB';E={[math]::Round($_.MemoryUsageGB,2)}},

                      @{N='Memory Free GB';E={[math]::Round(($_.MemoryTotalGB - $_.MemoryUsageGB),2)}}

                  $Result += Get-View -ViewType Datastore | Where-Object {$_.Name -notmatch "pag"} | Select-Object -Property Name,

                     @{N="FreeSpaceGB";E={[Math]::Round($_.Summary.FreeSpace/1GB,0)}},

                     @{N="CapacityGB"; E={[Math]::Round($_.Summary.Capacity/1GB,0)}},

                     @{N="ProvisionedSpaceGB";E={[Math]::Round(($_.Summary.Capacity - $_.Summary.FreeSpace + $_.Summary.Uncommitted)/1GB,0)}},

                     @{N="FreeSpace";E={[math]::Round(((100* ($_.Summary.FreeSpace/1GB))/ ($_.Summary.Capacity/1GB)),0)}} | sort -Property "FreeSpace"|

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

                  • 21. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
                    MrJohnson Enthusiast

                    I found the following.  Do you know if it is possible to add what you gave me to this script?

                     

                    # Author: Amol Patil

                    # DataStoreUsageReport

                     

                    set-executionpolicy Unrestricted -Force # Execute Policy

                     

                     

                    ##### Add VMWare Snanpin.

                    if(-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue))

                    {

                       Add-PSSnapin VMware.VimAutomation.Core

                    }

                    #*****************************************

                    $SCRIPT_PARENT   = Split-Path -Parent $MyInvocation.MyCommand.Definition

                     

                     

                    #************** Remove old files ***************************

                    remove-item ($SCRIPT_PARENT + "\Report\V*.html") -force

                     

                     

                    ########### Connect VCs from VC_List.txt ############

                    $VCs= Get-Content ($SCRIPT_PARENT + "\vc_list.txt") -ErrorAction SilentlyContinue # mention vcenter name where you want to check resources.

                    # $VCs= Get-Content  -Path N:\Scripts\VMWare\Storage_Report_HTML\vc_list.txt 

                    $D = get-date -uformat "%m-%d-%Y-%H:%M" # To get a current date.

                     

                     

                    Write-Host "Connecting to VC" -foregroundcolor yellow

                     

                     

                    #*****************************************

                    foreach($vc in $VCs)

                    {

                     

                     

                    Connect-VIServer $VC -WarningAction 0

                     

                     

                    $outputfile = ($SCRIPT_PARENT + "\Report\$($VC).html") #".\Report\$($VC).html"

                    Write-Host ""

                    Write-Host "Collecting details from $VC" -foregroundcolor green

                    $Result = @()

                    $Result += Get-View -ViewType Datastore | Where-Object {$_.Name -notmatch "pag"} | Select-Object -Property Name,

                      @{N="FreeSpaceGB";E={[Math]::Round($_.Summary.FreeSpace/1GB,0)}},

                      @{N="CapacityGB"; E={[Math]::Round($_.Summary.Capacity/1GB,0)}},

                      @{N="ProvisionedSpaceGB";E={[Math]::Round(($_.Summary.Capacity - $_.Summary.FreeSpace + $_.Summary.Uncommitted)/1GB,0)}},

                      @{N="FreeSpace";E={[math]::Round(((100* ($_.Summary.FreeSpace/1GB))/ ($_.Summary.Capacity/1GB)),0)}} | sort -Property "FreeSpace"

                     

                     

                     

                          $HTML = '<style type="text/css">

                          #Header{font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;}

                          #Header td, #Header th {font-size:14px;border:1px solid #98bf21;padding:3px 7px 2px 7px;}

                          #Header th {font-size:14px;text-align:center;padding-top:5px;padding-bottom:4px;background-color:#cccccc;color:#000000;}

                          #Header tr.alt td {color:#000;background-color:#EAF2D3;}

                          </Style>'

                     

                     

                        $HTML += "<HTML><BODY><Table border=1 cellpadding=0 cellspacing=0 id=Header><caption><font size=3 color=green><h1 align=""center"">~$VC-DataStore Verification Report~ </h1></font>

                        <h4 align=""right""><font size=3 color=""#00008B"">Date: $D </font></h4></caption>

                                  

                                <TR>

                                      <TH><B>DataStore Name</B></TH>

                                      <TH><B>Free Space (GB)</B></TD>

                                      <TH><B>Capacity (GB)</B></TH>

                                      <TH><B>Provisioned Space (GB)</B></TH>

                                      <TH><B>Free Space (%)</B></TH>

                                     

                                </TR>"

                        Foreach($Entry in $Result)

                        {

                            if($Entry.FreeSpace -lt "20")

                                {

                                      $HTML += "<TR bgColor=Red>"

                                }

                                else

                                {

                                      $HTML += "<TR>"

                                }

                                $HTML += "

                                                        <TD>$($Entry.Name)</TD>

                                                        <TD>$($Entry.FreeSpaceGB)</TD>

                                                        <TD>$($Entry.CapacityGB)</TD>

                                                        <TD>$($Entry.ProvisionedSpaceGB)</TD>

                                                        <TD>$($Entry.FreeSpace)</TD>

                                                  </TR>"

                        }

                        $HTML += "</Table></BODY></HTML>"

                     

                     

                          $HTML | Out-File $OutputFile

                     

                     

                    Disconnect-VIServer $VC -Confirm:$false

                    }

                     

                     

                    $Uname = Get-Content Env:USERNAME

                    $Comp = Get-Content Env:COMPUTERNAME

                      

                    #Send mail->

                        # Add email IDs in email_id.txt file with , and in next line.   

                        $mailto = Get-Content ($SCRIPT_PARENT + "\email_id.txt") -ErrorAction SilentlyContinue

                        $SMTPserver = "mail-server" # SMTP server

                        $msg = new-object Net.Mail.MailMessage 

                        $smtp = new-object Net.Mail.SmtpClient($SMTPserver) 

                        $msg.From = "email-address" # Sender ID

                        $msg.IsBodyHTML = $true

                        $msg.To.Add($mailto)  # Mail To id get from list

                     

                        $msg.Subject = "Datastores Usage Report - $vcs" # Subject of the email.

                     

                    foreach($vc in $vcs)

                         {

                         $MailTextT =  Get-Content ($SCRIPT_PARENT + "\Report\V*.html") -ErrorAction SilentlyContinue

                         $Sig =  "<html><p><o:p> </o:p></p><B> Capacity Report, <p> ProdSE (email-address)</B></p></html>"

                         $Top = "<html> This Script is executed on Server - <B>$Comp</B> by User - <b> $Uname </b></html>"

                         $MailText= $Top + $MailTextT + $Sig

                        

                        }

                       

                       $msg.Body = $MailText

                       $smtp.Send($msg)

                      

                    #*****************************************

                    • 22. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
                      LucD Guru
                      User ModeratorsvExpertCommunity Warriors

                      You are querying for information from 2 different objects (VMHost and Datastore).

                      That means you will have to combine this in 1 select cmdlet.

                      Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                      • 23. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
                        MrJohnson Enthusiast

                        I added the following.  But not sure what I'm missing.  I get the following in PowerCLI when running it.

                         

                        Key   : N

                        Value : CPUGHzCapacity

                        Name  : N

                         

                        Key   : E

                        Value : [math]::Round($_.CpuTotalMhz/1000,2)

                        Name  : E

                         

                        Key   : N

                        Value : CPUGHzUsed

                        Name  : N

                         

                        Key   : E

                        Value : [math]::Round($_.CpuUsageMhz/1000,2)

                        Name  : E

                         

                        Key   : N

                        Value : CPUGHzFree

                        Name  : N

                         

                        Key   : E

                        Value : [math]::Round(($_.CpuTotalMhz - $_.CpuUsageMhz)/1000,2)

                        Name  : E

                         

                        Key   : N

                        Value : MemoryCapacityGB

                        Name  : N

                         

                        Key   : E

                        Value : [math]::Round($_.MemoryTotalGB,2)

                        Name  : E

                         

                        Key   : N

                        Value : MemoryUsedGB

                        Name  : N

                         

                        Key   : E

                        Value : [math]::Round($_.MemoryUsageGB,2)

                        Name  : E

                         

                        Key   : N

                        Value : MemoryFreeGB

                        Name  : N

                         

                        Key   : E

                        Value : [math]::Round(($_.MemoryTotalGB - $_.MemoryUsageGB),2)

                        Name  : E

                         

                         

                        My changes

                         

                        ########### Connect VCs from VC_List.txt ############

                        $VCs= Get-Content ($SCRIPT_PARENT + "\vc_list.txt") -ErrorAction SilentlyContinue # mention vcenter name where you want to check resources.

                        # $VCs= Get-Content  -Path N:\Scripts\VMWare\Storage_Report_HTML\vc_list.txt

                        $D = get-date -uformat "%m-%d-%Y-%H:%M" # To get a current date.

                         

                         

                        Write-Host "Connecting to VC" -foregroundcolor yellow

                         

                         

                        #*****************************************

                        foreach($vc in $VCs)

                        {

                         

                         

                        Connect-VIServer $VC -WarningAction 0

                         

                         

                        $outputfile = ($SCRIPT_PARENT + "\Report\$($VC).html") #".\Report\$($VC).html"

                        Write-Host ""

                        Write-Host "Collecting details from $VC" -foregroundcolor green

                        $Result = @()

                        $Result += Get-View -ViewType Datastore | Where-Object {$_.Name -notmatch "pag"} | Select-Object -Property Name,

                          @{N="FreeSpaceGB";E={[Math]::Round($_.Summary.FreeSpace/1GB,0)}},

                          @{N="CapacityGB"; E={[Math]::Round($_.Summary.Capacity/1GB,0)}},

                          @{N="ProvisionedSpaceGB";E={[Math]::Round(($_.Summary.Capacity - $_.Summary.FreeSpace + $_.Summary.Uncommitted)/1GB,0)}},

                          @{N="FreeSpace";E={[math]::Round(((100* ($_.Summary.FreeSpace/1GB))/ ($_.Summary.Capacity/1GB)),0)}} | sort -Property "FreeSpace"

                          @{N="CPUGHzCapacity";E={[math]::Round($_.CpuTotalMhz/1000,2)}},

                          @{N="CPUGHzUsed";E={[math]::Round($_.CpuUsageMhz/1000,2)}},

                          @{N="CPUGHzFree";E={[math]::Round(($_.CpuTotalMhz - $_.CpuUsageMhz)/1000,2)}},

                          @{N="MemoryCapacityGB";E={[math]::Round($_.MemoryTotalGB,2)}},

                          @{N="MemoryUsedGB";E={[math]::Round($_.MemoryUsageGB,2)}},

                          @{N="MemoryFreeGB";E={[math]::Round(($_.MemoryTotalGB - $_.MemoryUsageGB),2)}}

                         

                         

                         

                              $HTML = '<style type="text/css">

                              #Header{font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;}

                              #Header td, #Header th {font-size:14px;border:1px solid #98bf21;padding:3px 7px 2px 7px;}

                              #Header th {font-size:14px;text-align:center;padding-top:5px;padding-bottom:4px;background-color:#cccccc;color:#000000;}

                              #Header tr.alt td {color:#000;background-color:#EAF2D3;}

                              </Style>'

                         

                         

                            $HTML += "<HTML><BODY><Table border=1 cellpadding=0 cellspacing=0 id=Header><caption><font size=3 color=green><h1 align=""center"">~$VC-DataStore Verification Report~ </h1></font>

                            <h4 align=""right""><font size=3 color=""#00008B"">Date: $D </font></h4></caption>

                                    

                                    <TR>

                                          <TH><B>DataStore Name</B></TH>

                                          <TH><B>Free Space (GB)</B></TD>

                                          <TH><B>Capacity (GB)</B></TH>

                                          <TH><B>Provisioned Space (GB)</B></TH>

                                          <TH><B>Free Space (%)</B></TH>

                                          <TH><B>CPU GHz Capacity (MHz)</B></TH>

                                          <TH><B>CPU GHz Free (MHz)</B></TD>

                                          <TH><B>CPU GHz Used (MHz)</B></TH>

                                          <TH><B>Memory Capacity (GB)</B></TH>

                                          <TH><B>MemoryFree (GB)</B></TD>

                                          <TH><B>Memory Used (GB)</B></TH>

                                       

                                    </TR>"

                            Foreach($Entry in $Result)

                            {

                                if($Entry.FreeSpace -lt "20")

                                    {

                                          $HTML += "<TR bgColor=Red>"

                                    }

                                    else

                                    {

                                          $HTML += "<TR>"

                                    }

                                    $HTML += "

                                                            <TD>$($Entry.Name)</TD>

                                                            <TD>$($Entry.FreeSpaceGB)</TD>

                                                            <TD>$($Entry.CapacityGB)</TD>

                                                            <TD>$($Entry.ProvisionedSpaceGB)</TD>

                                                            <TD>$($Entry.FreeSpace)</TD>

                                                            <TD>$($Entry.CPUGHzCapacity)</TD>

                                                            <TD>$($Entry.CPUGHzUsed)</TD>

                                                            <TD>$($Entry.CPUGHzFree)</TD>

                                                            <TD>$($Entry.MemoryCapacityGB)</TD>

                                                            <TD>$($Entry.MemoryUsedGB)</TD>

                                                            <TD>$($Entry.MemoryFreeGB)</TD>

                                                      </TR>"

                            }

                            $HTML += "</Table></BODY></HTML>"

                         

                         

                              $HTML | Out-File $OutputFile

                        • 24. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
                          LucD Guru
                          Community WarriorsUser ModeratorsvExpert

                          Like I said before, you are mixing properties from 2 different objects in 1 Select, that will not work.

                          With the Get-View -ViewType Datastore you get Datastore objects, but you also select properties that come from a VMHost object.

                          And there is this Sort-Object in the middle of the Select, that is causing errors.

                          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                          • 25. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
                            MrJohnson Enthusiast

                            Understood.  Can you help me in getting all the ESXi in each vClusters added to one report?

                             

                            I have about 4 vClusters in vCenter if that matters.

                             

                            Get-Cluster -Name -vCluster |

                            Get-VMHost|

                            Select Name,

                                @{N='CPU GHz Capacity';E={[math]::Round($_.CpuTotalMhz/1000,2)}},

                                @{N='CPU GHz Used';E={[math]::Round($_.CpuUsageMhz/1000,2)}},

                                @{N='CPU GHz Free';E={[math]::Round(($_.CpuTotalMhz - $_.CpuUsageMhz)/1000,2)}},

                                @{N='Memory Capacity GB';E={[math]::Round($_.MemoryTotalGB,2)}},

                                @{N='Memory Used GB';E={[math]::Round($_.MemoryUsageGB,2)}},

                                @{N='Memory Free GB';E={[math]::Round(($_.MemoryTotalGB - $_.MemoryUsageGB),2)}} |

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

                            • 26. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
                              LucD Guru
                              Community WarriorsUser ModeratorsvExpert

                              Sure, try like this

                               

                              Get-Cluster |

                              Get-VMHost|

                              Select Name,

                                  @{N='Cluster';E={$_.Parent.Name}},

                                  @{N='CPU GHz Capacity';E={[math]::Round($_.CpuTotalMhz/1000,2)}},

                                  @{N='CPU GHz Used';E={[math]::Round($_.CpuUsageMhz/1000,2)}},

                                  @{N='CPU GHz Free';E={[math]::Round(($_.CpuTotalMhz - $_.CpuUsageMhz)/1000,2)}},

                                  @{N='Memory Capacity GB';E={[math]::Round($_.MemoryTotalGB,2)}},

                                  @{N='Memory Used GB';E={[math]::Round($_.MemoryUsageGB,2)}},

                                  @{N='Memory Free GB';E={[math]::Round(($_.MemoryTotalGB - $_.MemoryUsageGB),2)}} |

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

                              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                              • 28. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
                                ehsanijavad Novice

                                Hi,

                                Thank you for your Scripts.

                                Please let me know, how can i write this with percent.

                                 

                                Thank you for your answer.

                                • 29. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
                                  LucD Guru
                                  User ModeratorsCommunity WarriorsvExpert

                                  Which property do you want in percentage?

                                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz