Datetime 如何向PowerShell&;的各行添加时间戳;输出?

Datetime 如何向PowerShell&;的各行添加时间戳;输出?,datetime,powershell,logging,process,Datetime,Powershell,Logging,Process,如果可能的话,如何将时间戳添加到由&PowerShell操作符生成的输出的每一行 例如: PS H:\> $result = & ping 192.168.1.1 PS H:\> echo $result Pinging 192.168.1.1 with 32 bytes of data: Reply from 192.168.1.1: bytes=32 time=104ms TTL=250 Reply from 192.168.1.1: bytes=32 time=106

如果可能的话,如何将时间戳添加到由
&
PowerShell操作符生成的输出的每一行

例如:

PS H:\> $result = & ping 192.168.1.1
PS H:\> echo $result

Pinging 192.168.1.1 with 32 bytes of data:
Reply from 192.168.1.1: bytes=32 time=104ms TTL=250
Reply from 192.168.1.1: bytes=32 time=106ms TTL=250
Reply from 192.168.1.1: bytes=32 time=102ms TTL=250
Reply from 192.168.1.1: bytes=32 time=102ms TTL=250
Ping statistics for 192.168.1.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 102ms, Maximum = 106ms, Average = 103ms
预期结果:

PS H:\> echo $result

2014-12-08T14:45:48.8898125+00:00:Pinging 192.168.1.1 with 32 bytes of data:
2014-12-08T14:45:48.8932661+00:00:Reply from 192.168.1.1: bytes=32 time=104ms TTL=250
2014-12-08T14:45:48.9233451+00:00:Reply from 192.168.1.1: bytes=32 time=106ms TTL=250
2014-12-08T14:45:48.9765438+00:00:Reply from 192.168.1.1: bytes=32 time=102ms TTL=250
2014-12-08T14:45:49.0233105+00:00:Reply from 192.168.1.1: bytes=32 time=102ms TTL=250
2014-12-08T14:45:49.0233201+00:00:
2014-12-08T14:45:49.0238753+00:00:Ping statistics for 192.168.1.1:
2014-12-08T14:45:49.0239210+00:00:    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
2014-12-08T14:45:49.0233318+00:00:Approximate round trip times in milli-seconds:
2014-12-08T14:45:49.0237209+00:00:    Minimum = 102ms, Maximum = 106ms, Average = 103ms
我知道如何拆分/加入PowerShell阵列,但这只能在
&
操作符完成后发生。我正在寻找更像实时性的解决方案,在&operator运行时将时间戳添加到输出中


顺便说一下,时间戳本身是
$($(Get Date-Format o)+“:”)

您可以使用一个过滤器:

filter timestamp {"$(Get-Date -Format o): $_"}
$result = & ping 192.168.1.1 | timestamp
来自
$result
的示例输出:

2014-12-08T11:42:59.2827202-05:00: 
2014-12-08T11:42:59.2857205-05:00: Pinging 192.168.1.1 with 32 bytes of data:
2014-12-08T11:43:03.1241043-05:00: Request timed out.
2014-12-08T11:43:08.1236042-05:00: Request timed out.
2014-12-08T11:43:13.1241042-05:00: Request timed out.
2014-12-08T11:43:18.1246042-05:00: Request timed out.
2014-12-08T11:43:18.1246042-05:00: 
2014-12-08T11:43:18.1246042-05:00: Ping statistics for 192.168.1.1:
2014-12-08T11:43:18.1246042-05:00:     Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

您可以将Start-Transcript cmdlet与配置文件中的自定义提示结合使用:

