2

I have written an environment IP check script in Powershell which works but I can't figure out how to display the output to screen in a formatted table which auto sizes the columns.

$infile = Import-Csv "~\Env_IPlist.csv" -Delimiter ","
$arr1 = @()

$IP_Addresses = $infile |
                Select-Object "Device_Type", "Device_Name", "IP_Address",
                    "IP_Role", "Location"

Write-Host "Started Ping Test ..."

foreach ($IP in $IP_Addresses) {
    if (Test-Connection $IP.("IP_Address") -Quiet -Count 1 -ErrorAction SilentlyContinue) {
        Write-Host $IP.("Device_Name") ":" $IP.("IP_Address") "Ping successful" -ForegroundColor Green
    } else {
        Write-Host $IP."Device_Name" ":" $IP.("IP_Address") "Ping failed" -ForegroundColor Red -BackgroundColor white
    }
}

Write-Host "Ping Test Completed!"
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Troy Hector
  • 25
  • 1
  • 1
  • 3
  • You're not outputting something that could be formatted as a table. You would usually use `Format-Table`. You could use a custom PSObject, an array, a hashtable or a fancy `Select-Object`. – Seth Feb 21 '19 at 12:46

2 Answers2

2

Add the Test-Connection result via a calculated property, then pipe the output to Format-Table.

$infile |
    Select-Object Device_Type, Device_Name, IP_Address, IP_Role, Location,
        @{n='Online';e={
            [bool](Test-Connection $_.IP_Address -Quiet -Count 1 -EA SilentlyContinue)
        }} |
    Format-Table -AutoSize
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

I rewrote the script using PSObject as initially suggested but now I do not know how to add the ForegroundColor in the if..else statement.

$infile = Import-Csv "~\Env_IPlist.csv" -Delimiter ","
$arr1 = @()

$IP_Addresses = $infile |
                Select-Object Device_Type, Device_Name, IP_Address, IP_Role,
                    Location, Status

foreach ($IP in $IP_Addresses) {
    if (Test-Connection $IP.IP_Address -Quiet -Count 1 -ErrorAction SilentlyContinue) {
        $PSObject = New-Object PSObject -Property @{
            Device_Type = $IP.Device_Type
            Device_Name = $IP.Device_Name
            IP_Address  = $IP.IP_Address
            IP_Role     = $IP.IP_Role
            Location    = $IP.Location
            Status      = "Successful"
        }
    } else {
        $PSObject = New-Object PSObject -Property @{
            Device_Type = $IP.Device_Type
            Device_Name = $IP.Device_Name
            IP_Address  = $IP.IP_Address
            IP_Role     = $IP.IP_Role
            Location    = $IP.Location
            Status      = "Failed"
        }
    }
    $arr1 += $PSObject
}

$arr1 | Format-Table -AutoSize
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Troy Hector
  • 25
  • 1
  • 1
  • 3
  • You could simplify to only one psobject and on output colorize like [the answers to this question](https://stackoverflow.com/questions/20705102/how-to-colorise-powershell-output-of-format-table) –  Feb 21 '19 at 15:59
  • Is this supposed to be an answer or a followup question? – Ansgar Wiechers Feb 23 '19 at 21:44