Remotehost的LastBoot时间不正确

时间:2014-04-11 20:36:10

标签: powershell powershell-v2.0 powershell-v3.0 powershell-remoting

Uptime脚本在LocalHost上显示正确的LastReboot时间,但显示RemoteHost的LastReboot时间不正确。 LocalHost将RemoteHost时间转换为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

请注意我如何使用DATETIME获取RemoteHost的正确LastReboot时间

$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

1 个答案:

答案 0 :(得分:1)

如果我正确理解您的问题,您希望在远程服务器的本地时间重启时间,但ConvertToDateTime()会为您提供本地系统的本地时间。您可以通过将时间转换为UTC并添加远程服务器的时区偏移来解决该问题:

$server = '...'

$os = gwmi Win32_OperatingSystem -Computer $server

$localtime     = $os.ConvertToDateTime($os.LastBootUpTime)
$universaltime = $localtime.ToUniversalTime()
$remotetime    = $universaltime.AddMinutes($os.CurrentTimeZone)