VMware Cloud Community
deensolea
Contributor
Contributor
Jump to solution

powercli script to get last boottime of guest OS

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
Jump to solution

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

37 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

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
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
deensolea
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
deensolea
Contributor
Contributor
Jump to solution

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
deensolea
Contributor
Contributor
Jump to solution

so you mean

$stat = 'sys.osuptime.latest'

only gets the VM uptime and not OS uptime ?

thanks in advance

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
deensolea
Contributor
Contributor
Jump to solution

its at level 1, by looks.

although not sure where to look for ?

Reply
0 Kudos
deensolea
Contributor
Contributor
Jump to solution

iam checking here

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
deensolea
Contributor
Contributor
Jump to solution

confirm level 1

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

deensolea
Contributor
Contributor
Jump to solution

as always works like charm !!!

Smiley Happy

Reply
0 Kudos
Sharkie405
Contributor
Contributor
Jump to solution

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

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
Jump to solution

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

Reply
0 Kudos
mrc247_de
Enthusiast
Enthusiast
Jump to solution

Hello LucD,

I tried to add two columns to your script, running OS from VMware Tools and the contents of the Nodes field.
Unfortunately I don't have that good Powershell and scripting knowledge.
Can you tell me what I'm doing wrong?

 

$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='OS';E={$_.Guest.GuestFullName}},

@{N='Notes';E={$_.Summary.Config.Annotation}},

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

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

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

 

greetings

Marcel

 

LucD
Leadership
Leadership
Jump to solution

The VM properties are available under the Entity property of the object returned by Get-Stat.
The 2 properties you tried to add are available under the ExtensionData property of the virtualMachine object.

$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='OS';E={$_.Entity.ExtensionData.Guest.GuestFullName}},
@{N='Notes';E={$_.Entity.ExtensionData.Summary.Config.Annotation}},
@{N='LastOSBoot';E={$now.AddSeconds(- $_.Value)}},
@{N='UptimeDays';E={[math]::Floor($_.Value/(24*60*60))}} |
Export-Csv -Path .\VM_Uptime_Report.csv -NoTypeInformation -UseCulture


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

mrc247_de
Enthusiast
Enthusiast
Jump to solution

thank you very much for the quick reply and the help. Works perfect. 🙂

 

Reply
0 Kudos