# Powershell - Test-Connection

**URL:** <https://forums.powershell.org/t/powershell-test-connection/5905>\
**Category:** PowerShell Help\
**Created:** [March 3, 2016, 12:48am UTC](https://forums.powershell.org/t/powershell-test-connection/5905 "2016-03-03T00:48:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![codeobiect](https://avatars.discourse-cdn.com/v4/letter/c/ecc23a/32.png) [@codeobiect](https://forums.powershell.org/u/codeobiect)\
**Post date:** [March 3, 2016, 12:48am UTC](https://forums.powershell.org/t/powershell-test-connection/5905/1 "2016-03-03T00:48:01Z")

</div>

Ok, i try add Test-Connection and now the code look likes this:

```
$html=
@'

HTML TABLE
 
  .alert {{
    background-color: #FB1717 }}
    .statusCom {{
    background-color: #FB1717
    }}
  

SystemNameDeviceIDVolumeNameSize(GB)FreeSpace(GB)Status
{0}

'@

$entryTemplate = '{0}{1}{2}{3}{4}{5}{6}'
$alertEntryTemplate = '{0}{1}{2}{3}{4}{5}{6}'
$statusTemplate = '{0}{1}{2}{3}{4}{5}{6}{7}'
Function Get-ComInfo {   
param(
$computers
)

#Here LOW Space thresold is lessthan 10% of the total size of the Volume
$PercentFree = @{Name="FreeSpace(GB)";Expression={"{0:N1}" -f($_.freespace /1GB)}} 
Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computer $computers |
Select SystemName,DeviceID,VolumeName,$PercentFree,@{Name="Size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{name="PercentFree(%)";Expression={int}}

}
$servers = Get-Content U:\Users\xxx\Desktop\servers.txt 

$client = $servers
    if(Test-Connection -ComputerName (Get-Content U:\Users\xxx\Desktop\servers.txt ) -BufferSize 16 -Count 1 -Quiet) {
$entries = $servers | % { Get-ComInfo -computers $_ } | % {

    if ([float]::Parse($_.'FreeSpace(GB)') -le 5) {
        $alertEntryTemplate -f $_.SystemName, $_.DeviceID, $_.VolumeName, $_.'FreeSpace(GB)', $_.'Size(GB)', $_.'PercentFree(%)', $_.'LOW SPACE'
    }

    else {
        $entryTemplate -f $_.SystemName, $_.DeviceID, $_.VolumeName, $_.'FreeSpace(GB)', $_.'Size(GB)', $_.'PercentFree(%)', $_.'LOW SPACE'
    }
}
    }
    else{
        $statusTemplate -f $_.SystemName, $_.DeviceID, $_.VolumeName, $_.'FreeSpace(GB)', $_.'Size(GB)', $_.'PercentFree(%)', $_.'LOW SPACE', $_.'Status'
    }

$html -f ($entries -join ' ') | out-file U:\Users\xxx\Desktop\Drivess.html
$OutputFile = "U:\Users\xxx\Desktop\Drivess.html"
$SMTPServer = "smtp.xxx.com" 
$EmailFrom = "xxxx@xxx.com"
$EmailTo = "xxxx@xxx.com"
$Subject = "$computers PROGRES ;))"  
$body = (Get-Content $OutputFile) 
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25) 
$SMTPClient.EnableSsl = $false
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("xxx", "xxx");
$MailTextT = Get-Content -Path U:\Users\xxx\Desktop\Drivess.html -Raw
$msg = New-object Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $body)
$msg.IsBodyHTML = $true  
$SMTPClient.Send($msg)

In html i add this:

.statusCom {{
    background-color: #FB1717
    }}
`
And:
`
$statusTemplate = '{0}{1}{2}{3}{4}{5}{6}{7}' `
```

And i have more than one computer and one is offline in my servers.txt it i get this error:

```
` Get-WmiObject : Serwer RPC it is offline. (Expectiob HRESULT: 0x800706BA)
At U:\Users\xxx\Desktop\test.ps1:36 char:1
+ Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computer $comp ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand `
```

If i have one computer and it is offline a get this:

```

```

What i do wrong?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [March 3, 2016, 2:02am UTC](https://forums.powershell.org/t/powershell-test-connection/5905/2 "2016-03-03T02:02:11Z")

</div>

You’re running Test-Connection against _all_ of your servers at once. That basically means “if any of these servers are online, run the stuff here”. What you probably want is to put the Test-Connection call inside your ForEach-Object loop, so you ping each computer individually. (Alternatively, you could just let the Get-WmiObject call fail and handle the error, not worrying about Test-Connection first.)

---

<div class="post-metadata">

**Author:** ![codeobiect](https://avatars.discourse-cdn.com/v4/letter/c/ecc23a/32.png) [@codeobiect](https://forums.powershell.org/u/codeobiect)\
**Post date:** [March 6, 2016, 8:12pm UTC](https://forums.powershell.org/t/powershell-test-connection/5905/3 "2016-03-06T20:12:03Z")

</div>

I add this:

```
ForEach-Object {
$ListComputers = Get-Content "U:\Users\XXX\Desktop\servers.txt" 
    if(Test-Connection -ComputerName $ListComputers -Count 1 ){
      
Function Get-ComInfo {   
param(
$computers
)

#Here LOW Space thresold is lessthan 10% of the total size of the Volume
$PercentFree = @{Name="FreeSpace(GB)";Expression={"{0:N1}" -f($_.freespace /1GB)}} 
Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computer $computers|
Select SystemName,DeviceID,VolumeName,$PercentFree,@{Name="Size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{name="PercentFree(%)";Expression={int}}

}

$entries = Get-Content U:\Users\XXX\Desktop\servers.txt | % { Get-ComInfo -computers $_ } | % {

    if ([float]::Parse($_.'FreeSpace(GB)') -le 5) {
        $alertEntryTemplate -f $_.SystemName, $_.DeviceID, $_.VolumeName, $_.'FreeSpace(GB)', $_.'Size(GB)', $_.'PercentFree(%)', $_.'LOW SPACE'
    }

    else {
        $entryTemplate -f $_.SystemName, $_.DeviceID, $_.VolumeName, $_.'FreeSpace(GB)', $_.'Size(GB)', $_.'PercentFree(%)', $_.'LOW SPACE'
    }
    }
    }}if(!(test-connection -computername $ListComputers -quiet -count 1)){
        $entryTemplate -f $_.SystemName, $_.DeviceID, $_.VolumeName, $_.'FreeSpace(GB)', $_.'Size(GB)', $_.'PercentFree(%)', $_.'LOW SPACE'
    }
    }
```

And i get this error:

```
Get-WmiObject :server is unavailable. (Exception from HRESULT: 0x800706BA)
At U:\Users\XXX\Desktop\test.ps1:40 char:1
+ Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computer $comp ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
```

Is Foreach -Object is well, Because there is not credited me to do a report of computers that are unavailable.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:42pm UTC](https://forums.powershell.org/t/powershell-test-connection/5905/4 "2024-05-16T20:42:43Z")

</div>


