I have a csv file containing 4 columns The first and second columns are IP, Host and The third and fourth column will specify the ping status of each of them So I need to learn how to ping ip host with powershell and insert the result in the third and fourth columns. Thanks
$ip = Get-Content -Path D:\PowerShell\hostlist.csv
foreach ( $ip1 in $ip ) { $ip2 = ping $ip1
if ( $ip2 -imatch "(100% loss)")
{ Write-Host $ip1 " ping is filed " }
else { Write-Host $ip1 "ping is success "} }
As per my comment.
Point of note:
The imatch
thing is not required. Windows is case insensitive by default and thus so is PowerShell. As documented in the PowerShell docs: about_Case-Sensitivity
Again, the below is just an example. Refactor as you need to.
FYI... If your file has no headers. Create them on the read of the file
'
127.0.0.1,LP70,,
10.1.1.1,none,,
' | ConvertFrom-Csv -Header IP, Host, Status, Results
# Results
<#
IP Host Status Results
-- ---- ------ -------
127.0.0.1 LP70
10.1.1.1 none
#>
Reachability testing by IPAddress
'
127.0.0.1,LP70,,
10.1.1.1,none,,
' | ConvertFrom-Csv -Header IP, Host, Status, Results |
ForEach {Test-Connection -ComputerName $PSItem.IP -Count 1 -ErrorAction Stop}
# Results
<#
Source Destination IPV4Address IPV6Address Bytes Time(ms)
------ ----------- ----------- ----------- ----- --------
LP70 127.0.0.1 127.0.0.1 32 0
Test-Connection : Testing connection to computer '10.1.1.1' failed: Error due to lack of resources
At line:5 char:10
+ ForEach {Test-Connection -ComputerName $PSItem.IP -Count 1 -ErrorActi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (10.1.1.1:String) [Test-Connection], PingException
+ FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand
#>
Your code, was refactored a bit, and a sample file, with headers
IP,Host,Status,Results
127.0.0.1,LP70,,
10.1.1.1,none,,
Create a new or overwrite an existing results Csv file
New-Item -Path 'D:\Temp' -Name 'hostlistResults.csv' -ItemType File -Force |
Out-Null
Import-Csv -Path 'D:\Temp\hostlist.csv' |
ForEach-Object {
$Script:Source = $PSItem
Try
{
"Reachability testiong for $($PSItem.Host)"
$TestResutlts = Test-Connection -ComputerName $PSItem.IP -Count 1 -ErrorAction Stop
[PSCustomObject]@{
IP = $Source.IP
Host = $Source.Host
Destination = $TestResutlts.ProtocolAddress
Status = 'Successful'
} |
Export-Csv -Path 'D:\Temp\hostlistResults.csv' -Append -NoTypeInformation
}
Catch
{
Write-Warning -Message "Reachability testing for $($TestResutlts.PSComputerName) encounterd issues"
[PSCustomObject]@{
IP = $Source.IP
Host = $Source.Host
Destination = 'Not Successful'
Status = 'Not Successful'
} |
Export-Csv -Path 'D:\Temp\hostlistResults.csv' -Append -NoTypeInformation
}
}
# Results
<#
Reachability testing for LP70
Reachability testing for none
WARNING: Reachability testing for LP70 encountered issues
#>
Review your results via importing.
Import-Csv -Path 'D:\Temp\hostlistResults.csv'
# Results
<#
IP Host Destination Status
-- ---- ----------- ------
127.0.0.1 LP70 127.0.0.1 Successful
10.1.1.1 none Not Successful Not Successful
#>