在开始睡眠(或直到脚本结束)之后,对象输出的怪异延迟 [英] weird delay of the output of an object when followed by start-sleep (or until script end)
问题描述
由于某种原因,在完成睡眠命令之前,对象不会输出.
For some reason, the object won't output until the sleep command is done.
[pscustomobject]@{message = 'hi'}; sleep 5
这是另一个例子.循环结束之前,您将看不到输出.
Here's another example. You won't see the output until the loop finishes.
foreach ($i in 1..60) {
if ($i -eq 1) { [pscustomobject]@{message = $i} }
sleep 1
}
我想您必须至少输出2个对象才能看到任何内容? ¯\ _(ツ)_/¯15秒后,您将看到两个对象.
I guess you have to output at least 2 objects to see anything? ¯\_(ツ)_/¯ After 15 seconds, you see both objects.
foreach ($i in 1..60) {
if ($i -eq 1 -or $i -eq 15) { [pscustomobject]@{message = $i} }
sleep 1
}
或输出足够的属性(> 4)以隐式调用format-list而不是format-table. 格式表是问题.这马上就出现了.
Or output enough properties (> 4) to implicitly call format-list instead of format-table. Format-table is the problem. This comes out right away.
[pscustomobject]@{a=1; b=2; c=3; d=4; e=5}; sleep 10
我想知道是否可以像-NoWait
这样添加格式表参数.
I wonder if a parameter to format-table could be added like -NoWait
.
具有包含列宽的格式文件的已知对象类型没有此问题.
Known object types with format files containing column widths don't have this problem.
foreach ($i in 1..60) {
if ($i -eq 1) { get-process powershell }
sleep 1
}
或默认为格式自定义的对象:
Or objects that default to format-custom:
foreach ($i in 1..60) {
if ($i -eq 1) { get-date }
sleep 1
}
推荐答案
输出少于5个属性,并且格式表隐式运行.在显示第一个对象之前,格式表将在第二个对象中等待不确定的时间.这适用于没有定义 default 表格视图的xml文件的对象类型(例如pscustomobject).
Output less than 5 properties, and format-table implicitly runs. Format-table will wait an indefinite amount of time for the second object, before displaying the first object. This is for object types (like pscustomobject) without an xml file that define a default table view.
# no output for 5 seconds
&{get-date
sleep 5
get-date} | format-table
DisplayHint DateTime Date Day DayOfWeek DayOfYear Hour Kind Millisecond Minute
----------- -------- ---- --- --------- --------- ---- ---- ----------- ------
DateTime Saturday, February 8, 2020 10:24:48 AM 2/8/2020 12:00:00 AM 8 Saturday 39 10 Local 618 24
DateTime Saturday, February 8, 2020 10:24:53 AM 2/8/2020 12:00:00 AM 8 Saturday 39 10 Local 892 24
与format-list比较:
Compare with format-list:
& {get-date
sleep 5
get-date} | format-list
DisplayHint : DateTime
Date : 2/8/2020 12:00:00 AM
Day : 8
DayOfWeek : Saturday
DayOfYear : 39
Hour : 20
Kind : Local
Millisecond : 408
Minute : 37
Month : 2
Second : 18
Ticks : 637167910384087860
TimeOfDay : 20:37:18.4087860
Year : 2020
DateTime : Saturday, February 8, 2020 8:37:18 PM
DisplayHint : DateTime
Date : 2/8/2020 12:00:00 AM
Day : 8
DayOfWeek : Saturday
DayOfYear : 39
Hour : 20
Kind : Local
Millisecond : 662
Minute : 37
Month : 2
Second : 23
Ticks : 637167910436622480
TimeOfDay : 20:37:23.6622480
Year : 2020
DateTime : Saturday, February 8, 2020 8:37:23 PM
这篇关于在开始睡眠(或直到脚本结束)之后,对象输出的怪异延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!