0

I am trying to test a network connection and if it's good, tell me it is found. ELSE write-host it is not found, and export that computer to a CSV. However, I am just getting the error "Testing connection to computer failed: error due to lack of resources." on computers that cannot get a connection. Does test-connection not work this way? The = "True" is working for me. Below is my code:

foreach ($computer in $Computers)
$computername = $computer.Computers
$testConnection = Test-Connection $computername
IF ($testConnection = "True")
    {
        {
    Write-Host "$computername found"
        }
        ElSE
        {
    Write-Host "$computername does not have CCleaner installed." #| Export-Csv C:\Users\jcheng\Desktop\Scripts\PingTestLog.txt -Append
        }
    {
DrixlRey
  • 205
  • 3
  • 4
  • 13
  • Possible duplicate of [Powershell - Test-Connection failed due to lack of resources](https://stackoverflow.com/questions/41267553/powershell-test-connection-failed-due-to-lack-of-resources) – Sage Pourpre Sep 20 '17 at 16:41

2 Answers2

1

Give this a try

foreach ($computer in $Computers)
$computername = $computer.Computers
$testConnection = Test-Connection $computername

If (($testConnection -ne "") -or ($testconnection -ne $null){
    Write-Host "$computername found"
}
Else{
    Write-Host "$computername does not have CCleaner installed." #| Export-Csv C:\Users\jcheng\Desktop\Scripts\PingTestLog.txt -Append
}

I would check on this: $computer.Computers, to see if it is actually returning the property that you are looking for.

As a side note, make sure your CCleaner version is newer than 5.33 :) it was recently breached

cet51
  • 1,196
  • 2
  • 11
  • 29
  • Hey this didn't work, the script just kept saying the computer was found, even when I put connections that could not have possibly connected. Yes the computers are correct. This was just a leftover script from me removing CCleaner thanks for the tips! – DrixlRey Sep 20 '17 at 17:00
  • Wait, I got it to work, I only used the $null option. Thanks a lot! Below is the script: IF ($testConnection -ne $null) { Write-Host "$computername found" } ElSE { $log = "$computername cannot be connected." $log $log | Out-File 'C:\Users\Log.txt'-Append } – DrixlRey Sep 20 '17 at 17:28
  • @DrixlRey Glad to help – cet51 Sep 20 '17 at 19:16
0

Use this

If ((Test-Connection $computername).PingSucceeded) { ... }

Steve
  • 1
  • 1