none
ConverttoDateTime

    Question

  • Hi Guys,

    I'm wondering if anyone can point me in the right direction here, i'm putting together a script to get me the system uptime of a few servers.

    The script appears to work fine although it claims our servers were last rebooted in the future ?!?

    What appear to happen is it gets mixed up between AM and PM.

     

    The script is as follows (i know its not pretty)

    ---------------------------

    $Computer = get-content c:\serverlist.txt
    $Reboot = GWMI -Cl Win32_OperatingSystem -computer $computer -filter "lastbootuptime is not null"

    foreach ($system in $reboot)

           {    
            $Date = $system.ConvertToDateTime($system.LastBootUpTime)
            Write-Host $system.csname "was restarted on:" $date
            $now = Get-Date
         $Uptime = $now - $date
         $d = $Uptime.Days
         $h = $Uptime.Hours
        
        write-host $system.csname "System Up for:" $d "days &",$h "hours"}

    -----------------------

    The Output is

    -----------------------

    APP01 was restarted on: 16/09/2010 1:02:43 p.m.
    APP01 System Up for: 0 days & 23 hours
    APP03 was restarted on: 17/09/2010 2:04:46 p.m.
    APP03 System Up for: 0 days & -1 hours
    ------------------------

     

    I've confirmed these servers were rebooted at 1.02am and 2.04am, defintley not during the day.

    Any help is appreciated, thanks.

    Friday, September 17, 2010 12:34 AM

All replies

  • Are the region settings on the servers correct?
    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Friday, September 17, 2010 2:04 AM
  • Yup they all look ok.  a few with the wrong keyboard settings but all correct on TimeZone and Locale.


    Monday, September 20, 2010 2:48 AM
  • Any chance your local timezone is 12 hours offset from GMT?


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Monday, September 20, 2010 2:52 AM
  • Sure is, Good old New Zealand time.
    Monday, September 20, 2010 3:09 AM
  • I suspect you're doing datetime math using one value in GMT/UTC (from WMI)  and another that's localized (from get-date), and coming up 12 hours off, which looks like an AM/PM issue. 
    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Monday, September 20, 2010 3:12 AM
  • OK, so it must be something on the server.  All our timezones are set to NZ time.

    I guess the math part is working correctly its just getting fed the wrong restart time, i presume then that the convertToDateTime is getting something confused.  Unfortunatley i can't decipher the value stored within that wmi object manually to get the local value.

    Monday, September 20, 2010 3:20 AM
  • If converToDateTime isn't localizing the result, you can convert $now to UTC, so that both of your timestamps are in the same time zone.  That should make your uptime come out right.

    $Uptime = $now.touniversaltime() - $date


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Monday, September 20, 2010 3:29 AM
  • that gave me server uptime of -10 hours :-)

     

    however if i do this:

    $Date = $system.ConvertToDateTime($system.LastBootUpTime)

    $date2 = $date.touniversaltime()

    It then give me 2am restarts instead of 2pm which looks much better..

    Monday, September 20, 2010 3:46 AM
  • Hm.  Well, whatever works.  I''m at GMT -5, and it's bedtime.  I might poke at it some tomorrow if I get a chance. 

    At least you have a solution that works.  I checked the documentaion on that LasBootUpTime property, but didn't seen anything definite on how that time is returned, so you may just have to experiment with it.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Monday, September 20, 2010 4:22 AM
  • Try this:

    $FilePath = "D:\Computers.txt"
    
    Get-WmiObject -Class Win32_OperatingSystem -ComputerName (Get-Content -Path $FilePath) -Filter "LastBootUpTime Is Not Null" |
    ForEach-Object {
    
    $LastBootUpTime = [DateTimeOffset]::Parse([Management.ManagementDateTimeConverter]::ToDateTime($_.LastBootUpTime).ToString())
    $SystemUpTime = New-TimeSpan -Start $LastBootUpTime.UtcDateTime -End ([DateTime]::UtcNow)
    
    Write-Host ("{0} Last Boot Up Time (UTC): {1}" -f $_.CSName, $LastBootUpTime.UtcDateTime)
    Write-Host ("{0} System Up Time: {1} Days {2} Hours {3} Minutes" -f $_.CSName, $SystemUpTime.Days, $SystemUpTime.Hours, $SystemUpTime.Minutes)
    
    }
    Monday, September 20, 2010 10:08 AM
  • Thanks Serge,

     

    That gets the correct reboot time but must screw up the actual time, it returns:

     

    APP01 Last Boot Up Time (UTC): 16/09/2010 1:02:43 a.m.
    APP01 System Up Time: 4 Days 19 Hours 45 Minutes


    APP03 Last Boot Up Time (UTC): 21/09/2010 2:05:04 a.m.
    APP03 System Up Time: 0 Days -5 Hours -16 Minutes


    APP04 Last Boot Up Time (UTC): 21/09/2010 2:06:44 a.m.
    APP04 System Up Time: 0 Days -5 Hours -17 Minutes

    Monday, September 20, 2010 8:56 PM
  • Funny. Try this:

    $FilePath = "D:\Computers.txt"
    
    Get-Content -Path $FilePath | ForEach-Object {
    
    $LastBootUpTime = [Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject -Class Win32_OperatingSystem -ComputerName $_).LastBootUpTime)
    $SystemUpTime = New-TimeSpan -Seconds (Get-WmiObject -Class Win32_PerfFormattedData_PerfOS_System -ComputerName $_).SystemUpTime
    
    Write-Host ("{0} Last Boot Up Time (UTC): {1}" -f $_, $LastBootUpTime.ToUniversalTime())
    Write-Host ("{0} System Up Time: {1} Days {2} Hours {3} Minutes" -f $_, $SystemUpTime.Days, $SystemUpTime.Hours, $SystemUpTime.Minutes)
    
    }
    
    Tuesday, September 21, 2010 8:57 AM
  • There is a bug in ConverttoDateTime() ScriptMethod in which it ignores time zones. Under the cover ConverttoDateTime() calls [System.Management.ManagementDateTimeConverter]::ToDateTime() should return DateTime will be in the current time zone of the system. But ConverttoDateTime() does not obey this.
    Analysis and C# equivalent code at Powershell ConvertToDateTime Method Definition and Implementation Error.
    Friday, April 21, 2017 4:34 PM