I'm having trouble exporting this basic net-connection test into the Export-Excel (import-Excel) feature. I need to export the results of a net-connection test from the array that was created.
Each CSV serverlist has 2 columns server name and port
Here is the code:
Import-Csv -Path .\serverlist.csv -Delimiter "," | ForEach-Object {$_.servers
$status = Test-NetConnection -ComputerName $_.servers -Port $_.port
if ($status.TcpTestSucceeded -eq $True) {
$result = "ONLINE"
$onlineCount ++ }
elseif ($status.TcpTestSucceeded -eq $false) {
$result = "OFFLINE"
$offlineCount ++ }
else {
$result = "ERROR"
}
$result = [PSCustomObject]@{
ComputerName = $status.ComputerName
Port = $status.RemotePort
Status = $result
Date = Get-Date -Format "dd/MM/yyyy"
Time = Get-Date -Format "HH:mm"
}
} $result | Export-Excel -Path ".\netStatus.xlsx" -AutoSize -AutoFilter -WorksheetName Net_Test
ii ".\netStatus.xlsx"
This is the Excel result. See there is only one column with addresses. I want : ComputerName, Port, Status, Date, Time
I wanted the $result array to show in excel similar to this:
I also get this error: