title: PC ping script in Powershell
body: <p>The script constantly pings a list of PC hostnames from the import CSV column 1.</p>
  
  <blockquote>
    <p>Column 1 (Hostname) Column 2 (User) &larr;Manually updated field.</p>
  </blockquote>
  
  <p>When the script recognises 3 failed ping attempts, it emails our email address with Time/Hostname/Owner (Taken from the .csv file) From/IP address/Failed Pings.</p>
  
  <p>This works perfectly. However, when the the PC comes back online it sends another email with the above but will not show the correct owner. Can someone please help with associating <code>$Back</code> "owner" with the appropriate User? Like it works with the initial email response. At the moment it will either show nothing or the last name in the User column on the email, regardless the first notification email is correct.</p>
  
  <pre><code>$computers = Import-Csv C:\temp\Reporting\MainHosts.csv
  $Sources = @($Env:COMPUTERNAME)
  $To = "Username@Email.com"#,"Username@Email.com"
  $From = "Username@Email.com"
  $SMTPServer = "SERVER"
  [datetime]$TimeStop = "16:17"
  $StartTime = Get-Date -format 'dd-MM-yyyy hh:mm:ss'
  
  Clear-Host
  
  $Header = @"
      &lt;style&gt;BODY{font-family: Arial; font-size: 11pt;}
      TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;text-align: center;}
      TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;text-align: center;}
      TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;text-align: center;}
      &lt;/style&gt;
  "@
  
  ######################Load data array (Table) ######################
  $Status = @{}
  foreach ($Source in $Sources) {
      foreach ($computer in $computers) {
          $computername = $($computer.Hostname)
          $owner = $($computer.User)
          $Status.Add("$Source`:$computername", [PSCustomObject]@{
              Time = ""
              Hostname = $computername
              Owner = $owner
              From = $Source
              'IP Address' = $null
              'Failed Pings' = 0
          })
      }
  }
  
  ###################### Script Begin Message ######################
  do {
      $Results = foreach ($Source in $Sources) {
          foreach ($computer in $computers) {
              $computername = $($computer.Hostname) #Defining the Hostname column within .csv sheet
              $owner = $($computer.User)
              try {
                  Write-Host "." -NoNewline
                  Get-WmiObject "Win32_PingStatus" -ComputerName $Source -Filter "Address = '$computername'" -ErrorAction Stop | Select PSComputerName,Address,IPV4Address,StatusCode
              } catch {
                  Write-Warning "Error with $computername`: $($Error[0])"
              }
          }
      }
  
      $Back = @()
      foreach ($Result in $Results) {
          $Key = "$($Result.PSComputerName):$($Result.Address)"
          $Status[$Key].'IP Address' = $Result.IPV4Address
          $Status[$Key].Time = Get-Date
          if ($Result.StatusCode -eq 0) {
              if ($Status[$Key].'Failed Pings' -ge 3) {
                  $Back += [PSCustomObject]@{
                      Time = Get-Date
                      Hostname = $Result.Address
                      Owner = $owner
                      From = $Result.PSComputerName
                      IPAddress = $Result.IPV4Address
                      Status = "Connectivity Returned"
                  }
              }
              $Status[$Key].'Failed Pings' = 0
          } else {
              $Status[$Key].'Failed Pings' ++
          }
      }
      # Email Alert
      if ($Back) {
          $HTML = $Back | Sort Destination,From | ConvertTo-Html -Head $Header -PreContent "&lt;p&gt;Ping Detection Script has detected the following workstations are now online and &lt;b&gt;restored connectivity!&lt;/b&gt;&lt;br&gt;&lt;/p&gt;" | Out-String
          Send-MailMessage -To $To -From $From -Subject "**No Action Required** - Ping Detection Script Reporting Connections Restored" -Body $HTML -BodyAsHtml -SmtpServer $SMTPServer
      }
  
      $Data = $Status.Values | Where { $_.'Failed Pings' -eq 3 -or ( ($_.'Failed Pings' -ne 0 -and -not ($_.'Failed Pings' % $Alert))) }
      if ($Data) {
          $HTML = $Data | Sort Destination,From | ConvertTo-Html -Head $Header -PreContent "&lt;p&gt;Ping Detection Script has detected &lt;b&gt;Offline&lt;/b&gt; workstations! See below list&lt;br&gt;&lt;/p&gt;" | Out-String
          Send-MailMessage -To $To -From $From -Subject "**Urgent Action Required** - Ping Detection Script Reporting Offline Workstations" -Body $HTML -BodyAsHtml -SmtpServer $SMTPServer
          Write-Host "Alert Email Sent!`n"
          Start-Sleep -Seconds 1
      }
  
      Clear-Host
      $Status.Values | Sort Destination,From | Format-Table -AutoSize
      Write-Host "Ping Script Monitoring ..." -NoNewline
      Start-Sleep -Seconds 4
  } until ($Time.Hour -eq $TimeStop.Hour -and $Time.Minute -eq $TimeStop.Minute)
  Write-Host "`nScript shutting down, time limit reached:  $($TimeStop.Hour):$($TimeStop.Minute)"
  Write-Output $Status.Values | Sort Destination,From | Format-Table -AutoSize
  </code></pre>