Powershell 脚本逐行在 Powershell 窗口上工作,但不在脚本中 [英] Powershell script works on Powershell window line-by-line, but not in script

查看:45
本文介绍了Powershell 脚本逐行在 Powershell 窗口上工作,但不在脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,用于遍历一堆域并从whois.exe.这逐行工作,但是当作为脚本运行时,它会冻结.这是它卡住的地方:

I have a script I'm using to loop through a bunch of domains and get dates from whois.exe. This works line-by-line, but when run as a script, it'll freeze. Here is where it gets stuck:

ForEach ($domain in $domains)
{
    $domainname = $domain.Name

    Write-Host "Processing $domainname..."

    # WhoIsCL responds with different information depending on if it's a .org or something else. 
    if($domainname -like "*.org" -and $domainname)
    {
        $date = .\WhoIs.exe -v "$domainname" | Select-String -Pattern "Registry Expiry Date: " -AllMatches
        Write-Host "Domain is a .org" -ForegroundColor "Yellow"

当我 CTRL+C 取消命令时,我可以验证 $domain 是正确的变量.然后我可以这样写:

When I CTRL+C to cancel the command, I can verify that $domain is the correct variable. I can then write this:

if($domainname -like "*.org" -and $domainname)
{
    "Test"  
}

... 并且测试"出现在命令行中.然后我运行:

... and "Test" appears in the command line. I then run:

$date = .\WhoIs.exe -v "$domainname" | Select-String -Pattern "Registry Expiry Date: " -AllMatches

检查日期后,结果正确,我得到了合适的日期.鉴于它在Processing $domainname..."和Domain is a .org"之前冻结,我只能假设 WhoIs.exe 冻结.那么,为什么会在脚本运行时发生这种情况,而不是直接从 Powershell 窗口中发生?

Upon checking the date, it comes out right and I get the appropriate date. Given it freezes right as it says "Processing $domainname..." and right before "Domain is a .org", I can only assume WhoIs.exe is freezing. So, why does this happen as the script is being run, but not directly from the Powershell window?

最后,我通过简单地将整个脚本复制并粘贴到 Powershell 窗口(这很愚蠢,但它似乎可以运行)进行了最终测试,并获得了相同的结果.它在 whois.exe 处冻结.

Lastly, I did a final test by simply copying and pasting the entire script into a Powershell window (which is just silly, but it appears to function) and get the same result. It freezes at whois.exe.

我最好的猜测是 whois.exe 需要以不同的方式运行才能在我的 for 循环中的 Powershell 中可靠.但是,我似乎没有办法在 Start-Process 中测试使用它并获取字符串输出.

My best guess is that whois.exe needs to be run differently to be reliable in Powershell in my for-loop. However, I don't seem to have a way to test using it in a Start-Process and get string output.

无论如何,建议会很棒.我肯定撞墙了.

Anyways, advise would be great. I've definitely hit a wall.

谢谢!

推荐答案

如果您的脚本在多个域中运行,则可能是您受到了限制.这是 Nominet AUP 的引述:

If your script is running through lots of domains, it could be that you're being throttled. Here is a quote from the Nominet AUP:

最大查询速率为每秒 5 次查询,最多 1,000每滚动 24 小时查询一次.如果超过查询限制一个块将被强加.有关块的更多详细信息,请参阅详细的使用说明页面.这些限制不是每个 IP地址,它们是每个用户的.

The maximum query rate is 5 queries per second with a maximum of 1,000 queries per rolling 24 hours. If you exceed the query limits a block will be imposed. For further details regarding blocks please see the detailed instructions for use page. These limits are not per IP address, they are per user.

http://registrars.nominet.org.uk/registration-and-domain-management/acceptable-use-policy

不同的注册商可能会有不同的行为,但我希望有某种速率限制.这将解释为什么脚本(具有高容量)与临时手动查找的行为不同.

Different registrars may behave differently, but I'd expect some sort of rate limit. This would explain why a script (with high volume) behaves differently to ad-hoc manual lookups.

从下面的评论中提出的解决方案是将 Start-Sleep -Seconds 1 添加到每个 Whois 查找之间的循环中.

Proposed solution from the comments below is to add Start-Sleep -Seconds 1 to the loop between each Whois lookup.

这篇关于Powershell 脚本逐行在 Powershell 窗口上工作,但不在脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