1 2 Previous Next 23 Replies Latest reply on Feb 10, 2020 5:04 AM by rgonzalez2

    Report Powered off Vmware + Date

    rgonzalez2 Novice

      Hi,

      I need a little help. LucD Guru of vmware

      i have this script in powershell CLI:

       

      Connect-VIServer -Server xxxxx -User 'xxxx@vsphere.local' -Password 'xxxxx'
      $vms = Get-VM
      
      Foreach ( $vm in $vms ) {
      
         Write-Output ("")
      
         Write-Output ("> Nombre VM: " +  $vm.Name)
      
         Write-Output ("> On/Off: " + $vm.PowerState)
      
         Write-Output ("> Fecha: " + $lastPO.CreatedTime)
      }
      
      Disconnect-VIServer * -Confirm:$false
      

       

      But, i dont know why, the Time dont found. for example

      > Nombre VM: (xxxxxxxx)
      >On/Off: PoweredOff
      > Fecha: 
      

      the "fecha" is empty

      Why can i resolve this?

      the idea is export to HTML or CSV, but this is secundary.

      thanks a lot

        • 1. Re: Report Powered off Vmware + Date
          bewe Expert

          where is $lastpo defined ?

          you have to query the events : Last Power Off / Guest OS Shutdown Date - PowerCLI

          • 2. Re: Report Powered off Vmware + Date
            scott28tt Champion
            VMware EmployeesUser ModeratorsCommunity Warriors

            Moderator: Moved to PowerCLI

            • 3. Re: Report Powered off Vmware + Date
              rgonzalez2 Novice

              Could you correct the code for me? im really noob hahaha

              • 4. Re: Report Powered off Vmware + Date
                LucD Guru
                User ModeratorsCommunity WarriorsvExpert

                You could do something like the following.

                It looks 7 days back. If you want to look back further in time, change the $start variable.

                 

                $start = (Get-Date).AddDays(7)

                Get-VM |

                Select Name,PowerState,

                    @{N='Last PO';E={

                        Get-VIEvent -Entity $_ -Start $start -MaxSamples ([int]::MaxValue) |

                            where{$_ -is [VMware.Vim.VmPoweredOffEvent] -or

                                  $_ -is [VMware.Vim.VmGuestShutdownEvent]} |

                        Sort-Object -Property CreatedTime |

                        Select -Last 1 | Select -ExpandProperty CreatedTime}} |

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

                 

                Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                • 5. Re: Report Powered off Vmware + Date
                  rgonzalez2 Novice

                  But i need show the date,

                   

                  for other hand do i need the first line no??

                  Connect-VIServer -Server xxxxx -User 'xxxx@vsphere.local' -Password 'xxxxx'

                   

                  So the full code is...

                   

                  Connect-VIServer -Server xxxxx -User 'xxxx@vsphere.local' -Password 'xxxxx'  
                  $start = (Get-Date).AddDays(-7)
                  
                  Get-VM |
                  Select Name,PowerState,
                      @{N='Last PO';E={
                          Get-VIEvent -Entity $_ -Start $start -MaxSamples ([int]::MaxValue) |
                              where{$_ -is [VMware.Vim.VmPoweredOffEvent] -or
                                    $_ -is [VMware.Vim.VmGuestShutdownEvent]} |
                          Sort-Object -Property CreatedTime |
                          Select -Last 1 | Select -ExpandProperty CreatedTime}} |
                  Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
                  
                  
                  Disconnect-VIServer * -Confirm:$false  
                  

                   

                   

                  I just need in my script that the date be reflected.

                   

                   

                  thanks for your time

                  • 6. Re: Report Powered off Vmware + Date
                    LucD Guru
                    vExpertCommunity WarriorsUser Moderators

                    Yes, we most of the time assume that the Connect-VIServer is already done.

                     

                    The 'Last PO' property will have the date, provided there has been a power off in the last 7 days.

                    Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                    • 7. Re: Report Powered off Vmware + Date
                      rgonzalez2 Novice

                      Hi, thx for ur help, but the last PO dont found

                      and dont show only the poweredoff, i dont undertand why dont found

                       

                      Thanks for your time

                      • 8. Re: Report Powered off Vmware + Date
                        LucD Guru
                        User ModeratorsCommunity WarriorsvExpert

                        If you only want to check the powered off VMs you can add a where-clause.

                         

                        Like I said before if the power off didn't happen in the last 7 days, no date will be found.

                        You can make the period longer by increasing the negative value on the AddDays method.

                         

                         

                        $start = (Get-Date).AddDays(-7)

                        Get-VM |

                        where {$_.PowerState -eq 'PoweredOff'} |

                        Select Name,PowerState,

                            @{N='Last PO';E={

                                Get-VIEvent -Entity $_ -Start $start -MaxSamples ([int]::MaxValue) |

                                    where{$_ -is [VMware.Vim.VmPoweredOffEvent] -or

                                          $_ -is [VMware.Vim.VmGuestShutdownEvent]} |

                                Sort-Object -Property CreatedTime |

                                Select -Last 1 | Select -ExpandProperty CreatedTime}} |

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

                         

                        Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                        1 person found this helpful
                        • 9. Re: Report Powered off Vmware + Date
                          rgonzalez2 Novice

                          Nice, thanks for all LucD

                          • 10. Re: Report Powered off Vmware + Date
                            LucD Guru
                            vExpertCommunity WarriorsUser Moderators

                            For your info: I noticed that the value in the AddDays method was not a negative value in the example script.

                            The value should, of course, be a negative value, you want the Start date to be in the past.

                            I corrected that in the code above.

                            Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                            • 11. Re: Report Powered off Vmware + Date
                              rgonzalez2 Novice

                              But.. i have the same valor with valor (999999) that (-7)

                               

                              Why?

                               

                              In the valor 9999 i see a 1 VM more.

                              • 12. Re: Report Powered off Vmware + Date
                                bewe Expert

                                try with smaller negative values. -99999 days points to the year 1746 - for a minimum i would start with 1.1.1970

                                • 13. Re: Report Powered off Vmware + Date
                                  rgonzalez2 Novice

                                  But with the valor -7000. ,my report (CSV) is empty

                                   

                                  Why?

                                  • 14. Re: Report Powered off Vmware + Date
                                    LucD Guru
                                    Community WarriorsUser ModeratorsvExpert

                                    The events by definition happened in the past.
                                    The AddDays method gets a date that is offset from the date (obtained through Get-Date) on which you call the AddDays method.

                                    The maximum retention date for events is around 1 year, so going back further than +/- 365 days makes no sense.

                                     

                                    The VM that is returned, which date does it show?

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