如何修改此 PowerShell 脚本以将这两个部分导出到 CSV 文件而不切断任何数据?

How can I modify this PowerShell script to export both sections to a CSV without cutting off any data?

我从网上得到这个脚本。我想至少我可以弄清楚如何将数据导出到 CSV。事实证明,这远远超出了我的能力范围。我试过 Export-Csv 和管道,但都没有成功。

我有一次成功尝试,但所有数据都被推入 1 列并被截断,-Width 参数也没有解决这个问题。其余时间我只是得到随机信息,例如 Length 或似乎是数字和字母混乱的损坏数据。

所有此脚本都在 IP 列表上执行 运行 pingnslookup。我想将它移动到一个 CSV 文件中,这样我就可以对数据进行排序并找到 empty/not 不再使用的 IP 来清理我们的 IP space 甚至识别我们的 DNS 中的问题。

$InputFile = 'C:\Temp\list.txt'
$addresses = Get-Content $InputFile
$reader = New-Object IO.StreamReader $InputFile
while ($reader.ReadLine() -ne $null) { $TotalIPs++ }
Write-Host ""
Write-Host "Performing nslookup on each address..."    
foreach ($address in $addresses) {
    ## Progress bar
    $i++
    $percentdone = (($i / $TotalIPs) * 100)
    $percentdonerounded = "{0:N0}" -f $percentdone
    Write-Progress -Activity "Performing nslookups" -CurrentOperation "Working on IP: $address (IP $i of $TotalIPs)" -Status "$percentdonerounded% complete" -PercentComplete $percentdone
    ## End progress bar
    try {
        [System.Net.Dns]::Resolve($address) | Select HostName, AddressList
    } catch {
        Write-Host "$address was not found. $_" -ForegroundColor Green
    }
}
Write-Host ""
Write-Host "Pinging each address..."
foreach($address in $addresses) {
    ## Progress bar
    $j++
    $percentdone2 = (($j / $TotalIPs) * 100)
    $percentdonerounded2 = "{0:N0}" -f $percentdone2
    Write-Progress -Activity "Performing pings" -CurrentOperation "Pinging IP: $address (IP $j of $TotalIPs)" -Status "$percentdonerounded2% complete" -PercentComplete $percentdone2
    ## End progress bar
    if (Test-Connection -ComputerName $address -Count 2 -Quiet) {
        Write-Host "$address responded" -ForegroundColor Green 
    } else {
        Write-Warning "$address does not respond to pings"              
    }
}
Write-Host ""
Write-Host "Done!"

简化,简化...

Get-Content $InputFile | ForEach-Object {
    $ip = $_

    try {
        $dns = [Net.Dns]::Resolve($ip)
    } catch {
        Write-Host "$address not found.`n$_"
    }

    New-Object -Type PSObject -Property @{
        'Address'     = $ip
        'Hostname'    = $dns.Hostname
        'AddressList' = $dns.AddressList
        'Online'      = [bool](Test-Connection $ip -Count 2 -Quiet)
    }
} | Export-Csv 'C:\path\to\output.csv' -NoType