PowerShell Mass Test-Connection

前端 未结 3 772
野趣味
野趣味 2021-01-05 12:34

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-Connect

3条回答
  •  鱼传尺愫
    2021-01-05 13:12

    Powershell 7 and Foreach-Object -Parallel makes it much simpler now:

    Get-Content -path C:\Utilities\servers.txt | ForEach-Object -Parallel {
        Test-Connection $_ -Count 1 -TimeoutSeconds 1 -ErrorAction SilentlyContinue -ErrorVariable e
        if ($e)
        {
            [PSCustomObject]@{ Destination = $_; Status = $e.Exception.Message }
        }
    } | Group-Object Destination | Select-Object Name, @{n = 'Status'; e = { $_.Group.Status } }
    

提交回复
热议问题