简体   繁体   English

大规模测试连接Powershell

[英]Mass test-connection powershell

Well, I have problem with Test-Connection function in PowerShell. 好吧,我在PowerShell中的Test-Connection函数有问题。 I have a csv file with column Server name and IPAddress . 我有一个csv文件,其列Server nameIPAddress I need to create connection report with information Server name , IPAddress , Result . 我需要使用信息服务器名称 IPAddress Result创建连接报告。 How could I add information about column server name? 如何添加有关列服务器名称的信息?

My current code looks like: 我当前的代码如下:

$Report = @()
$Servers = Import-CSV "C:\skrypt\bobi\servers.csv"  -Delimiter ';'
foreach ($Server in $Servers) {
    if ($Alive = Test-Connection -ComputerName $Server.IPAddress -Count 1 -quiet) {
        $TestResult = New-Object psobject -Property @{
            IPAddress = $Server.IPAddress
            Result    = "Online"
        }
    }
    else {
        $TestResult = New-Object psobject -Property @{
            IPAddress = $Server.IPAddress
            Result    = "Offline"
        }
    }
    $Report += $TestResult
}       
$Report | Select-Object IPAddress, Result | Export-Csv C:\skrypt\bobi\Report.csv -nti

I think this might help you: 我认为这可能对您有帮助:

$Servers = @(
    [PSCustomObject]@{
        'Server Name' = 'Server1'
        IPAddress = '10.10.10.10'
    }
    [PSCustomObject]@{
        'Server Name' = 'Wrong'
        IPAddress = '10.10.10.999'
    }
    [PSCustomObject]@{
        'Server Name' = 'Server2'
        IPAddress = '10.10.10.15'
    }
)

$Report = foreach ($Server in $Servers) {
    # First we collect all details we know in an object
    $Result = [PSCustomObject]@{
        Name   = $Server.'Server Name'
        IP     = $Server.IPAddress
        Online = $false
    }

    # Then we do the test
    if (Test-Connection -ComputerName $Server.IPAddress -Count 1 -Quiet) {
        # If the connection is successful, we set it to True
        $Result.Online = $true    
    }

    # As a last step we return the complete object
    # where it is collected in the array of Report
    $Result
}

$Report | Select-Object Name, IP, Online

In case your CSV file have column with hostnames you would need to change PSCustomObject to: 如果您的CSV文件包含带有主机名的列,则需要将PSCustomObject更改为:

$TestResult = New-Object psobject -Property @{
    IPAddress = $Server.IPAddress
    HostName = $Server.HostName #assuming column name is "HostName"
    Result    = "Result"
}

In case your CSV file does not have column with hostnames you would need to request System.Net.Dns class with its GetHostByAddress method. 如果您的CSV文件中没有带有主机名的列,则需要使用其GetHostByAddress方法请求System.Net.Dns类。 Like so: 像这样:

$TestResult = New-Object psobject -Property @{
    IPAddress = $Server.IPAddress
    HostName = $([System.Net.Dns]::GetHostByAddress($Server.HostName).HostName -join ';')
    Result    = "Result"
}

In both cases you then will need to pipe HostName property to export csv file 在这两种情况下,您都需要使用管道的HostName属性来导出csv文件

$Report | Select-Object IPAddress, HostName, Result | Export-Csv C:\skrypt\bobi\Report.csv -nti

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM