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
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.
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
where is $lastpo defined ?
you have to query the events : Last Power Off / Guest OS Shutdown Date - PowerCLI
Moderator: Moved to PowerCLI
Could you correct the code for me? im really noob hahaha
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.
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
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
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
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
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.
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
Nice, thanks for all LucD
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
But.. i have the same valor with valor (999999) that (-7)
Why?
In the valor 9999 i see a 1 VM more.
try with smaller negative values. -99999 days points to the year 1746 - for a minimum i would start with 1.1.1970
But with the valor -7000. ,my report (CSV) is empty
Why?
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
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
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
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.
i can confirm that the code is working and giving valid results with days -7000 and greater. no results wih -99999
Can you check what the retention date is in your environment?
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