deensolea
Contributor
Contributor

powercli script to get last boottime of guest OS

Jump to solution

hi

can I get some help with a script to get last boottime of guest OS (Win7 and 10) for multiple VMs.

thanks

1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Just came to realise that we can use the Realtime value, that is in fact even more precise.

Something like this

$stat = 'sys.osuptime.latest'

$now = Get-Date

$vms = Get-VM

Get-Stat -Entity $vms -Stat $stat -Realtime -MaxSamples 1 |

Select @{N='VM';E={$_.Entity.Name}},

    @{N='LastOSBoot';E={$now.AddSeconds(- $_.Value)}},

    @{N='UptimeDays';E={[math]::Floor($_.Value/(24*60*60))}}


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

View solution in original post

16 Replies
RvdNieuwendijk
Leadership
Leadership

You can use the following PowerShell command to get the last boot time of multiple systems:

Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName Server01,Server02 | Select-Object -Property CSName,LastBootUpTime

Replace Server01,Server02 with the names of the machines you want to retrieve the last boot time from.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
LucD
Leadership
Leadership

Try like this

$code = @'

Get-CimInstance -ClassName win32_operatingsystem | select -ExpandProperty lastbootuptime

'@

Get-VM | where{$_.PowerState -eq 'PoweredOn' -and $_.Guest.GuestFamily -match 'windows'} |

Select @{N='VM';E={$_.Name}},

    @{N='LastBoot';E={[DateTime](Invoke-VMScript -VM $_ -ScriptText $code -ScriptType Powershell).ScriptOutput}}


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

0 Kudos
deensolea
Contributor
Contributor

Get-CimInstance : The client cannot connect to the destination specified in the request. Verify that the service on

the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service

running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following

command on the destination to analyze and configure the WinRM service: "winrm quickconfig".

At line:1 char:1

+ Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName NLAMSV ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ConnectionError: (root\cimv2:Win32_OperatingSystem:String) [Get-CimInstance], CimExcepti

0 Kudos
deensolea
Contributor
Contributor

hi LucD

since VMtools Is outdated on some VMs getting warnings and no output. can we modify the below to get boottime instead of uptime ?

$stat = 'sys.osuptime.latest'

$entity = Get-VM (get-content servers.txt)

Get-Stat -Entity $entity -Stat $stat -Realtime -MaxSamples 1 -ErrorAction SilentlyContinue | Select @{N='VM';E={$_.Entity.Name}}, @{N='Uptime (d.hh:mm:ss)';E={[timespan]::FromSeconds($_.value)}}

this I got from you and works perfectly in getting uptime.

0 Kudos
LucD
Leadership
Leadership

There is a difference between the VM's Uptime (time the VM was started) and the guest OS last boottime (last time the guest OS was started).

The VM can be kept running, while the guest OS is rebooted.

So these can be two completely different timestamps.

The guest OS boottime can only be retrieved from within the guest OS, hence the Get-CimInstance solution for a Windows guest OS.

Afaik, vSphere doesn't keep track (events/metric) of the last time the guest OS was restarted.

The only solution is to query the guest OS I'm afraid.


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

0 Kudos
deensolea
Contributor
Contributor

so you mean

$stat = 'sys.osuptime.latest'

only gets the VM uptime and not OS uptime ?

thanks in advance

0 Kudos
LucD
Leadership
Leadership

Not sure to be honest, but are you running with Statistics level 4 on one or more of your Historical intervals?

uptime.png


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

0 Kudos
deensolea
Contributor
Contributor

its at level 1, by looks.

although not sure where to look for ?

0 Kudos
deensolea
Contributor
Contributor

iam checking here

web console>vcenter server>manage>general>statistics interval

0 Kudos
LucD
Leadership
Leadership

You can also check with

$si = Get-View ServiceInstance

$perfMgr = Get-View -Id $si.Content.PerfManager

$perfMgr.HistoricalInterval | Select Name,Level


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

0 Kudos
deensolea
Contributor
Contributor

confirm level 1

0 Kudos
LucD
Leadership
Leadership

Just came to realise that we can use the Realtime value, that is in fact even more precise.

Something like this

$stat = 'sys.osuptime.latest'

$now = Get-Date

$vms = Get-VM

Get-Stat -Entity $vms -Stat $stat -Realtime -MaxSamples 1 |

Select @{N='VM';E={$_.Entity.Name}},

    @{N='LastOSBoot';E={$now.AddSeconds(- $_.Value)}},

    @{N='UptimeDays';E={[math]::Floor($_.Value/(24*60*60))}}


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

View solution in original post

deensolea
Contributor
Contributor

as always works like charm !!!

Smiley Happy

0 Kudos
Sharkie405
Contributor
Contributor
Can someone tell me how I would get this output to a csv file or even just a text file?
0 Kudos
LucD
Leadership
Leadership

Try like this

$stat = 'sys.osuptime.latest'

$now = Get-Date

$vms = Get-VM

Get-Stat -Entity $vms -Stat $stat -Realtime -MaxSamples 1 |

Select @{N='VM';E={$_.Entity.Name}},

    @{N='LastOSBoot';E={$now.AddSeconds(- $_.Value)}},

    @{N='UptimeDays';E={[math]::Floor($_.Value/(24*60*60))}} |

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


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

Sharkie405
Contributor
Contributor

Thank you so much for that quick reply! That worked perfectly!

0 Kudos