Hyper-V VM start time

Its fairly easy to see how long a VM has been running – but how do you know the Hyper-V VM start time?

In Hyper-V the VM uptime is easy to find

PS> Get-VM | where State -eq 'Running'

Name       State   CPUUsage(%) MemoryAssigned(M) Uptime           Status             Version
----       -----   ----------- ----------------- ------           ------             -------
W16AS01    Running 6           1246              00:12:18.6480000 Operating normally 8.0
W16CN01    Running 0           538               00:09:18.2180000 Operating normally 8.2
W16DC01    Running 0           940               00:15:19.1550000 Operating normally 8.0
W17035CN01 Running 0           540               00:06:17.7980000 Operating normally 8.2
W1709CN01  Running 0           512               00:03:15.8540000 Operating normally 8.2

Sometimes you might want to know when the VM was started

The Uptime property is a TimeSpan so you can calculate the start time

PS> $now = Get-Date
PS> Get-VM | where State -eq 'Running' | select Name, @{N='StartTime'; E={$now - $_.Uptime}}

Name       StartTime
----       ---------
W16AS01    02/12/2017 10:23:15
W16CN01    02/12/2017 10:26:16
W16DC01    02/12/2017 10:20:15
W17035CN01 02/12/2017 10:29:16
W1709CN01  02/12/2017 10:32:18

Once you’ve added the calculated property you can use like any other property

PS> Get-VM | where State -eq 'Running' | select Name, @{N='StartTime'; E={$now - $_.Uptime}} | sort StartTime

Name       StartTime
----       ---------
W16DC01    02/12/2017 10:18:46
W16AS01    02/12/2017 10:21:47
W16CN01    02/12/2017 10:24:47
W17035CN01 02/12/2017 10:27:48
W1709CN01  02/12/2017 10:30:50

Using calculated fields like this is a handy technique for changing the way data is displayed.

Advertisements
This entry was posted in Hyper-V, Powershell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s