hi
can I get some help with a script to get last boottime of guest OS (Win7 and 10) for multiple VMs.
thanks
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
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.
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
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
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.
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
so you mean
$stat = 'sys.osuptime.latest'
only gets the VM uptime and not OS uptime ?
thanks in advance
Not sure to be honest, but are you running with Statistics level 4 on one or more of your Historical intervals?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
its at level 1, by looks.
although not sure where to look for ?
iam checking here
web console>vcenter server>manage>general>statistics interval
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
confirm level 1
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
as always works like charm !!!
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
Thank you so much for that quick reply! That worked perfectly!
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
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
thank you very much for the quick reply and the help. Works perfect. 🙂