Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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 an output, which is 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"
}
" "
}