无法将数据导出为 CSV 或 Excel

Unable to export the data to CSV or Excel

我已经编写了一个脚本来检查每个服务器的 nslookup 并将详细信息导出到 Excel,但是我的脚本正在查找,但是当我导出时我无法导出输出我得到空数据.

请帮我导出数据到Excel

代码

## Loop through each server for Nslookup
foreach ($Server in $Servers)
{
    $Addresses = $null
    try {
        $Addresses = [System.Net.Dns]::GetHostAddresses("$Server").IPAddressToString
    }
    catch { 
        $Addresses = "Server IP cannot resolve"
    }
    foreach($Address in $addresses) {
        #write-host $Server, $Address
         $Server_Name = $Server
         $IP_Address = $Address                 
    } 
  } 
$result | Export-Excel -Path $FileName -AutoSize -BoldTopRow -FreezeTopRow -TitleBold -WorksheetName Server_Nslookup_Details

您的内部 foreach 循环不产生任何输出,只是为 2 个变量($Server_Name$IP_Address)赋值:

foreach($Address in $addresses) {
    $Server_Name = $Server
    $IP_Address = $Address
}

您可能打算改为构建 new object

$result = foreach($Server in $Servers) {
    $addresses = try {
        [System.Net.Dns]::GetHostAddresses($Server).IPAddressToString
    }
    catch {
        "Server IP cannot resolve"
    }
    foreach($address in $addresses) {
        [pscustomobject]@{
            Server    = $Server
            IPAddress = $address
        }
    }
}

$result | Export-Excel ....