本文介绍了PowerShell 大规模测试连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简单的脚本来检查一个非常大的服务器列表的状态.在这种情况下,我们将其称为servers.txt.我知道使用 Test-Connection,您可以在 -count 开关上指定的最短时间是 1.我的问题是,如果您最终在脚本中有 1000 台机器,则返回结果可能会延迟 1000 秒.我的问题:有没有一种方法可以快速针对测试连接测试大量机器,而无需等待每台机器一次失败?

I am attempting to put together a simple script that will check the status of a very large list of servers. in this case we'll call it servers.txt. I know with Test-Connection the minimum amount of time you can specify on the -count switch is 1. my problem with this is if you ended up having 1000 machines in the script you could expect a 1000 second delay in returning the results. My Question: Is there a way to test a very large list of machines against test-connection in a speedy fashion, without waiting for each to fail one at a time?

当前代码:

Get-Content -path C:\Utilities\servers.txt | foreach-object {new-object psobject -property @{ComputerName=$_; Reachable=(test-connection -computername $_ -quiet -count 1)} } | ft -AutoSize

推荐答案

Test-Connection 有一个 -AsJob 开关,可以执行您想要的操作.为了达到同样的目的,你可以尝试:

Test-Connection has a -AsJob switch which does what you want. To achieve the same thing with that you can try:

Get-Content -path C:\Utilities\servers.txt |ForEach-Object { Test-Connection -ComputerName $_ -Count 1 -AsJob } |找工作|接收-工作-等待 |Select-Object @{Name='ComputerName';Expression={$_.Address}},@{Name='Reachable';Expression={if ($_.StatusCode -eq 0) { $true } else { $false}}} |ft -AutoSize

希望有帮助!

这篇关于PowerShell 大规模测试连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-15 18:46