AndygRomo
Contributor
Contributor

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

Jump to solution

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 Solution

Accepted Solutions
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
19 Replies
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

PabloJAguilar20
Enthusiast
Enthusiast

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

Pablo J Aguilar Consultor de Infraestructuras Virtuales. Buenos Aires - Argentina
0 Kudos
LucD
Leadership
Leadership

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

    }}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

AndygRomo
Contributor
Contributor

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

0 Kudos
LucD
Leadership
Leadership

My last entry added that.

Or doesn't it work?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
AndygRomo
Contributor
Contributor

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

0 Kudos
AndygRomo
Contributor
Contributor

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

0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
AndygRomo
Contributor
Contributor

Great this out puts but no dates in any fields

0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
LucD
Leadership
Leadership

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}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
AndygRomo
Contributor
Contributor

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

0 Kudos
AndygRomo
Contributor
Contributor

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

0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
AndygRomo
Contributor
Contributor

Thank you LucD that works I had to power on a VM then back off for it to show in the output this is very helpful ...Im new to PowerCli so this is all good for me

0 Kudos
LucD
Leadership
Leadership

That could mean that you are not retaining the Tasks and Events for a longer time.

You can check if the retention (Tasks and Events) is enabled and for how many days with

Get-AdvancedSetting -Entity $global:DefaultVIServer -Name 'event.maxAge','event.maxAgeEnabled','task.maxAge','task.maxAgeEnabled'


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

vmk2014
Expert
Expert

LucD,

I have a list of powered-off VM's and want to delete to reclaim space,but before that want to check VM's residing on which data store name (e.g. DS_VMAX_Win) and second from the powered-off list VM's with data store name, because  and powered-off date. 3rd, if possible to find out VM's residing on a data store name ( VM's running on VMAX_Oracle) are coming from which storage array. Example VMAX1011 or powremax2025 etc.

thanks

v

0 Kudos
LucD
Leadership
Leadership

Please create a new thread for your question.
Appending your questions to existing threads is very confusing for other users who search these communities.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vmk2014
Expert
Expert

Sorry, LucD. will do now

0 Kudos