VMware Cloud Community
jrhodes
Contributor
Contributor
Jump to solution

Last VM powerOn

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?

Jim

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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


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

View solution in original post

Reply
0 Kudos
22 Replies
Niket
Enthusiast
Enthusiast
Jump to solution

Hi,

Connect-VIServer -Server

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

I hope it helps you.

Thanks

Niket

Reply
0 Kudos
jrhodes
Contributor
Contributor
Jump to solution

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

dsy12512kw

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you try this:

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


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

jrhodes
Contributor
Contributor
Jump to solution

Thanks

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?

Jim

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

Reply
0 Kudos
jrhodes
Contributor
Contributor
Jump to solution

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.

Jim

Reply
0 Kudos
Salalon
Contributor
Contributor
Jump to solution

hi all,

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

Thx.

Regards,

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
Salalon
Contributor
Contributor
Jump to solution

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'

Regards,

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Do

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

from the prompt before you start the script.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
Salalon
Contributor
Contributor
Jump to solution

hi,

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;

Regards,

Reply
0 Kudos
tdubb123
Expert
Expert
Jump to solution

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

t

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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?


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

Reply
0 Kudos
tdubb123
Expert
Expert
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try changing to -MaxSamples ([int]::MaxValue)


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

Reply
0 Kudos
tdubb123
Expert
Expert
Jump to solution

I cant seem to get the Names of the VM column come up with $vm.name

Reply
0 Kudos
tdubb123
Expert
Expert
Jump to solution

instead of the powered On value, what would be the value to check for powered Off?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

Get-VM MyVM | Get-VIEvent -Types Info -MaxSamples ([int]::MaxValue) |

  Where-Object {$_.fullFormattedMessage -match "Powered Off"} | `

  Sort-Object -property createdTime |  select -last 1 | %{

    Write-Host $_.vm.name $_.createdTime | Out-Default

  }


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

Reply
0 Kudos
tdubb123
Expert
Expert
Jump to solution

thanks lucd,

why I am getting these errors

The request failed with HTTP stastus 501: Not implemented on some vms?

Reply
0 Kudos