title: PowerShell Function Can-Ping doesn't return $True or $False with switch
body: <p>I wrote a function to check if a client is online or offline, this works great! However, when I try to use the switch <code>-Remember</code> it doesn't iterate the scriptblock <code>$PingCheck</code> as desired because the test has already been done once, but it's not returning <code>$true</code> or <code>$false</code> either. </p>
  
  <p>What would be the best way to have the function always output <code>$true</code> or <code>$false</code> but don't go through the script block on duplicates?</p>
  
  <p>Thank you for your help as always.</p>
  
  <pre><code># Function to check if $Server is online
  Function Can-Ping ($Server,[switch]$SendMail,[switch]$Remember) {
  
   $PingCheck = {
  
          $Error.Clear()
  
          if (Test-Connection -ComputerName $Server -BufferSize 16 -Count 1 -ErrorAction 0 -quiet) { # ErrorAction 0 doesn't display error information when a ping is unsuccessful
  
              Write-Host "$(Get-TimeStamp) $Server &gt; Function Can-Ping: Ping test ok" -ForegroundColor Gray; return $true
          } 
          else {
              $Error.Clear()
              Write-Host "$(Get-TimeStamp) $Server &gt; Function Can-Ping: Ping test FAILED" -ForegroundColor Gray
  
              Write-Host "$(Get-TimeStamp) $Server &gt; Function Can-Ping: Flushing DNS" -ForegroundColor Gray
              ipconfig /flushdns | Out-Null
  
              Write-Host "$(Get-TimeStamp) $Server &gt; Function Can-Ping: Registering DNS" -ForegroundColor Gray
              ipconfig /registerdns | Out-Null
  
              Write-Host "$(Get-TimeStamp) $Server &gt; Function Can-Ping: NSLookup for $Server" -ForegroundColor Gray
              nslookup $Server | Out-Null # Surpressing error here is not possible unless using '2&gt; $null', but if we do this, we don't get $true or $false for the function so '| Out-Null' is an obligation
              if (!$?) {
                  Write-Host "$(Get-TimeStamp) $Server &gt; Function Can-Ping: NSlookup can't find the hostname, DNS issues or hostname incorrect?" -ForegroundColor Yellow
                  # Write-Host $Error -ForegroundColor Red
                  if ($SendMail) {
                      Send-Mail "FAILED Ping test" "$(Get-TimeStamp) NSlookup can't find $Server, hostname incorrect or DNS issues?" "$error"
                  }
                  $script:arrayCanPingError += "ERROR | $(Get-TimeStamp) Ping test failed: NSlookup can't find $Server, hostname incorrect or DNS issues?`n$error"
                  $script:HTMLarrayCanPingError += "ERROR | $(Get-TimeStamp) Ping test failed:&lt;br&gt;NSlookup can't find $Server, hostname incorrect or DNS issues?&lt;br&gt;$error&lt;br&gt;"
                  return $false
                  }
              else {
                  Write-Host "$(Get-TimeStamp) $Server &gt; Function Can-Ping: Re-pinging $Server" -ForegroundColor Gray
                  if (Test-Connection -ComputerName $Server -BufferSize 16 -Count 1 -ErrorAction 0 -Quiet) {
                     Write-Host "$(Get-TimeStamp) $Server &gt; Function Can-Ping: Ping test ok, problem resolved" -ForegroundColor Gray
                     return $true
                  }
                  else {
                        Write-Host "$(Get-TimeStamp) $Server &gt; Function Can-Ping: DNS Resolving is ok but can't connect, server offline?" -ForegroundColor Yellow
                        if ($SendMail) {
                            Send-Mail "FAILED Ping test" "$error" "DNS Resolving is ok but can't connect to $Server, server offline?"
                        } 
                        $script:arrayCanPingError += "ERROR Ping test failed: DNS Resolving is ok but can't connect to $Server, server offline?`n$error"
                        $script:HTMLarrayCanPingError += "ERROR Ping test failed: DNS Resolving is ok but can't connect to $Server, server offline?&lt;br&gt;$error&lt;br&gt;"
                        return $false
                  }
              }
          }
      }
  
      # Call the scriptblock $PingAction every time, unless the switch $Remember is provided, than we only check each server once
      if ($Remember) {
          Write-Host "$(Get-TimeStamp) $Server &gt; Function Can-Ping: Switch '-Remember' detected" -ForegroundColor Gray
          While ($tmpPingCheckServers -notcontains $Server) { 
                    &amp;$PingCheck
                    $script:tmpPingCheckServers = @($tmpPingCheckServers+$Server) #Script wide variable, othwerwise it stays empty when we leave the function / @ is used to store it as an Array (table) instaed of a string
          }           
      } 
      else {
            &amp;$PingCheck
      }
  }
  </code></pre>