在foreach循环中分批暂停?(Pausing in batches during a foreach loop?)

编程入门 行业动态 更新时间:2024-10-27 12:29:28
在foreach循环中分批暂停?(Pausing in batches during a foreach loop?)

我正在编写一个脚本,将500多条DNS记录迁移到Windows 2008(从'03开始),然后通过DNSCMD(内置MS DNS工具)操纵属性。

无论如何 - 在DNS中有一个限制,如果你做“太快”的事情,它会开始窒息 - 我以前在我的旧DOS脚本中看到过这种情况。

那就是说,我如何批处一个foreach循环?

比如(例如用外行人的话来说脚本)

$records = C:\myfile.txt foreach ($record in records) { MyFunction (only to the first 20 records) start-sleep 2 MyFunction (to the next 20 records) ... etc .... }

那可能吗? 如果是这样你会如何接近它? 我想我可以在MyFunction之后添加一个简单的start-sleep 1 ,但是有500多条记录,很多功能,这将花费很长时间。 :(我认为在DOS世界中找到的效率方面是最好的。感谢!

I'm writing a script to migrate 500+ DNS records to Windows 2008 (from '03) and then manipulating attributes - all via DNSCMD (builtin MS DNS tool).

Anyways - there is a limitation in DNS where if you do things "too fast" it starts choking on itself - I've seen this before in my old DOS scripts.

That said, how would I batch up a foreach loop?

Like for example (in laymans terms w/ scripting)

$records = C:\myfile.txt foreach ($record in records) { MyFunction (only to the first 20 records) start-sleep 2 MyFunction (to the next 20 records) ... etc .... }

Is that possible? If so how would you approach it? I guess I could add a simple start-sleep 1 after MyFunction, but with 500+ records, times many functions, that's gonna take a VERY long time. :( I think batching them is the best in terms of efficiency we've found from the DOS world. Thanks!

最满意答案

如果您从文件中读取它们,可以使用-ReadCount批量处理它们:

Get-Content C:\myfile.txt -ReadCount 20 | foreach { foreach ($record in $_) { MyFunction $record } start-sleep 2 }

If you're reading them from a file, you can batch them up using -ReadCount:

Get-Content C:\myfile.txt -ReadCount 20 | foreach { foreach ($record in $_) { MyFunction $record } start-sleep 2 }

更多推荐

本文发布于:2023-07-29 20:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319346.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Pausing   foreach   loop   batches

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!