VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Last Boot (Locale) and Uptime (Locale) output shows blank

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

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

That Worked.. thank you very much LucD 🙂

0 Kudos