Pergunta

The Uptime script showing correct LastReboot time on LocalHost however showing incorrect LastReboot time of RemoteHost. LocalHost converting the RemoteHost time into LocalTime

Function get-Uptime {
Param([string]$computername=$env:computername)
Process{
if ($_) {$computername=$_}
    $Computerobj = "" | select ComputerName, Uptime, LastReboot
    $WOS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName
    $LastBootUpTime = $WOS.ConvertToDateTime($WOS.LastBootUpTime)
    $Uptime = (Get-Date) - $LastBootUpTime
    $day =$Uptime.Days
    $hour =$Uptime.Hours
    $minute =$Uptime.Minutes
    $second = $Uptime.Seconds
$Computerobj.ComputerName = $computername
$Computerobj.Uptime = "$day Days $hour Hours $minute Min $second Sec"
$Computerobj.LastReboot = $LastBootUpTime
$Computerobj    
}
}

*****LocalHost****** LastReboot Correctly displayed***

ComputerName : LocalHost
Uptime       : 72 Days 12 Hours 56 Min 14 Sec
LastReboot   : 1/29/2014 3:18:46 AM
TimeZone     : (UTC-05:00) Eastern Time (US & Canada)

*****RemoteHost****** LastReboot incorrectly displayed***

ComputerName : RemoteHost
Uptime       : 73 Days 12 Hours 47 Min 52 Sec
LastReboot   : 1/28/2014 3:27:55 AM

*****Actual/Correct LastReboot time on RemoteHost******

ComputerName : RemoteHost
Uptime       : 73 Days 12 Hours 47 Min 52 Sec
LastReboot   : 1/28/2014 12:27:55 AM

Note sure how I can use DATETIME to obtain the correct LastReboot time of RemoteHost

$DateTime = Get-WmiObject -Class Win32_LocalTime -ComputerName $computername

$DateTime_STR = Get-Date -Year $DateTime.Year -Month $DateTime.Month -Day $DateTime.Day -Hour $DateTime.Hour -Minute $DateTime.Minute -Second $DateTime.Second
Foi útil?

Solução

If I understand your question correctly, you want the reboot time in the remote server's localtime, but ConvertToDateTime() gives you your local system's localtime. You can work around that issue by converting the time to UTC and adding the remote server's timezone offset:

$server = '...'

$os = gwmi Win32_OperatingSystem -Computer $server

$localtime     = $os.ConvertToDateTime($os.LastBootUpTime)
$universaltime = $localtime.ToUniversalTime()
$remotetime    = $universaltime.AddMinutes($os.CurrentTimeZone)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top