1 2 Previous Next 22 Replies Latest reply on Jun 6, 2017 10:52 PM by LucD

    Last VM powerOn

    jrhodes Novice


      I am trying to pull together a script that will list all my powerd off VMs and then list its last PowerOn.



      Anyone have any ideas?






        • 1. Re: Last VM powerOn
          Niket Hot Shot



          Connect-VIServer -Server

          Get-VM | Where {$_.PowerState -eq 'poweredOff' } |  % {$_ | Get-View; Write-Host $_.Name ; Write-Host $_.Runtime.BootTime }

          I hope it helps you.




          • 2. Re: Last VM powerOn
            jrhodes Novice


            That didn't work for me.  I am looking for some date that the VM was last running.



            When I tried that it gave me the fllowing output:



            Capability           : VMware.Vim.VirtualMachineCapability

            Config               : VMware.Vim.VirtualMachineConfigInfo

            Layout               : VMware.Vim.VirtualMachineFileLayout

            EnvironmentBrowser   : VMware.Vim.ManagedObjectReference

            ResourcePool         : VMware.Vim.ManagedObjectReference

            ResourceConfig       : VMware.Vim.ResourceConfigSpec

            Runtime              : VMware.Vim.VirtualMachineRuntimeInfo

            Guest                : VMware.Vim.GuestInfo

            Summary              : VMware.Vim.VirtualMachineSummary

            Datastore            : {VMware.Vim.ManagedObjectReference}

            Network              : {VMware.Vim.ManagedObjectReference}

            Snapshot             :

            GuestHeartbeatStatus : gray

            Parent               : VMware.Vim.ManagedObjectReference

            CustomValue          : + OverallStatus        : green ConfigStatus         : green ConfigIssue          : {} EffectiveRole        : {-1} Permission           : {} Name                 : dsy12512kw DisabledMethod       : {RevertToCurrentSnapshot_Task, RemoveAllSnapshots_Task, ResetVM_Task, UnmountToolsInstaller...} RecentTask           : {} DeclaredAlarmState   : {alarm-4.vm-271, alarm-5.vm-271, alarm-6.vm-271} TriggeredAlarmState  : {} +Value                :

            AvailableField       : {Snapshot Size (MB), Snapshots}

            MoRef                : VMware.Vim.ManagedObjectReference

            Client               : VMware.Vim.VimClient








            • 3. Re: Last VM powerOn
              LucD Guru
              User ModeratorsvExpertCommunity Warriors

              Can you try this:

              Get-VM | where {$_.PowerState -eq 'poweredOff' } | %{
                $vm = $_ | get-view
                Write-Host $vm.Name $vm.runtime.boottime


              1 person found this helpful
              • 4. Re: Last VM powerOn
                jrhodes Novice





                When I run that it gives me the following:


                qvmxp6-jmat2 1/1/0001 12:00:00 AM

                QSQSEA78SR 1/1/0001 12:00:00 AM

                qw3k06r 1/1/0001 12:00:00 AM

                qudsea77arc1 1/1/0001 12:00:00 AM

                qw3k05du1 1/1/0001 12:00:00 AM

                qw2k04du1 1/1/0001 12:00:00 AM

                qaoptsoawin32zh 1/1/0001 12:00:00 AM

                qaoptsoaw32zhtw 1/1/0001 12:00:00 AM

                qaoptsoaw32ptbr 1/1/0001 12:00:00 AM

                qorsia80tgt2 1/1/0001 12:00:00 AM

                qw3k08 1/1/0001 12:00:00 AM

                qw2k07du1 1/1/0001 12:00:00 AM

                q10gebs12d 1/1/0001 12:00:00 AM

                qw3k04du2 1/1/0001 12:00:00 AM

                qor10rhsia80 1/1/0001 12:00:00 AM

                qorsia77src 1/1/0001 12:00:00 AM

                qw3k06du2 1/1/0001 12:00:00 AM

                qw2k06du1 1/1/0001 12:00:00 AM

                q10gebs510b 1/27/2009 8:10:51 PM

                qaoptsoawin32fr 1/1/0001 12:00:00 AM

                qaebsws1 1/1/0001 12:00:00 AM

                qw2k05du1 1/1/0001 12:00:00 AM

                qvmxp6-nd1 1/21/2009 7:46:55 PM


                It appears that it is not finding a date for most of the powered off VMs, Right?



                Where is is getting this info? Logs or the VC database? 



                Any ideas why?






                • 5. Re: Last VM powerOn
                  LucD Guru
                  Community WarriorsvExpertUser Moderators

                  That is indeed the problem with the bootTime property in the VirtualMachineRuntimeInfo object.


                  A better approach would be to use the power on event.

                  Something like this

                  Get-VM <VM-name> | Get-VIEvent -Types Info -MaxSamples 1000 | `
                    Where-Object {$_.fullFormattedMessage -match "Power On"} | `
                    Sort-Object -property createdTime |  select -last 1 | %{
                      Write-Host $_.vm.name $_.createdTime | Out-Default

                  The problem with this method is that is currently a known issue with the Get-VIEvent cmdlet.

                  The cmdlet returns only 1000 events independent of what you specify in the -maxSmaples parameter.

                  If the power on is some time ao it could be that it doesn't fall in the last 1000 events.


                  There is a method to bypass this issue by using the SDK methods.

                  See  Get-VIEvent maxsamples size

                  • 6. Re: Last VM powerOn
                    jrhodes Novice


                    Thanks Luc!



                    This helps a lot. At least I now know it is a limitation of the VI Toolkit.



                    I will research using the VI-SDK to get the info I need.









                    • 7. Re: Last VM powerOn
                      Salalon Enthusiast

                      hi all,


                      i need this same report but i have a infraestrture vcenter 2.5 not Sphere, is possible ? how ?




                      • 8. Re: Last VM powerOn
                        LucD Guru
                        User ModeratorsvExpertCommunity Warriors

                        You should be able to use the same script with VC 2.5.






                        Blog: LucD notes

                        Twitter: lucd22

                        • 9. Re: Last VM powerOn
                          Salalon Enthusiast


                          i can´t to execute this script, i have one error:






                          C:\&gt; & '.\VM Last Poweron Time.ps1'

                          File C:\VM Last Poweron Time.ps1 cannot be loaded. The file C:\VM Last Poweron

                          Time.ps1 is not digitally signed. The script will not execute on the system. Pl

                          ease see "get-help about_signing" for more details..

                          At line:1 char:2

                          + &  &lt;&lt;&lt;&lt; '.\VM Last Poweron Time.ps1'









                          • 10. Re: Last VM powerOn
                            LucD Guru
                            User ModeratorsCommunity WarriorsvExpert

                            I suspect you haven't set the execution policy for PowerShell scripts yet.


                            Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

                            from the prompt before you start the script.






                            Blog: LucD notes

                            Twitter: lucd22

                            • 11. Re: Last VM powerOn
                              Salalon Enthusiast





                              thx, but now i have a diferent error:



                              C:\&gt;  get-vm | ./'VM Last Poweron Time.ps1'

                              Parameter declarations are a comma-separated list of variable names with option

                              al initializer expressions.

                              At C:\VM Last Poweron Time.ps1:9 char:11

                              +         )] &lt;&lt;&lt;&lt;






                              • 12. Re: Last VM powerOn
                                tdubb123 Master

                                I am trying to do the same the createtime value is not the same as what i see in tasks



                                • 13. Re: Last VM powerOn
                                  LucD Guru
                                  User ModeratorsCommunity WarriorsvExpert

                                  There is indeed a time difference.

                                  The script is using the time the corresponding event was created, not when the Task was actually running.

                                  You could look for TaskEvent entries, and get the actual Task timestamp.


                                  Are there big time differences?

                                  • 14. Re: Last VM powerOn
                                    tdubb123 Master

                                    not it looks ok.


                                    but I am trying to do this now and not able to get my VM name


                                    get-content poweredoffvms.txt | % { get-vm -name $_ | get-vievent -types INfo -maxsamples 1000 | ? {$_.fullformattedmessage -match "Power On"} |Sort-object -property createdtime | select


                                    $vm.name, fullformattedmessage, createdtime}


                                    but not working

                                    1 2 Previous Next