暂停后执行Powershell foreach

时间:2020-06-20 15:34:09

标签: powershell foreach 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

碰巧最后一个“暂停”命令在“ 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

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

1 个答案:

答案 0 :(得分:3)

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

$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()