Windows Vista Forums

Format-List Problem?

  1. #1


    Ward Guest

    Format-List Problem?

    Hi,

    I am running the following powershell command:


    get-alert | where {$_.ResolutionState -eq 0 } | Format-List
    MonitoringObjectDisplayName,Name,ResolutionState,RepeatCount,TimeRaised


    Which displays the following:

    MonitoringObjectDisplayName : SQL1
    Name : The SQL Server Service Broker or Database
    Mirroring
    transport is disabled or not configured
    ResolutionState : 0
    RepeatCount : 1
    TimeRaised : 20/11/2007 6:41:23 PM

    The problem is the TimeRaised field is no in LocalTime, it is GMT time I
    suspect. I want to convert it
    to my local time. (In my case GMT+10) if I try the following it does not work

    get-alert | where {$_.ResolutionState -eq 0 } | Format-List
    MonitoringObjectDisplayName,Name,ResolutionState,RepeatCount,TimeRaised.ToLocalTime()


    MonitoringObjectDisplayName : SQL1
    Name : The SQL Server Service Broker or Database
    Mirroring
    transport is disabled or not configured
    ResolutionState : 0
    RepeatCount : 1

    ** No TimeRaised field **

    Any suggestions on the syntax to make this work?

    Thanks,

    Ward




      My System SpecsSystem Spec

  2. #2


    Kiron Guest

    Re: Format-List Problem?

    Use a calculated property:

    @{label = <[string]>; expression = <[scriptBlock]>}

    get-alert | where {$_.ResolutionState -eq 0 } | Format-List MonitoringObjectDisplayName, Name, ResolutionState, RepeatCount, @{label = 'TimeRaised'; expression = {$_.TimeRaised.ToLocalTime()}}


    --
    Kiron

      My System SpecsSystem Spec

  3. #3


    Ward Guest

    Re: Format-List Problem?

    Kiron,

    Awsome!! Thanks that was great. For my understanding can you tell me why my
    method would not of worked. Is it backause you cannot call a function on the
    same line as a format-list argument. Sorry badly phrased quesiton not sure
    how else to ask it

    Ward


      My System SpecsSystem Spec

  4. #4


    Kiron Guest

    Re: Format-List Problem?

    The -property parameter takes the property names you want to display, these names are not objects that have methods or properties you can call. PowerShell Format-* Cmdlets let you create calculated properties to access members (methods/properties) of the current object in the pipe '$_'.

    --
    Kiron

      My System SpecsSystem Spec

  5. #5


    Hal Rottenberg Guest

    Re: Format-List Problem?

    Ward wrote:

    > Kiron,
    >
    > Awsome!! Thanks that was great. For my understanding can you tell me why my
    > method would not of worked. Is it backause you cannot call a function on the
    > same line as a format-list argument. Sorry badly phrased quesiton not sure
    > how else to ask it
    get-alert | where {$_.ResolutionState -eq 0 } | Format-List
    MonitoringObjectDisplayName,Name,ResolutionState,RepeatCount,TimeRaised.ToLocalTime()

    It's because what you were trying to operate on is not an object. It's just a
    bit of text in a set of parameters. To do something like what you are trying to
    do, you would have to use sub-expression notation $() , i.e.

    $a | format-list One,Two,Three,$( get-time )

    But that isn't really what you wanted to do, using a calculated expression like
    Kiron says is probably the best way.
    --

    Hal Rottenberg
    Blog: http://halr9000.com
    Webmaster, Psi (http://psi-im.org)
    Co-host, PowerScripting Podcast (http://powerscripting.net)

      My System SpecsSystem Spec

  6. #6


    Ward Guest

    Re: Format-List Problem?

    Hal / Kiron,

    Thanks great explinations.

    So does that mean that:

    get-alert | where {$_.ResolutionState -eq 0 } | Format-List
    MonitoringObjectDisplayName, Name, ResolutionState, RepeatCount,
    $(TimeRaised.ToLocalTime())

    would work as well?

    And how to extend the example a bit better if I am allowed to ask, is there
    an easy way I can get a datediff between that calculated date and the current
    date and time?

    Thanks,

    Ward

      My System SpecsSystem Spec

  7. #7


    Brandon Shell [MVP] Guest

    Re: Format-List Problem?

    The $_ is required, because you are performing the Format-List on each item
    in pipeline.

    Do date diff try something like this

    Format-List MonitoringObjectDisplayName, Name, ResolutionState, RepeatCount,@{l='TimeDifference';e={($_.TimeRaised.ToLocalTime()).Subtract($_.TimeRaised)}}

    Brandon Shell
    ---------------
    Blog: http://www.bsonposh.com/
    PSH Scripts Project: www.codeplex.com/psobject

    W> Hal / Kiron,
    W>
    W> Thanks great explinations.
    W>
    W> So does that mean that:
    W>
    W> get-alert | where {$_.ResolutionState -eq 0 } | Format-List
    W> MonitoringObjectDisplayName, Name, ResolutionState, RepeatCount,
    W> $(TimeRaised.ToLocalTime())
    W>
    W> would work as well?
    W>
    W> And how to extend the example a bit better if I am allowed to ask, is
    W> there an easy way I can get a datediff between that calculated date
    W> and the current date and time?
    W>
    W> Thanks,
    W>
    W> Ward
    W>



      My System SpecsSystem Spec

  8. #8


    Shay Levi Guest

    Re: Format-List Problem?

    After seeing Brandon's solution, I'm not sure what exactly did you mean by
    "current date and time". I can also interpret it as:

    .... {(get-date).Subtract($_.TimeRaised.ToLocalTime())}}

    You decide :-)


    -----
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com



    > Hal / Kiron,
    >
    > Thanks great explinations.
    >
    > So does that mean that:
    >
    > get-alert | where {$_.ResolutionState -eq 0 } | Format-List
    > MonitoringObjectDisplayName, Name, ResolutionState, RepeatCount,
    > $(TimeRaised.ToLocalTime())
    >
    > would work as well?
    >
    > And how to extend the example a bit better if I am allowed to ask, is
    > there an easy way I can get a datediff between that calculated date
    > and the current date and time?
    >
    > Thanks,
    >
    > Ward
    >



      My System SpecsSystem Spec

Format-List Problem?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Get-ACL, format the list and get groupmembership Jesper Hansen PowerShell 2 23 Sep 2009
-f format Format-Table Format-List against Select Personne PowerShell 5 25 Nov 2008
Format-List Issue Per Moeller-Olsen PowerShell 8 29 Mar 2008
Format List on PromptForChoice zfan PowerShell 4 29 Feb 2008
How to get a list of PropertyName from the format-list cmdlet? Dung K Hoang PowerShell 4 07 Oct 2006