Hi,
I am unable to get the output for Last Boot (Locale) and Uptime (Locale) as it shows blank from the below script, please help.
function getTimeSpanFormatted($timespan)
{
$timeTakenStr=""
if ($timespan.Days -gt 0)
{
$timeTakenStr += "$($timespan.days) days "
}
if ($timespan.Hours -gt 0)
{
$timeTakenStr += "$($timespan.Hours) hrs "
}
if ($timespan.Minutes -gt 0)
{
$timeTakenStr += "$($timespan.Minutes) min "
}
if ($timespan.Seconds -gt 0)
{
$timeTakenStr += "$($timespan.Seconds) sec "
}
return $timeTakenStr
}
# pass a UTCTime source and it will convert to the locale (UTC+Locale Windows Timezone)
function Convert-UTCtoLocal([parameter(Mandatory=$true)][String]$UTCTime)
{
try {
$TZ = [System.TimeZoneInfo]::FindSystemTimeZoneById($strCurrentTimeZone);
$LocalTime = [System.TimeZoneInfo]::ConvertTimeFromUtc($UTCTime, $TZ);
return $LocalTime;
}
catch {
return $null;
}
}
Get-Vmhost | sort-object Name | Select-Object Name,
@{N="Last Boot (UTC)";E={$_.ExtensionData.Summary.Runtime.BootTime}},
@{N="Last Boot (Locale)";E={Convert-UTCtoLocal -UTCTime $_.ExtensionData.Sumary.Runtime.BootTime}},
@{N="Uptime (Locale)";E={getTimeSpanFormatted((Get-Date) - (Convert-UTCtoLocal -UTCTime $_ExtensionData.Summary.Runtime.BootTime))}} | ft -auto
You forgot to copy 1 line in that Convert-UTCtoLocal function, and there were a couple of typos in the calculated properties.
function getTimeSpanFormatted($timespan) {
$timeTakenStr = ""
if ($timespan.Days -gt 0) {
$timeTakenStr += "$($timespan.days) days "
}
if ($timespan.Hours -gt 0) {
$timeTakenStr += "$($timespan.Hours) hrs "
}
if ($timespan.Minutes -gt 0) {
$timeTakenStr += "$($timespan.Minutes) min "
}
if ($timespan.Seconds -gt 0) {
$timeTakenStr += "$($timespan.Seconds) sec "
}
return $timeTakenStr
}
# pass a UTCTime source and it will convert to the locale (UTC+Locale Windows Timezone)
function Convert-UTCtoLocal([parameter(Mandatory = $true)][String]$UTCTime) {
try {
$strCurrentTimeZone = (Get-WmiObject win32_timezone).StandardName
$TZ = [System.TimeZoneInfo]::FindSystemTimeZoneById($strCurrentTimeZone);
$LocalTime = [System.TimeZoneInfo]::ConvertTimeFromUtc($UTCTime, $TZ);
return $LocalTime;
} catch {
return $null;
}
}
Get-VMHost | Sort-Object Name | Select-Object Name,
@{N = "Last Boot (UTC)"; E = { $_.ExtensionData.Summary.Runtime.BootTime } },
@{N = "Last Boot (Locale)"; E = { Convert-UTCtoLocal -UTCTime $_.ExtensionData.Summary.Runtime.BootTime } },
@{N = "Uptime (Locale)"; E = { getTimeSpanFormatted((Get-Date) - (Convert-UTCtoLocal -UTCTime $_.ExtensionData.Summary.Runtime.BootTime)) } } |
Format-Table -auto
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You forgot to copy 1 line in that Convert-UTCtoLocal function, and there were a couple of typos in the calculated properties.
function getTimeSpanFormatted($timespan) {
$timeTakenStr = ""
if ($timespan.Days -gt 0) {
$timeTakenStr += "$($timespan.days) days "
}
if ($timespan.Hours -gt 0) {
$timeTakenStr += "$($timespan.Hours) hrs "
}
if ($timespan.Minutes -gt 0) {
$timeTakenStr += "$($timespan.Minutes) min "
}
if ($timespan.Seconds -gt 0) {
$timeTakenStr += "$($timespan.Seconds) sec "
}
return $timeTakenStr
}
# pass a UTCTime source and it will convert to the locale (UTC+Locale Windows Timezone)
function Convert-UTCtoLocal([parameter(Mandatory = $true)][String]$UTCTime) {
try {
$strCurrentTimeZone = (Get-WmiObject win32_timezone).StandardName
$TZ = [System.TimeZoneInfo]::FindSystemTimeZoneById($strCurrentTimeZone);
$LocalTime = [System.TimeZoneInfo]::ConvertTimeFromUtc($UTCTime, $TZ);
return $LocalTime;
} catch {
return $null;
}
}
Get-VMHost | Sort-Object Name | Select-Object Name,
@{N = "Last Boot (UTC)"; E = { $_.ExtensionData.Summary.Runtime.BootTime } },
@{N = "Last Boot (Locale)"; E = { Convert-UTCtoLocal -UTCTime $_.ExtensionData.Summary.Runtime.BootTime } },
@{N = "Uptime (Locale)"; E = { getTimeSpanFormatted((Get-Date) - (Convert-UTCtoLocal -UTCTime $_.ExtensionData.Summary.Runtime.BootTime)) } } |
Format-Table -auto
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That Worked.. thank you very much LucD 🙂