Reputation: 1
i am using the following PowerShell expression to Ping various machines in my company. $serverName are the IP's of the machines I want to ping (Around 25) and PS outputs the findings nicely as 172.172.172.172 is alive and pinging
on each line etc.
Is there a way I can have PowerShell output a comment I have given to each variable like below or the computername?
172.172.172.172 - Norwich PC1
192.192.192.192 # My PC is alive and Pinging
Thanks
$ServerName =
"172.172.172.172",
"192.192.192.192"
"192.192.193.193"
foreach ($Server in $ServerName) {
if (test-Connection -ComputerName $Server -Count 2 -Quiet ) {
write-Host "$Server is alive and Pinging " -ForegroundColor Green
} else
{ Write-Warning "$Server Switched off not pinging "
}
}
Upvotes: 0
Views: 80
Reputation: 95712
You can create custom Powershell objects but probably the easiest in this case would be to replace your list of addresses with some data in CSV format:
$Servers = @"
"Addr","Comment"
"172.172.172.172","Norwich PC1"
"192.192.192.192","My PC"
"192.192.193.193",""
"@
foreach ($Server in (ConvertFrom-Csv $Servers)) {
if (test-Connection -ComputerName $Server.addr -Count 2 -Quiet ) {
write-Host "$($Server.addr)($($Server.Comment)) is alive and Pinging " -ForegroundColor Green
} else
{ Write-Warning "$($Server.addr)($($Server.Comment)) Switched off not pinging "
}
}
You could also easily save the addresses in a separate file if you wanted in which case use Import-Csv
.
Upvotes: 1