ToShortTimeString() vs .Converttodatetime( )

This topic contains 7 replies, has 3 voices, and was last updated by Profile photo of H Man H Man 2 years, 5 months ago.

  • Author
    Posts
  • #16647
    Profile photo of H Man
    H Man
    Participant

    Hi All

    I am doing a wmi query (Get-WmiObject -Class Win32_OperatingSystem) and im looking to format the LastBootUpTime

    Select-Object CSName,@{ Name = "Last Restarted On" ; Expression = { $_.Converttodatetime( $_.LastBootUpTime ) } }

    I am currently using the Converttodatetime( ) method, is there any way to use the ToShortTimeString() , im not opposed to builidng a hash table with that method either . im looking for just the Time no seconds .eg 11:00

    thank you

  • #16648
    Profile photo of H Man
    H Man
    Participant

    Ill be using this rt do some conditional html formatting

    [xml]$html = $Reboot   | ConvertTo-Html -fragment
    
    #check each row, skipping the TH header row
    for ($i=1;$i -le $html.table.tr.count-1;$i++) {
      $class = $html.CreateAttribute("class")
      #check the value of the last column and assign a class to the row
      if (($html.table.tr[$i].td[-1] -as [datetime]) -notlike '*11:00*') {                                          
        $class.value = "danger"  
        $html.table.tr[$i].Attributes.Append($class) | Out-Null
      }
      
    } 
    
  • #16649
    Profile photo of H Man
    H Man
    Participant

    not sure if my conditional statement is on point

  • #16650
    Profile photo of H Man
    H Man
    Participant

    im looking to mark red all computers that didn't reboot @ 11:00

  • #16651
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You should be able to do this:

    $_.Converttodatetime($_.LastBootUpTime).ToShortTimeString()
    
  • #16652
    Profile photo of H Man
    H Man
    Participant

    thanks again Dave! really appreciate your help today

  • #16653
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Also, if you query the class using Get-CimInstance, you'll get a DateTime by default.

    For future posts, please try not to reply to your own post. A lot of us look for "topics without a reply" as a way of finding posts that need answered; when you reply to your own topic, sometimes that keeps your topic from being seen by folks who might be able to help. Instead, try to think about your post and include all the needed information up front. You can edit the original post to add more information, if something occurs to you before someone is able to reply. Thanks.

  • #16654
    Profile photo of H Man
    H Man
    Participant

    ok Don Will do ! and thanks for the tip

You must be logged in to reply to this topic.