# Configure PS prompt to show date and time
function prompt
{ 
    $promptStringStart = "PS:" + (Get-Date -format MM/dd/yy` hh:mm:ss)
    $promptStringEnd += ">"
    Write-Host $promptStringStart -NoNewline -ForegroundColor Yellow
    Write-Host $promptStringEnd -NoNewline -ForegroundColor Yellow
    return " "
}
这在我的Windows7工作站上非常有效。然而,在一些较新的服务器上,2012 R2安装的开始记录似乎有点破损。这修复了其中的一部分:

。。。但它仍然无法解决自定义提示的问题

例如,我在控制台上看到:

PS:02/14/17 08:28:20> hostname
server463
这是写入日志的内容:

PS:02/14/17 08:28:20
>

PS>hostname
server463

对于正在查找有关
过滤器的详细信息的任何人,
。搜索“过滤器”和“powershell”这两个词的任何组合都会给出一百万个示例,而没有任何文档,这让人感到惊讶。此外,powershell中的
帮助过滤器
也没有提供明显的帮助

mjolinor提供的答案是做类似事情的最佳方式,但我想进一步说明

filter timestamp {"$(Get-Date): $_"}
这是打电话的捷径

function timestamp { Process{"$(Get-Date): $_"} }
这两个函数都创建了接受管道输入的命名函数。在powershell中运行帮助行以了解更多信息。管道一次将在单个对象上运行,该对象可以使用
$\uu
引用。因此,每个函数都将使用
|
管道字符迭代管道中的每个项目

这与普通函数的行为不同,因为它在对象到达时处理对象,而不是一次处理所有对象。比如跑步

function timestamp {
        "$(Get-Date): $input"
}
$result = & ping 127.0.0.1
$result | timestamp
将整个
$result
对象转储到一行上,并生成如下所示的响应

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
会给你格式很好的

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
2018年3月14日15:23:16:
2018年3月14日15:23:16:ping 127.0.0.1,数据为32字节:

2018年3月14日15:23:16:回复自127.0.0.1:bytes=32时间您可以对其使用
ForEach对象
cmdlet(在下面的示例中使用了
%
别名)

结果:

23:41:51:301:
23:41:51:302: Pinging 192.168.1.1 with 32 bytes of data:
23:41:55:255: Request timed out.
23:42:00:266: Request timed out.
23:42:05:254: Request timed out.
23:42:10:253: Request timed out.
23:42:10:261:
23:42:10:263: Ping statistics for 192.168.1.1:
23:42:10:265:     Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
2019-04-23T23:45:40.5816185+02:00:
2019-04-23T23:45:40.5845856+02:00: Pinging 192.168.1.1 with 32 bytes of data:
2019-04-23T23:45:44.2560567+02:00: Request timed out.
2019-04-23T23:45:49.2549104+02:00: Request timed out.
2019-04-23T23:45:54.2547535+02:00: Request timed out.
2019-04-23T23:45:59.2547932+02:00: Request timed out.
2019-04-23T23:45:59.2577788+02:00:
2019-04-23T23:45:59.2607707+02:00: Ping statistics for 192.168.1.1:
2019-04-23T23:45:59.2627647+02:00:     Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
或者使用类似于mjolinor答案中的格式:

ping 192.168.1.1 | %{ "{0:o}: {1}" -f (Get-Date), $_ }
结果:

23:41:51:301:
23:41:51:302: Pinging 192.168.1.1 with 32 bytes of data:
23:41:55:255: Request timed out.
23:42:00:266: Request timed out.
23:42:05:254: Request timed out.
23:42:10:253: Request timed out.
23:42:10:261:
23:42:10:263: Ping statistics for 192.168.1.1:
23:42:10:265:     Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
2019-04-23T23:45:40.5816185+02:00:
2019-04-23T23:45:40.5845856+02:00: Pinging 192.168.1.1 with 32 bytes of data:
2019-04-23T23:45:44.2560567+02:00: Request timed out.
2019-04-23T23:45:49.2549104+02:00: Request timed out.
2019-04-23T23:45:54.2547535+02:00: Request timed out.
2019-04-23T23:45:59.2547932+02:00: Request timed out.
2019-04-23T23:45:59.2577788+02:00:
2019-04-23T23:45:59.2607707+02:00: Ping statistics for 192.168.1.1:
2019-04-23T23:45:59.2627647+02:00:     Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

我的问题是如何首先将时间戳添加到输出中,而不是如何随后过滤它们,这就是过滤器所做的。PowerShell筛选器是一个脚本块,它接受来自管道的输入,并对从管道获取的每个对象运行一次。它在功能上与只有进程块的函数相同。该过滤器会将时间戳添加到可执行文件的每一行输出中。啊,事实上,它的工作方式与您描述的完全相同。谢谢,问题解决了!谢谢,这是一个非常有趣的方法,我相信它在某些场景中是有用的。我坚持使用时间戳过滤器的方法,尽管根据mjolinor上面的回答。更适合我的需要。这可能是最短的代码,可以让事情顺利完成。我坚持使用mjolinor的解决方案,因为它更适合我(更大的代码库、多个脚本等,所以代码的可重用性对我来说很重要)