How do I add timestamps to individual lines of PowerShell & output?

后端 未结 4 1406
忘了有多久
忘了有多久 2020-12-29 05:11

How, if at all, is it possible to add timestamps to each line of an output generated by the & PowerShell operator?

Example:

PS H:\\&         


        
4条回答
  •  旧巷少年郎
    2020-12-29 05:25

    For anyone that is looking for more information on filter, here is the documentation. It was surprising difficult to find since searching any combination of the word "filter" and "powershell" will give a million examples and no documentation. Also help filter in powershell provides no obvious help either.

    The answer provided by mjolinor is the best way to do something like this, but I wanted to expand on it.

    filter timestamp {"$(Get-Date): $_"}
    

    Is a shortcut for calling this

    function timestamp { Process{"$(Get-Date): $_"} }
    

    Both of these create named functions that accept input from the pipeline. Run help pipline in powershell to learn more. The pipeline will operate on a single object at a time, and that object can be referred to with the automatic variable $_. So each function will iterate over every item in the pipeline that was piped to it using the | pipe character.

    This behaves differently than a normal function in that it's working on the objects as they arrive, instead of all at once. For example running

    function timestamp {
            "$(Get-Date): $input"
    }
    $result = & ping 127.0.0.1
    $result | timestamp
    

    Would dump the entire $result object on a single line and result in a response that looks like this

    03/14/2018 15:23:16:  Pinging 127.0.0.1 with 32 bytes of data: Reply from 127.0.0.1: b
    ytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Reply from 12
    7.0.0.1: bytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128  Pi
    ng statistics for 127.0.0.1:     Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), 
    Approximate round trip times in milli-seconds:     Minimum = 0ms, Maximum = 0ms, Avera
    ge = 0ms
    

    Since the function is operating on the object as a whole you would have to iterate over each line. Changing it to this

    function timestampfunction {
        foreach ($i in $input){
            "$(Get-Date): $i"
        }
    }
    

    Would give you the nicely formatted

    03/14/2018 15:23:16: 
    03/14/2018 15:23:16: Pinging 127.0.0.1 with 32 bytes of data:
    03/14/2018 15:23:16: Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
    03/14/2018 15:23:16: Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
    03/14/2018 15:23:16: Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
    03/14/2018 15:23:16: Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
    03/14/2018 15:23:16: 
    03/14/2018 15:23:16: Ping statistics for 127.0.0.1:
    03/14/2018 15:23:16:     Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    03/14/2018 15:23:16: Approximate round trip times in milli-seconds:
    03/14/2018 15:23:16:     Minimum = 0ms, Maximum = 0ms, Average = 0ms
    

    Here is a nicely written article on the differences between these approaches.

提交回复
热议问题