Hyper-V: Script to Find VM Creation Date

Hyper-V: Script to Find VM Creation Date

Ever need to determine/list the date/time of the creation of the VMs on a Hyper-V server?

You can download www.codeplex.com/pshyperv and run get-vm | select InstallDate, ElementName.  However, the dates are in the outlput not formatted as dates.

Here is a sample (last boot time on my PC using WMI) how to convert "WMI" date into a "normal date".

PS C:\Users\> $os=get-wmiobject win32_operatingsystem

PS C:\Users\> [System.Management.ManagementDateTimeConverter]::ToDateTime($os.LastBootUpTime)

October 2010. 15:05:15

If you have SCVMM, and use it's PowerShell library, get-vm | Select Name, CreationTime will produce the same output, with a readable date.

If you don't have VMM or the community cmdlets, then Get-WmiObject -namespace root\virtualization  -class msvm_computersystem | select InstallDate, ElementName.

 You can alse use Windows PowerShell, as in this example:

.\Get-CreationTime.ps1 –HostName <Hyper-V HOST NAME>

param([string]$hostName)

function WMIDateStringToDateTime( [String] $strWmiDate )
{
    $strWmiDate.Trim() > $null
    $iYear   = [Int32]::Parse($strWmiDate.SubString( 0, 4))
    $iMonth  = [Int32]::Parse($strWmiDate.SubString( 4, 2))
    $iDay    = [Int32]::Parse($strWmiDate.SubString( 6, 2))
    $iHour   = [Int32]::Parse($strWmiDate.SubString( 8, 2))
    $iMinute = [Int32]::Parse($strWmiDate.SubString(10, 2))
    $iSecond = [Int32]::Parse($strWmiDate.SubString(12, 2))
    # decimal point is at $strWmiDate.Substring(14, 1)
    $iMicroseconds = [Int32]::Parse($strWmiDate.Substring(15, 6))
    $iMilliseconds = $iMicroseconds / 1000
    $iUtcOffsetMinutes = [Int32]::Parse($strWmiDate.Substring(21, 4))
    if ( $iUtcOffsetMinutes -ne 0 )
    {
        $dtkind = [DateTimeKind]::Local
    }
    else
    {
        $dtkind = [DateTimeKind]::Utc
    }
    return New-Object -TypeName DateTime `
                      -ArgumentList $iYear, $iMonth, $iDay, `
                                    $iHour, $iMinute, $iSecond, `
                                    $iMilliseconds, $dtkind
}

$VMinfo = @(get-wmiobject -computername $hostName -namespace root\virtualization Msvm_VirtualSystemGlobalSettingData)

#WMIDateStringToDateTime '$vminfo[0].creationtime'
foreach($x in $vminfo)
{
    $x.elementname
    #$x.creationtime
    if($x.creationtime)
    {
        WMIDateStringToDateTime -strWmiDate $x.creationtime
    }
    else
    {
         write-host "CreationTime NULL" -foregroundcolor "yellow" -backgroundcolor "black"
    }
    "                      "
}

 

 

 

Leave a Comment
  • Please add 3 and 6 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Ed Price - MSFT edited Revision 3. Comment: Tags

  • Craig Lussier edited Revision 2. Comment: added en-US to tags and title

Page 1 of 1 (2 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • The Creation date in VMM is the same as the "Added date" so if you remove from VMM and re-add it to VMM (the host that is) all of the VMs on the host will have their "Creation date" changed. have you found a way to stop this from happening? I know I can run creation dates on the hyper-v hosts themselves because Hyper-V manager knows the difference but it seems someone messed up when calculating the creation date for VMM. Thanks, John

  • Craig Lussier edited Revision 2. Comment: added en-US to tags and title

  • Ed Price - MSFT edited Revision 3. Comment: Tags

  • nice

  • nice

  • nice

Page 1 of 1 (6 items)