Powershell foreach 暂停后执行

Powershell foreach executing after pause

我创建了一个脚本来读取名称列表并使用“Resolve-DnsName”将它们转换为 IP。 我希望用户只需双击脚本并阅读输出。

cls
Write-Host -ForegroundColor Green -BackgroundColor DarkRed "
 ______________________________
|X      | Y     | Z            |
|-------+ ------+--------------|
|F2M1   | 49621 |esbsa9908ee43 |
|F2M2   | 47546 |esbsa009908jc1|
|F2M3   |xxxxxxx|7417191543366 |
|F2M4   | 47516 |esbsa9908ee18 |
|F2M5   | 47385 |7417191543116 |
|Capital| 86658 |7417191543242 |
|______________________________|"
Write-Host "`n`n"


$estacoes = @(
    'esbsa9908ee43',
    'esbsa009908jc1',
    '7417191543366',
    'esbsa9908ee18',
    '7417191543116',
    '7417191543242'
)

ForEach ($estacao in $estacoes){
    Resolve-DnsName -ErrorAction Continue -Type A -QuickTimeout -Name $estacao | Select-Object Name,IpAddress
    }

Pause

碰巧最后的“pause”命令在“foreach”之前执行。

 ______________________________
|X      | Y     | Z            |
|-------+ ------+--------------|
|F2M1   | 49621 |esbsa9908ee43 |
|F2M2   | 47546 |esbsa009908jc1|
|F2M3   |xxxxxxx|7417191543366 |
|F2M4   | 47516 |esbsa9908ee18 |
|F2M5   | 47385 |7417191543116 |
|Capital| 86658 |7417191543242 |
|______________________________|


Press Enter to continue...:


Name           IPAddress
----           ---------
esbsa9908ee43  172.18.18.215
esbsa009908jc1 172.18.18.44
7417191543366  172.18.18.18
esbsa9908ee18  172.18.18.21
7417191543116  172.18.18.126
7417191543242  172.30.165.50

我似乎找不到原因。 你们能帮忙吗?

它看起来类似于管道输出和写入主机输出发生的有趣事情,其中​​写入主机输出将首先出现,即使它在管道之后 运行。这是更改输出顺序的一种方法:

$estacoes = echo microsoft.com yahoo.com
$result = ForEach ($estacao in $estacoes){
    Resolve-DnsName -ErrorAction Continue -Type A -QuickTimeout -Name $estacao | 
      Select-Object Name,IpAddress 
    }

$result | format-table

pause

# didn't work
# 'Press Enter to continue...:'
# [void][console]::readkey()