1 2 Previous Next 16 Replies Latest reply on Jan 15, 2018 4:41 AM by LucD

    list VM's in a datastore with powered off state and the date they was powered off ? using powerCli

    AndygRomo Novice

      Hi Everyone I want to create a csv with this detail list of vm's in a data store that are powered off and the date they was powered off i need the data store details not the vcentre details ?

        • 1. Re: list VM's in a datastore with powered off state and the date they was powered off ? using powerCli
          LucD Guru
          User ModeratorsCommunity WarriorsvExpert

          Try like this

           

          $datastoreName = 'MyDS'

          Get-Datastore -Name $datastoreName | Get-Vm |

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

          Select @{N='Datastore';E={$datastoreName}},Name,PowerState |

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

          • 2. Re: list VM's in a datastore with powered off state and the date they was powered off ? using powerCli
            PabloJAguilar2014 Novice
            vExpert

            For get the event of power off you have to use that

             

            get-vm $vm | Get-viEvent | where {$_.FullFormattedMessage -like "*powered off"} | Select @{N='VM';E={$vm}}, Username, CreatedTime

             

             

             

            Regards

            • 3. Re: list VM's in a datastore with powered off state and the date they was powered off ? using powerCli
              LucD Guru
              Community WarriorsvExpertUser Moderators

              Didn't notice you needed the power off date as well, try like this

               

              $datastoreName = 'MyDS'

              Get-Datastore -Name $datastoreName | Get-Vm |

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

              Select @{N='Datastore';E={$datastoreName}},Name,PowerState,

                  @{N='PoweredOff';E={

                      Get-VIEvent -Entity $_ -MaxSamples ([int]::MaxValue) | where{$_ -is [VMware.Vim.VmPoweredOffEvent]} |

                      Sort-Object -Property CreatedTime -Descending |

                      select -First 1 -ExpandProperty CreatedTime

                  }}

              1 person found this helpful
              • 4. Re: list VM's in a datastore with powered off state and the date they was powered off ? using powerCli
                AndygRomo Novice

                This is great thank you but can we also get the powered off date ?

                • 6. Re: list VM's in a datastore with powered off state and the date they was powered off ? using powerCli
                  AndygRomo Novice

                  This looks good but i also need to output it to csv and this line with my location returns error when added to bottom of the script ?

                   

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

                  • 7. Re: list VM's in a datastore with powered off state and the date they was powered off ? using powerCli
                    AndygRomo Novice

                    I cant see the output for dates on screen and i'd like to output to csv ...thank you for your help

                    • 8. Re: list VM's in a datastore with powered off state and the date they was powered off ? using powerCli
                      LucD Guru
                      vExpertUser ModeratorsCommunity Warriors

                      You have to use the pipeline to send it to the Export-XCsv cmdlet.

                      Like this

                       

                       

                      $datastoreName = 'MyDS'

                      Get-Datastore -Name $datastoreName | Get-Vm |

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

                      Select @{N='Datastore';E={$datastoreName}},Name,PowerState,

                          @{N='PoweredOff';E={

                              Get-VIEvent -Entity $_ -MaxSamples ([int]::MaxValue) | where{$_ -is [VMware.Vim.VmPoweredOffEvent]} |

                              Sort-Object -Property CreatedTime -Descending |

                              select -First 1 -ExpandProperty CreatedTime

                          }} |

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

                      • 10. Re: list VM's in a datastore with powered off state and the date they was powered off ? using powerCli
                        LucD Guru
                        Community WarriorsvExpertUser Moderators

                        Are you archiving the events sufficiently long?

                         

                        Can you check if the following returns anything?

                         

                        $vm = Get-vm VM_2

                         

                        Get-VIEvent -Entity $vm -MaxSamples ([int]::MaxValue) |

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

                        Sort-Object -Property CreatedTime -Descending

                         

                        • 11. Re: list VM's in a datastore with powered off state and the date they was powered off ? using powerCli
                          LucD Guru
                          Community WarriorsvExpertUser Moderators

                          And also this one, to check which kind of events you are capturing for that VM

                           

                          $vm = Get-vm VM_2

                           

                          Get-VIEvent -Entity $vm -MaxSamples ([int]::MaxValue) |

                          Group-Object -Property {$_.GetType().Name}

                          • 12. Re: list VM's in a datastore with powered off state and the date they was powered off ? using powerCli
                            AndygRomo Novice

                            VM_2 is the name of my datastore you have Get-vm on that last script ?

                            • 13. Re: list VM's in a datastore with powered off state and the date they was powered off ? using powerCli
                              AndygRomo Novice

                              Values : {UserLoginSessionEvent}

                              Count  : 7903

                              Group  : {VMware.Vim.UserLoginSessionEvent, VMware.Vim.UserLoginSessionEvent, VMware.Vim.UserLoginSessionEvent,

                                       VMware.Vim.UserLoginSessionEvent...}

                              Name   : UserLoginSessionEvent

                               

                               

                               

                               

                              Values : {UserLogoutSessionEvent}

                              Count  : 7811

                              Group  : {VMware.Vim.UserLogoutSessionEvent, VMware.Vim.UserLogoutSessionEvent, VMware.Vim.UserLogoutSessionEvent,

                                       VMware.Vim.UserLogoutSessionEvent...}

                              Name   : UserLogoutSessionEvent

                               

                               

                               

                               

                              Values : {AlarmStatusChangedEvent}

                              Count  : 332

                              Group  : {VMware.Vim.AlarmStatusChangedEvent, VMware.Vim.AlarmStatusChangedEvent, VMware.Vim.AlarmStatusChangedEvent,

                                       VMware.Vim.AlarmStatusChangedEvent...}

                              Name   : AlarmStatusChangedEvent

                               

                               

                               

                               

                              Values : {EventEx}

                              Count  : 8884

                              Group  : {VMware.Vim.EventEx, VMware.Vim.EventEx, VMware.Vim.EventEx, VMware.Vim.EventEx...}

                              Name   : EventEx

                               

                               

                               

                               

                              Values : {DrsVmMigratedEvent}

                              Count  : 128

                              Group  : {VMware.Vim.DrsVmMigratedEvent, VMware.Vim.DrsVmMigratedEvent, VMware.Vim.DrsVmMigratedEvent,

                                       VMware.Vim.DrsVmMigratedEvent...}

                              Name   : DrsVmMigratedEvent

                               

                               

                               

                               

                              Values : {VmEmigratingEvent}

                              Count  : 129

                              Group  : {VMware.Vim.VmEmigratingEvent, VMware.Vim.VmEmigratingEvent, VMware.Vim.VmEmigratingEvent,

                                       VMware.Vim.VmEmigratingEvent...}

                              Name   : VmEmigratingEvent

                               

                               

                               

                               

                              Values : {TaskEvent}

                              Count  : 3142

                              Group  : {VMware.Vim.TaskEvent, VMware.Vim.TaskEvent, VMware.Vim.TaskEvent, VMware.Vim.TaskEvent...}

                              Name   : TaskEvent

                               

                               

                               

                               

                              Values : {ScheduledTaskCompletedEvent}

                              Count  : 763

                              Group  : {VMware.Vim.ScheduledTaskCompletedEvent, VMware.Vim.ScheduledTaskCompletedEvent,

                                       VMware.Vim.ScheduledTaskCompletedEvent, VMware.Vim.ScheduledTaskCompletedEvent...}

                              Name   : ScheduledTaskCompletedEvent

                               

                               

                               

                               

                              Values : {ScheduledTaskStartedEvent}

                              Count  : 763

                              Group  : {VMware.Vim.ScheduledTaskStartedEvent, VMware.Vim.ScheduledTaskStartedEvent, VMware.Vim.ScheduledTaskStartedEvent,

                                       VMware.Vim.ScheduledTaskStartedEvent...}

                              Name   : ScheduledTaskStartedEvent

                               

                               

                               

                               

                              Values : {VmAcquiredTicketEvent}

                              Count  : 170

                              Group  : {VMware.Vim.VmAcquiredTicketEvent, VMware.Vim.VmAcquiredTicketEvent, VMware.Vim.VmAcquiredTicketEvent,

                                       VMware.Vim.VmAcquiredTicketEvent...}

                              Name   : VmAcquiredTicketEvent

                               

                               

                               

                               

                              Values : {GeneralUserEvent}

                              Count  : 718

                              Group  : {VMware.Vim.GeneralUserEvent, VMware.Vim.GeneralUserEvent, VMware.Vim.GeneralUserEvent,

                                       VMware.Vim.GeneralUserEvent...}

                              Name   : GeneralUserEvent

                               

                               

                               

                               

                              Values : {VmReconfiguredEvent}

                              Count  : 1456

                              Group  : {VMware.Vim.VmReconfiguredEvent, VMware.Vim.VmReconfiguredEvent, VMware.Vim.VmReconfiguredEvent,

                                       VMware.Vim.VmReconfiguredEvent...}

                              Name   : VmReconfiguredEvent

                               

                               

                               

                               

                              Values : {VmStartingEvent}

                              Count  : 6

                              Group  : {VMware.Vim.VmStartingEvent, VMware.Vim.VmStartingEvent, VMware.Vim.VmStartingEvent, VMware.Vim.VmStartingEvent...}

                              Name   : VmStartingEvent

                               

                               

                               

                               

                              Values : {VmPoweredOnEvent}

                              Count  : 6

                              Group  : {VMware.Vim.VmPoweredOnEvent, VMware.Vim.VmPoweredOnEvent, VMware.Vim.VmPoweredOnEvent,

                                       VMware.Vim.VmPoweredOnEvent...}

                              Name   : VmPoweredOnEvent

                               

                               

                               

                               

                              Values : {VmGuestShutdownEvent}

                              Count  : 3

                              Group  : {VMware.Vim.VmGuestShutdownEvent, VMware.Vim.VmGuestShutdownEvent, VMware.Vim.VmGuestShutdownEvent}

                              Name   : VmGuestShutdownEvent

                               

                               

                               

                               

                              Values : {VmFailedToShutdownGuestEvent}

                              Count  : 3

                              Group  : {VMware.Vim.VmFailedToShutdownGuestEvent, VMware.Vim.VmFailedToShutdownGuestEvent,

                                       VMware.Vim.VmFailedToShutdownGuestEvent}

                              Name   : VmFailedToShutdownGuestEvent

                               

                               

                               

                               

                              Values : {VmStoppingEvent}

                              Count  : 3

                              Group  : {VMware.Vim.VmStoppingEvent, VMware.Vim.VmStoppingEvent, VMware.Vim.VmStoppingEvent}

                              Name   : VmStoppingEvent

                               

                               

                               

                               

                              Values : {VmPoweredOffEvent}

                              Count  : 5

                              Group  : {VMware.Vim.VmPoweredOffEvent, VMware.Vim.VmPoweredOffEvent, VMware.Vim.VmPoweredOffEvent,

                                       VMware.Vim.VmPoweredOffEvent...}

                              Name   : VmPoweredOffEvent

                               

                               

                               

                               

                              Values : {VmBeingRelocatedEvent}

                              Count  : 5

                              Group  : {VMware.Vim.VmBeingRelocatedEvent, VMware.Vim.VmBeingRelocatedEvent, VMware.Vim.VmBeingRelocatedEvent,

                                       VMware.Vim.VmBeingRelocatedEvent...}

                              Name   : VmBeingRelocatedEvent

                               

                               

                               

                               

                              Values : {VmRelocatedEvent}

                              Count  : 5

                              Group  : {VMware.Vim.VmRelocatedEvent, VMware.Vim.VmRelocatedEvent, VMware.Vim.VmRelocatedEvent,

                                       VMware.Vim.VmRelocatedEvent...}

                              Name   : VmRelocatedEvent

                               

                               

                               

                               

                              Values : {ExtendedEvent}

                              Count  : 2

                              Group  : {VMware.Vim.ExtendedEvent, VMware.Vim.ExtendedEvent}

                              Name   : ExtendedEvent

                               

                               

                               

                               

                              Values : {VmBeingHotMigratedEvent}

                              Count  : 1

                              Group  : {VMware.Vim.VmBeingHotMigratedEvent}

                              Name   : VmBeingHotMigratedEvent

                               

                               

                               

                               

                              Values : {VmBeingCreatedEvent}

                              Count  : 2

                              Group  : {VMware.Vim.VmBeingCreatedEvent, VMware.Vim.VmBeingCreatedEvent}

                              Name   : VmBeingCreatedEvent

                               

                               

                               

                               

                              Values : {VmInstanceUuidAssignedEvent}

                              Count  : 2

                              Group  : {VMware.Vim.VmInstanceUuidAssignedEvent, VMware.Vim.VmInstanceUuidAssignedEvent}

                              Name   : VmInstanceUuidAssignedEvent

                               

                               

                               

                               

                              Values : {VmUuidAssignedEvent}

                              Count  : 2

                              Group  : {VMware.Vim.VmUuidAssignedEvent, VMware.Vim.VmUuidAssignedEvent}

                              Name   : VmUuidAssignedEvent

                               

                               

                               

                               

                              Values : {VmMacAssignedEvent}

                              Count  : 2

                              Group  : {VMware.Vim.VmMacAssignedEvent, VMware.Vim.VmMacAssignedEvent}

                              Name   : VmMacAssignedEvent

                               

                               

                               

                               

                              Values : {VmCreatedEvent}

                              Count  : 2

                              Group  : {VMware.Vim.VmCreatedEvent, VMware.Vim.VmCreatedEvent}

                              Name   : VmCreatedEvent

                               

                               

                               

                               

                              Values : {DatastoreFileCopiedEvent}

                              Count  : 1

                              Group  : {VMware.Vim.DatastoreFileCopiedEvent}

                              Name   : DatastoreFileCopiedEvent

                               

                               

                               

                               

                              Values : {AlarmActionTriggeredEvent}

                              Count  : 19

                              Group  : {VMware.Vim.AlarmActionTriggeredEvent, VMware.Vim.AlarmActionTriggeredEvent, VMware.Vim.AlarmActionTriggeredEvent,

                                       VMware.Vim.AlarmActionTriggeredEvent...}

                              Name   : AlarmActionTriggeredEvent

                               

                               

                               

                               

                              Values : {AlarmEmailCompletedEvent}

                              Count  : 16

                              Group  : {VMware.Vim.AlarmEmailCompletedEvent, VMware.Vim.AlarmEmailCompletedEvent, VMware.Vim.AlarmEmailCompletedEvent,

                                       VMware.Vim.AlarmEmailCompletedEvent...}

                              Name   : AlarmEmailCompletedEvent

                               

                               

                               

                               

                              Values : {VmMigratedEvent}

                              Count  : 1

                              Group  : {VMware.Vim.VmMigratedEvent}

                              Name   : VmMigratedEvent

                               

                               

                               

                               

                              Values : {VimAccountPasswordChangedEvent}

                              Count  : 3

                              Group  : {VMware.Vim.VimAccountPasswordChangedEvent, VMware.Vim.VimAccountPasswordChangedEvent,

                                       VMware.Vim.VimAccountPasswordChangedEvent}

                              Name   : VimAccountPasswordChangedEvent

                               

                               

                               

                               

                              Values : {UserPasswordChanged}

                              Count  : 3

                              Group  : {VMware.Vim.UserPasswordChanged, VMware.Vim.UserPasswordChanged, VMware.Vim.UserPasswordChanged}

                              Name   : UserPasswordChanged

                               

                               

                               

                               

                              Values : {DatastoreFileDeletedEvent}

                              Count  : 4

                              Group  : {VMware.Vim.DatastoreFileDeletedEvent, VMware.Vim.DatastoreFileDeletedEvent, VMware.Vim.DatastoreFileDeletedEvent,

                                       VMware.Vim.DatastoreFileDeletedEvent}

                              Name   : DatastoreFileDeletedEvent

                               

                               

                               

                               

                              Values : {VmDisconnectedEvent}

                              Count  : 52

                              Group  : {VMware.Vim.VmDisconnectedEvent, VMware.Vim.VmDisconnectedEvent, VMware.Vim.VmDisconnectedEvent,

                                       VMware.Vim.VmDisconnectedEvent...}

                              Name   : VmDisconnectedEvent

                               

                               

                               

                               

                              Values : {HostConnectionLostEvent}

                              Count  : 3

                              Group  : {VMware.Vim.HostConnectionLostEvent, VMware.Vim.HostConnectionLostEvent, VMware.Vim.HostConnectionLostEvent}

                              Name   : HostConnectionLostEvent

                               

                               

                               

                               

                              Values : {HostSyncFailedEvent}

                              Count  : 3

                              Group  : {VMware.Vim.HostSyncFailedEvent, VMware.Vim.HostSyncFailedEvent, VMware.Vim.HostSyncFailedEvent}

                              Name   : HostSyncFailedEvent

                               

                               

                               

                               

                              Values : {AlarmSnmpCompletedEvent}

                              Count  : 3

                              Group  : {VMware.Vim.AlarmSnmpCompletedEvent, VMware.Vim.AlarmSnmpCompletedEvent, VMware.Vim.AlarmSnmpCompletedEvent}

                              Name   : AlarmSnmpCompletedEvent

                               

                               

                               

                               

                              Values : {VmConnectedEvent}

                              Count  : 58

                              Group  : {VMware.Vim.VmConnectedEvent, VMware.Vim.VmConnectedEvent, VMware.Vim.VmConnectedEvent,

                                       VMware.Vim.VmConnectedEvent...}

                              Name   : VmConnectedEvent

                               

                               

                               

                               

                              Values : {HostConnectedEvent}

                              Count  : 3

                              Group  : {VMware.Vim.HostConnectedEvent, VMware.Vim.HostConnectedEvent, VMware.Vim.HostConnectedEvent}

                              Name   : HostConnectedEvent

                               

                               

                               

                               

                              Values : {DrsResourceConfigureFailedEvent}

                              Count  : 6

                              Group  : {VMware.Vim.DrsResourceConfigureFailedEvent, VMware.Vim.DrsResourceConfigureFailedEvent,

                                       VMware.Vim.DrsResourceConfigureFailedEvent, VMware.Vim.DrsResourceConfigureFailedEvent...}

                              Name   : DrsResourceConfigureFailedEvent

                               

                               

                               

                               

                              Values : {DrsResourceConfigureSyncedEvent}

                              Count  : 2

                              Group  : {VMware.Vim.DrsResourceConfigureSyncedEvent, VMware.Vim.DrsResourceConfigureSyncedEvent}

                              Name   : DrsResourceConfigureSyncedEvent

                               

                               

                               

                               

                              Values : {VmRemovedEvent}

                              Count  : 2

                              Group  : {VMware.Vim.VmRemovedEvent, VMware.Vim.VmRemovedEvent}

                              Name   : VmRemovedEvent

                               

                               

                               

                               

                              Values : {MigrationResourceWarningEvent}

                              Count  : 8

                              Group  : {VMware.Vim.MigrationResourceWarningEvent, VMware.Vim.MigrationResourceWarningEvent,

                                       VMware.Vim.MigrationResourceWarningEvent, VMware.Vim.MigrationResourceWarningEvent...}

                              Name   : MigrationResourceWarningEvent

                               

                               

                               

                               

                              Values : {VmMessageEvent}

                              Count  : 2

                              Group  : {VMware.Vim.VmMessageEvent, VMware.Vim.VmMessageEvent}

                              Name   : VmMessageEvent

                               

                               

                               

                               

                              Values : {VmConfigMissingEvent}

                              Count  : 12

                              Group  : {VMware.Vim.VmConfigMissingEvent, VMware.Vim.VmConfigMissingEvent, VMware.Vim.VmConfigMissingEvent,

                                       VMware.Vim.VmConfigMissingEvent...}

                              Name   : VmConfigMissingEvent

                               

                               

                               

                               

                              Values : {AlarmClearedEvent}

                              Count  : 1

                              Group  : {VMware.Vim.AlarmClearedEvent}

                              Name   : AlarmClearedEvent

                               

                               

                               

                               

                              Values : {BadUsernameSessionEvent}

                              Count  : 1

                              Group  : {VMware.Vim.BadUsernameSessionEvent}

                              Name   : BadUsernameSessionEvent

                              • 14. Re: list VM's in a datastore with powered off state and the date they was powered off ? using powerCli
                                LucD Guru
                                vExpertUser ModeratorsCommunity Warriors

                                No I did mean to have one of your VMs that is currently powered off.

                                 

                                I added the event for VMs that were powered off via the Shutdown Guest OS as well.

                                Does this make a difference?

                                 

                                $datastoreName = 'MyDS'

                                 

                                Get-Datastore -Name $datastoreName | Get-Vm |

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

                                Select @{N='Datastore';E={$datastoreName}},Name,PowerState,

                                    @{N='PoweredOff';E={

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

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

                                        Sort-Object -Property CreatedTime -Descending |

                                        select -First 1 -ExpandProperty CreatedTime

                                    }} |

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

                                1 2 Previous Next