NCNecros
NCNecros

Reputation: 2415

how can i write elegant ping with powershell

I create script for check available resources:

$servers = New-Object System.Collections.Generic.Dictionary"[String,String]"
$servers.Add("Server 1","127.0.0.1")
$servers.Add("Server 2","127.0.0.1")
$servers.Add("Server 3","127.0.0.1")

$virtuals = New-Object System.Collections.Generic.Dictionary"[String,String]"
$virtuals.Add("VM 1","127.0.0.1")
$virtuals.Add("VM 2","127.0.0.1")
$virtuals.Add("VM 3","127.0.0.1")


Write-Output("`n")
Write-Output("Check availiable servers")


foreach ($key in $servers.GetEnumerator()){

    if ((Test-Connection $key.Value -Count 3 -Quiet)){        
        [pscustomobject]@{Server = $key.Key; Status = "Availiable"}
    }else{
    [pscustomobject]@{Server = $key.Key; Status = "Not availiable"}
    }
}
Remove-Variable -Name [pscustomobject]
Write-Output("`n")
Write-Output("Check availiable virtuals")
Write-Output("`n")

foreach ($key in $virtuals.GetEnumerator()){

    if ((Test-Connection $key.Value -Count 3 -Quiet)){
        [pscustomobject]@{VM = $key.Key; Status = "Availiable"}
        
    }else{
        [pscustomobject]@{VM = $key.Key; Status = "Not availiable"}
    }
}

enter image description here

If I change the title, then the output is in the following table without the first column. And I can't find how I can print the title in every new cycle? My ultimate goal is to have a header for each new loop and color-coded available and unavailable addresses.

Upvotes: 0

Views: 155

Answers (2)

Theo
Theo

Reputation: 61253

If by 'elegant' you mean outputting a line in the table in some color, depending on availability, use something like this:

$servers = [ordered]@{
    "Server 1" = "127.0.0.1"
    "Server 2" = "127.0.0.1"
    "Server 3" = "127.0.0.1"
}

$virtuals = [ordered]@{
    "VM 1" = "127.0.0.1"
    "VM 2" = "127.0.0.1"
    "VM 3" = "127.0.0.1"
}


Write-Host "Check available servers" -ForegroundColor Cyan

$svr = foreach ($key in $servers.GetEnumerator()){
    if ((Test-Connection $key.Value -Count 3 -Quiet)){        
        [pscustomobject]@{Server = $key.Key; Status = "Available"}
    }
    else{
        [pscustomobject]@{Server = $key.Key; Status = "Not available"}
    }
}
# output this as colored table
$svr | Format-Table | Out-String -Stream | ForEach-Object {
    $color = if ($_ -match 'Not available') { 'Red' } else { 'Green' }
    Write-Host $_ -ForegroundColor $color
}

Write-Host

# now do the same for the VMs
Write-Host "Check available virtuals" -ForegroundColor Cyan

$vms = foreach ($key in $virtuals.GetEnumerator()){
    if ((Test-Connection $key.Value -Count 3 -Quiet)){
        [pscustomobject]@{VM = $key.Key; Status = "Available"}
    }
    else{
        [pscustomobject]@{VM = $key.Key; Status = "Not available"}
    }
}

# output this as colored table
$vms | Format-Table | Out-String -Stream | ForEach-Object {
    $color = if ($_ -match 'Not available') { 'Red' } else { 'Green' }
    Write-Host $_ -ForegroundColor $color
}

Output:

enter image description here

Upvotes: 1

Neil
Neil

Reputation: 41

As PowerShell has already added the titles to the tops of the columns from the previous item, it doesn't add them again. Part of its way to reduce repetition or something probably.

You could maybe do an output using Write-Host and then you can format to your hearts content.

Write-Host "Servers" -ForegroundColor Cyan
Write-Host "Virtuals" -ForegroundColor Cyan

You could also do something like...

if ($status -eq "Available") {
    $color = "Green"
} else {
    $color = "Red"
}

Then when you come to write your results you could do...

Write-Host "Server Name" -ForegroundColor $color

Hope that makes sense, I can write something out fully if you need.

Upvotes: 1

Related Questions