rgonzalez2
Contributor
Contributor

Report Powered off Vmware + Date

Jump to solution

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

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

23 Replies
berndweyand
Expert
Expert

where is $lastpo defined ?

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

0 Kudos
scott28tt
VMware Employee
VMware Employee

Moderator: Moved to PowerCLI

0 Kudos
rgonzalez2
Contributor
Contributor

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

0 Kudos
LucD
Leadership
Leadership

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
rgonzalez2
Contributor
Contributor

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

0 Kudos
LucD
Leadership
Leadership

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
rgonzalez2
Contributor
Contributor

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

pastedImage_0.png

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

Thanks for your time

0 Kudos
LucD
Leadership
Leadership

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

rgonzalez2
Contributor
Contributor

Nice, thanks for all LucD

0 Kudos
LucD
Leadership
Leadership

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
rgonzalez2
Contributor
Contributor

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

Why?

In the valor 9999 i see a 1 VM more.

0 Kudos
berndweyand
Expert
Expert

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

0 Kudos
rgonzalez2
Contributor
Contributor

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

Why?

0 Kudos
LucD
Leadership
Leadership

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
rgonzalez2
Contributor
Contributor

Sorry, the machine was my mistake. But when I put 9999 or -7 both reports are the same

On the other hand, the machines that report to me are only 1 year old, so I will not get 100% of the machines turned off, right?

thanks

0 Kudos
LucD
Leadership
Leadership

It all depends on how long you keep events in your environment.

I can't explain why you would see the same result with -7 or 9999.
Or do you mean neither of these return anything?


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

0 Kudos
berndweyand
Expert
Expert

it depends on the retention time of your vcenter settings. if the value of event retention days for example is 365 days then older events are deleted from the seat-database and you can not query them.

0 Kudos
berndweyand
Expert
Expert

i can confirm that the code is working and giving valid results with days -7000 and greater. no results wih -99999

0 Kudos
LucD
Leadership
Leadership

Can you check what the retention date is in your environment?

Get-AdvancedSetting -Entity $global:defaultVIServer -Name "event.maxAge"

Get-AdvancedSetting -Entity $global:defaultVIServer -Name "event.maxAgeEnabled"


Btw, I assume you are connected to a vCenter, and not an ESXi node?


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

0 Kudos