Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否需要等待“等待”;“中断”;powershell中获取内容的等待循环?_Powershell - Fatal编程技术网

是否需要等待“等待”;“中断”;powershell中获取内容的等待循环?

是否需要等待“等待”;“中断”;powershell中获取内容的等待循环?,powershell,Powershell,是否需要等待“中断”powershell中获取内容的等待循环 Get-Content "$PSScriptRoot\out.txt" -Wait out.txt有固定数量的写入行,我想在到达末尾时停止循环。 到目前为止,我需要发送一个带有“ctrl+c”的信号来完成。 在伪代码中,这类似于 Get-Content "$PSScriptRoot\out.txt" -WaitUntil 9 Get-Content "$PSScriptRoot\out.txt" -WaitUntilMatch "^

是否需要等待“中断”powershell中获取内容的等待循环

Get-Content "$PSScriptRoot\out.txt" -Wait
out.txt
有固定数量的写入行,我想在到达末尾时停止循环。 到目前为止,我需要发送一个带有“ctrl+c”的信号来完成。 在伪代码中,这类似于

Get-Content "$PSScriptRoot\out.txt" -WaitUntil 9
Get-Content "$PSScriptRoot\out.txt" -WaitUntilMatch "^LastOutput"

因此,当输出
9
行时,我们就打破了循环。或者,当达到某个正则表达式匹配时,我们可以中断循环吗?

您可以通过管道将值从Get Content传递到ForEach对象,并在满足条件时中断

例如,如果要在达到某个正则表达式匹配时中断,可以使用以下命令:

Get-Content -Path "$PSScriptRoot\out.txt" -Wait | ForEach-Object {$_ ; if($_ -Match "some regex"){break} }
如果您想在n行之后中断,那么正如Bruce回答的那样,您可以使用
选择对象-第一个n

请注意,如果文件已包含满足条件的内容,并且您只需要添加到文件中的新数据,则还应使用
-tail
选项

例如,如果文件已包含9行,并且您只希望在运行命令后添加到文件中的前9行,请使用:

Get-Content -Path "$PSScriptRoot\out.txt" -Wait -tail 1 | select -skip 1 -first 9

使用
-tail 1
,命令将仅输出文件的最后一行以及随后将添加到文件中的新数据<代码>-跳过1用于过滤掉最后一行并仅输出新数据。

您可以使用
选择对象-第一个n
在n个项目之后停止管道:

Get-Content -Path "$PSScriptRoot\out.txt" -Wait  | select -first 9

(虽然我喜欢将
-WaitUntil
-WaitMatch
添加到
获取内容
。在上打开一个问题可能是值得的)

获取内容-路径“$PSScriptRoot\out.txt”-Wait-TotalCount 9
@不可更正1只有当文件不包含9行时,这才有效。如果是这样,它只会给出文件的前9行,而-wait开关将不起作用。