Reputation: 21
I am trying to get the contents from a file, the ping result and the description. The everything through pinging works, but I how do I get the description in my output?
GC D:\Temp\System.txt | %{
If (Test-Connection $_ -Quiet -Count 2){
Write-Host "$_ is UP" -b Green
}
Else{
Write-Host "$_ is Down" -b Red
}
}
PAUSE
Content with IP addresses to ping and their descriptions
10.200.11.2 Firewall
10.200.11.14 Test1
192.168.254.81 Test2
192.168.254.133 Test3
Upvotes: 1
Views: 46
Reputation: 10044
To add several other ways to do this:
Since it looks like you are using tabs delimiters you could split on the tabs.
GC D:\Temp\System.txt | % {
$hostname = ($_.Split("`t") | ? {$_ -ne ""})[0]
$description = ($_.Split("`t") | ? {$_ -ne ""})[1]
If (Test-Connection $hostname -Quiet -Count 2) {
Write-Host "Hostname: $hostname Description: $description is UP" -b Green
} Else {
Write-Host "Hostname: $hostname Description: $description is Down" -b Red
}
}
Or you could import it as a CSV with a tab delimiter
Import-CSV D:\Temp\System.txt -Delimiter `t -Header hostname,description | % {
If (Test-Connection $_.hostname -Quiet -Count 2) {
Write-Host "Hostname: $($_.hostname) Description: $($_.description) is UP" -b Green
[pscustomobject]@{
Hostname = $_.hostname
Description = $_.description
Status = "Up"
}
} Else {
Write-Host "Hostname: $($_.hostname) Description: $($_.description) is Down" -b Red
[pscustomobject]@{
Hostname = $_.hostname
Description = $_.description
Status = "Down"
}
}
} | ConvertTo-HTML | Out-File D:\Temp\Example.html
Upvotes: 0
Reputation: 473
Use regular expressions, ($_ -replace "\s.+","")
- All that up to the space (tabulation)
($_ -replace ".+\s","")
- All that after a space (tabulation)
GC D:\Temp\System.txt | %{
If (Test-Connection ($_ -replace "\s.+","") -Quiet -Count 2){
Write-Host "$_ is UP" -b Green
Write-Host ($_ -replace ".+\s","")
}
Else{
Write-Host "$_ is Down" -b Red
Write-Host ($_ -replace ".+\s","")
}
String Write-Host ($_ -replace ".+\s","")
are descriptions
Upvotes: 1