Pio*_*r L 15 powershell logging datetime 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=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
Run Code Online (Sandbox Code Playgroud)
期望的结果:
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
Run Code Online (Sandbox Code Playgroud)
我知道如何拆分/加入PowerShell数组,但这只能在&
操作员完成后才会发生.我正在寻找更类似于实时的解决方案,其中时间戳在&运算符运行时添加到输出中.
顺便说一句,时间戳本身就是 $($(Get-Date -Format o) + ":")
mjo*_*nor 49
你可以使用一个过滤器:
filter timestamp {"$(Get-Date -Format o): $_"}
$result = & ping 192.168.1.1 | timestamp
Run Code Online (Sandbox Code Playgroud)
样本输出来自$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),
Run Code Online (Sandbox Code Playgroud)
Mar*_*ski 10
您只需使用ForEach-Object
cmdlet 即可(%
在下面的示例中使用别名)
ping 192.168.1.1 | %{ "{0:HH:mm:ss:fff}: {1}" -f (Get-Date), $_ }
Run Code Online (Sandbox Code Playgroud)
结果:
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),
Run Code Online (Sandbox Code Playgroud)
或者使用 mjolinor 答案中的格式:
ping 192.168.1.1 | %{ "{0:o}: {1}" -f (Get-Date), $_ }
Run Code Online (Sandbox Code Playgroud)
结果:
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),
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以将 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 " "
}
Run Code Online (Sandbox Code Playgroud)
这在我的 Windows 7 工作站上效果很好。然而,在一些较新的 Server 2012 R2 安装上,Start-Transcript 似乎略有损坏。这修复了其中的一部分: https: //support.microsoft.com/en-us/help/3014136/powershell-transcript-file-doesn-t-contain-the- Correct-information-in-windows-server-2012- r2
...但它仍然无法解决自定义提示的问题。
例如,我在控制台上看到这个:
PS:02/14/17 08:28:20> hostname
server463
Run Code Online (Sandbox Code Playgroud)
这是写入日志的内容:
PS:02/14/17 08:28:20
>
PS>hostname
server463
Run Code Online (Sandbox Code Playgroud)
对于正在寻找更多信息的任何人filter
,这里是文档。很难找到令人惊讶的结果,因为搜索“过滤器”和“ powershell”一词的任意组合将提供一百万个示例,而没有文档。同样help filter
在powershell中也没有明显的帮助。
mjolinor提供的答案是执行类似操作的最佳方法,但我想对此进行扩展。
filter timestamp {"$(Get-Date): $_"}
Run Code Online (Sandbox Code Playgroud)
是调用此的快捷方式
function timestamp { Process{"$(Get-Date): $_"} }
Run Code Online (Sandbox Code Playgroud)
这两个都创建了命名函数,它们接受来自管道的输入。help pipline
在powershell中运行以了解更多信息。流水线一次将对单个对象进行操作,并且可以使用自动变量 引用该对象$_
。因此,每个函数都将使用|
管道字符对管道中管道传递的每个项目进行迭代。
这与普通函数的行为有所不同,因为它在对象到达时对它们起作用,而不是一次完成。例如跑步
function timestamp {
"$(Get-Date): $input"
}
$result = & ping 127.0.0.1
$result | timestamp
Run Code Online (Sandbox Code Playgroud)
将整个$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
Run Code Online (Sandbox Code Playgroud)
由于该函数在整个对象上运行,因此您必须遍历每行。更改为此
function timestampfunction {
foreach ($i in $input){
"$(Get-Date): $i"
}
}
Run Code Online (Sandbox Code Playgroud)
会给你很好的格式
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
Run Code Online (Sandbox Code Playgroud)
这是一篇写得很好的文章,介绍了这些方法之间的区别。
归档时间: |
|
查看次数: |
29381 次 |
最近记录: |