# Stop script if test connection failes

**URL:** <https://forums.powershell.org/t/stop-script-if-test-connection-failes/6022>\
**Category:** PowerShell Help\
**Created:** [March 17, 2016, 7:10pm UTC](https://forums.powershell.org/t/stop-script-if-test-connection-failes/6022 "2016-03-17T19:10:37Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![andy-smith](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@andy-smith](https://forums.powershell.org/u/andy-smith)\
**Post date:** [March 17, 2016, 7:10pm UTC](https://forums.powershell.org/t/stop-script-if-test-connection-failes/6022/1 "2016-03-17T19:10:37Z")

</div>

I’m trying to get my script to stop if test connection is False.

It keeps trying to query the remote PC even if it fails

function checkhw {

$HostName=$InputBox.text;  
$newLine = [System.Environment]::NewLine  
foreach ($HostName In $InputBox.text){  
$ping = (Test-Connection -ComputerName $HostName -Count 1 -quiet )  
try  
{  
$errorActionPreference = “stop”  
$outputBoxping.text = $ping  
$outputboxquery.text = write “Querying: $HostName”  
$computerSystem = get-wmiobject Win32\_ComputerSystem -ComputerName $HostName  
$computerBIOS = get-wmiobject Win32\_BIOS -ComputerName $HostName  
$computerOS = get-wmiobject Win32\_OperatingSystem -ComputerName $HostName  
$computerOSB = get-wmiobject Win32\_OperatingSystem -ComputerName $HostName  
$computerBLD = get-wmiobject Win32\_OperatingSystem -ComputerName $HostName  
$computerCPU = get-wmiobject Win32\_Processor -ComputerName $HostName  
$computerHDD = Get-WmiObject Win32\_LogicalDisk -ComputerName $HostName -Filter “DeviceID=‘C:’”  
$computerLCD = get-wmiobject Win32\_DesktopMonitor -ComputerName $HostName  
$outputboxquery.text = write “Done”  
$Man = $computerSystem.Manufacturer  
$Mod = $computerSystem.model -replace “1095BV6”, “C30” -replace “3664AK7”, “M72e” -replace “30A0S0AT0A”, “E32”-replace “32373A0”, “M92p” -replace “3237EF9”, “M92p” -replace “3167BC8”, “M71e” -replace “10A4S00R0D”, “M93” -replace “3664AK9”, “M72e” -replace “3267B69”, “M72e Tiny” -replace “0833AL2”, “M70e” -replace “4263BM6”, “C20” -replace “30A8S00300”, “P700” -replace “11363A0”, “C30” -replace “30AGS0CR05”, “P300” -replace “43516D7”, “S30” -replace “0606C26”, “S30” -replace “43517A4”, “S30 QA” -replace “VMware Virtual Platform”, “VDI” -replace “30A8S00400”, “P700 QA” -replace “4157ER5”, “S20”  
$Ser = $computerBIOS.SerialNumber  
$CPUs = $ComputerSystem.NumberOfProcessors  
$Cores = $ComputerSystem.NumberOfLogicalProcessors  
$HDD = “{0:N2}” -f ($computerHDD.Size/1GB) + “GB”  
$RAM = “{0:N2}” -f ($computerSystem.TotalPhysicalMemory/1GB) + “GB”  
$HDDF = “{0:N2}” -f ($computerHDD.freespace/1GB) + “GB”  
$OS = $computerOS.caption -replace “Microsoft Windows 7 Enterprise”, “Win 7” -replace “Microsoft Windows XP Professional”, “XP”  
$OSB = $computerOSB.ConvertToDateTime(($computerOSB).InstallDate)  
$Usr = $computerSystem.UserName  
$Check = write " Hostname: " $computerSystem.Name, $newline, "Manufacturer: " $Man, $newline, "Model: " $Mod, $newline, "Serial: " $Ser, $newline, "No. CPU’s: " $CPUs,$newline, "No. Cores’s: " $Cores, $newline, "Memory: " $RAM, $newline, "HDD Size: " $HDD, $newline, "HDD Free Space: " $HDDF, $newline, "Operating System: " $OS, $newline, "Built Date: " $OSB, $newline, "User Logged in: " $Usr  
$outputBoxHW.text=$Check  
}

```
        catch 
        { 
           out-file $labelPing.text         
        }}
         }

```

---

<div class="post-metadata">

**Author:** ![paal-braathen](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/paal-braathen/32/523_2.png) [@paal-braathen](https://forums.powershell.org/u/paal-braathen)\
**Post date:** [March 17, 2016, 7:31pm UTC](https://forums.powershell.org/t/stop-script-if-test-connection-failes/6022/2 "2016-03-17T19:31:52Z")

</div>

1. 

You should wrap your script in “pre” tags when posting. Also make sure the indentation is correct. Then it will look like this:

```
function checkhw {
	$HostName=$InputBox.text;
	$newLine = [System.Environment]::NewLine
	foreach ($HostName In $InputBox.text) {
		$ping = (Test-Connection -ComputerName $HostName -Count 1 -quiet)
		try
		{
			$errorActionPreference = "stop"
			$outputBoxping.text = $ping
			$outputboxquery.text = write "Querying: $HostName"
			$computerSystem = get-wmiobject Win32_ComputerSystem -ComputerName $HostName
			$computerBIOS = get-wmiobject Win32_BIOS -ComputerName $HostName
			$computerOS = get-wmiobject Win32_OperatingSystem -ComputerName $HostName
			$computerOSB = get-wmiobject Win32_OperatingSystem -ComputerName $HostName
			$computerBLD = get-wmiobject Win32_OperatingSystem -ComputerName $HostName
			$computerCPU = get-wmiobject Win32_Processor -ComputerName $HostName
			$computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $HostName -Filter "DeviceID='C:'"
			$computerLCD = get-wmiobject Win32_DesktopMonitor -ComputerName $HostName
			$outputboxquery.text = write "Done"
			$Man = $computerSystem.Manufacturer
			$Mod = $computerSystem.model -replace "1095BV6", "C30" -replace "3664AK7", "M72e" -replace "30A0S0AT0A", "E32"-replace "32373A0", "M92p" -replace "3237EF9", "M92p" -replace "3167BC8", "M71e" -replace "10A4S00R0D", "M93" -replace "3664AK9", "M72e" -replace "3267B69", "M72e Tiny" -replace "0833AL2", "M70e" -replace "4263BM6", "C20" -replace "30A8S00300", "P700" -replace "11363A0", "C30" -replace "30AGS0CR05", "P300" -replace "43516D7", "S30" -replace "0606C26", "S30" -replace "43517A4", "S30 QA" -replace "VMware Virtual Platform", "VDI" -replace "30A8S00400", "P700 QA" -replace "4157ER5", "S20"
			$Ser = $computerBIOS.SerialNumber
			$CPUs = $ComputerSystem.NumberOfProcessors
			$Cores = $ComputerSystem.NumberOfLogicalProcessors
			$HDD = "{0:N2}" -f ($computerHDD.Size/1GB) + "GB"
			$RAM = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
			$HDDF = "{0:N2}" -f ($computerHDD.freespace/1GB) + "GB"
			$OS = $computerOS.caption -replace "Microsoft Windows 7 Enterprise", "Win 7" -replace "Microsoft Windows XP Professional", "XP"
			$OSB = $computerOSB.ConvertToDateTime(($computerOSB).InstallDate)
			$Usr = $computerSystem.UserName
			$Check = write " Hostname: " $computerSystem.Name, $newline, "Manufacturer: " $Man, $newline, "Model: " $Mod, $newline, "Serial: " $Ser, $newline, "No. CPU's: " $CPUs,$newline, "No. Cores's: " $Cores, $newline, "Memory: " $RAM, $newline, "HDD Size: " $HDD, $newline, "HDD Free Space: " $HDDF, $newline, "Operating System: " $OS, $newline, "Built Date: " $OSB, $newline, "User Logged in: " $Usr
			$outputBoxHW.text=$Check
		} catch {
			out-file $labelPing.text
		}
	}
}
```

1. 

This script does not run properly. You’re using undeclared variables. I would recommend that you set the following:

```
Set-StrictMode -Version “latest”
```
 This will give you errors when trying to use undeclared variables. Read about Set-StrictMode here: [https://technet.microsoft.com/en-us/library/hh849692.aspx](https://technet.microsoft.com/en-us/library/hh849692.aspx)

1. 

It’s not that easy to understand what you need help with since the script doesn’t run properly, but if you want to skip the foreach when the Test-Connection fails you could just add this:

```
$ping = (Test-Connection -ComputerName $HostName -Count 1 -quiet)
if (-not $ping) {
continue
}
```
  
Read about the “continue” statement here: [https://technet.microsoft.com/en-us/library/hh847821.aspx](https://technet.microsoft.com/en-us/library/hh847821.aspx)

---

<div class="post-metadata">

**Author:** ![andy-smith](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@andy-smith](https://forums.powershell.org/u/andy-smith)\
**Post date:** [March 17, 2016, 8:02pm UTC](https://forums.powershell.org/t/stop-script-if-test-connection-failes/6022/3 "2016-03-17T20:02:00Z")

</div>

Thanks Paal

The continue works a treat.

And i’m reading up on the strictmode 🙂

I have an output box the displays the True or False of test connection.  
It doesn’t display the False anymore when it fails

Any Ideas?

$outputBoxping = New-Object System.Windows.Forms.TextBox  
$outputBoxping.Location = New-Object System.Drawing.Size(70,380)  
$outputBoxping.Size = New-Object System.Drawing.Size(50,20)  
$Form.Controls.Add($outputBoxping)

---

<div class="post-metadata">

**Author:** ![paal-braathen](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/paal-braathen/32/523_2.png) [@paal-braathen](https://forums.powershell.org/u/paal-braathen)\
**Post date:** [March 17, 2016, 8:21pm UTC](https://forums.powershell.org/t/stop-script-if-test-connection-failes/6022/4 "2016-03-17T20:21:53Z")

</div>

Could you post a full example that runs without errors (also when using strict mode)?

---

<div class="post-metadata">

**Author:** ![bobmccoy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bobmccoy/32/650_2.png) [@bobmccoy](https://forums.powershell.org/u/bobmccoy)\
**Post date:** [March 17, 2016, 8:24pm UTC](https://forums.powershell.org/t/stop-script-if-test-connection-failes/6022/5 "2016-03-17T20:24:31Z")

</div>

OK, my post has _nothing_ to do with stopping your script. But I need to address that mess for replacing model numbers. If you need to have the data in the script, it would be much better addressed as a hash table to do the translation. It cleans up the script considerably making it easier to maintain, and it removes all the unnecessary replaces on a string that has already been replaced. Consider this …

```
$Mod = "10A4S00R0D" #sample for test, replace with $Mod = $computerSystem.model

# the table should be defined before the $host loop, not in it.
$modTable = @{
    "1095BV6" = "C30"
    "3664AK7" = "M72e"
    "30A0S0AT0A" = "E32"
    "32373A0" = "M92p"
    "3237EF9" = "M92p"
    "3167BC8" = "M71e"
    "10A4S00R0D" = "M93"
    "3664AK9" = "M72e"
    "3267B69" = "M72e Tiny"
    "0833AL2" = "M70e"
    "4263BM6" = "C20"
    "30A8S00300" = "P700"
    "11363A0" = "C30"
    "30AGS0CR05" = "P300"
    "43516D7" = "S30"
    "0606C26" = "S30"
    "43517A4" = "S30 QA"
    "VMware Virtual Platform" = "VDI"
    "30A8S00400" = "P700 QA"
    "4157ER5" = "S20"
}
if ($modTable.keys -contains $Mod)
{
    $Mod = $modTable[$Mod]
}
$Mod
```

“But wait, there’s more!” But that’s still not the best solution. The best would be be to extract the data altogether from your script. Move your data to a CSV file like this …

```
Old,New
"1095BV6","C30"
"3664AK7","M72e"
"30A0S0AT0A","E32"
```

… and then read it in to create a lookup dictionary;

```
$Mod = "3664AK7" # sample input
# Create dictionary (before $host loop)
$content = Import-CSV -Path .\modtable.csv
$modTable = $content | Group-Object -AsHashTable -AsString -Property old
if ($modTable.keys -contains $Mod)
{
    $Mod = $modTable[$Mod].New
}
$Mod
```

That way, if you need to add/change/delete models, you don’t have to edit a working production script.

---

<div class="post-metadata">

**Author:** ![andy-smith](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@andy-smith](https://forums.powershell.org/u/andy-smith)\
**Post date:** [March 17, 2016, 9:27pm UTC](https://forums.powershell.org/t/stop-script-if-test-connection-failes/6022/6 "2016-03-17T21:27:32Z")

</div>

Thanks for all your help guys.

But how do i paste it using this pre thing…

Sorry i am a newbie

---

<div class="post-metadata">

**Author:** ![bobmccoy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bobmccoy/32/650_2.png) [@bobmccoy](https://forums.powershell.org/u/bobmccoy)\
**Post date:** [March 17, 2016, 9:41pm UTC](https://forums.powershell.org/t/stop-script-if-test-connection-failes/6022/7 "2016-03-17T21:41:36Z")

</div>

You paste in you code like you would normally, but you surround it with the pre and /pre tags.

---

<div class="post-metadata">

**Author:** ![andy-smith](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@andy-smith](https://forums.powershell.org/u/andy-smith)\
**Post date:** [March 17, 2016, 9:45pm UTC](https://forums.powershell.org/t/stop-script-if-test-connection-failes/6022/8 "2016-03-17T21:45:03Z")

</div>

This is my code for checking desktop hardware. I’m trying not to use external sources like csv as i want to roll it up into an exe for others to use.

```
Set-StrictMode -Version "latest"
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$Form = New-Object System.Windows.Forms.Form
$Form.BackColor = "White"
$Form.StartPosition = "CenterScreen"
$User = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$Form.text = "Test"
$Form.Size = New-Object System.Drawing.Size(800,510)

############################################## Start Check Hardware functions

function checkhw {

$HostName=$InputBox.text; 
$newLine = [System.Environment]::NewLine 
foreach ($HostName In $InputBox.text){
$ping = (Test-Connection -ComputerName $HostName -Count 1 -quiet)
if (-not $ping) {
    continue  
    }
            $outputBoxping.text = $ping  
            $errorActionPreference = "stop"
            $outputboxquery.text = write "Querying: $HostName"
            $computerSystem = get-wmiobject Win32_ComputerSystem -ComputerName $HostName
            $computerBIOS = get-wmiobject Win32_BIOS -ComputerName $HostName
            $computerOS = get-wmiobject Win32_OperatingSystem -ComputerName $HostName
            $computerOSB = get-wmiobject Win32_OperatingSystem -ComputerName $HostName
            $computerBLD = get-wmiobject Win32_OperatingSystem -ComputerName $HostName
            $computerCPU = get-wmiobject Win32_Processor -ComputerName $HostName
            $computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $HostName -Filter "DeviceID='C:'"
            $computerLCD = get-wmiobject Win32_DesktopMonitor -ComputerName $HostName
            $outputboxquery.text = write "Done"
            $Man = $computerSystem.Manufacturer
            $Mod = $computerSystem.model -replace "1095BV6", "C30" -replace "3664AK7", "M72e" -replace "30A0S0AT0A", "E32"-replace "32373A0", "M92p" -replace "3237EF9", "M92p" -replace "3167BC8", "M71e" -replace "10A4S00R0D", "M93" -replace "3664AK9", "M72e" -replace "3267B69", "M72e Tiny" -replace "0833AL2", "M70e" -replace "4263BM6", "C20" -replace "30A8S00300", "P700" -replace "11363A0", "C30" -replace "30AGS0CR05", "P300" -replace "43516D7", "S30" -replace "0606C26", "S30" -replace "43517A4", "S30 QA" -replace "VMware Virtual Platform", "VDI" -replace "30A8S00400", "P700 QA" -replace "4157ER5", "S20"
            $Ser = $computerBIOS.SerialNumber
            $CPUs = $ComputerSystem.NumberOfProcessors
            $Cores = $ComputerSystem.NumberOfLogicalProcessors
            $HDD = "{0:N2}" -f ($computerHDD.Size/1GB) + "GB"
            $RAM = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
            $HDDF = "{0:N2}" -f ($computerHDD.freespace/1GB) + "GB" 
            $OS = $computerOS.caption -replace "Microsoft Windows 7 Enterprise", "Win 7" -replace "Microsoft Windows XP Professional", "XP"
            $OSB = $computerOSB.ConvertToDateTime(($computerOSB).InstallDate)
            $Usr = $computerSystem.UserName
            $Check = write " Hostname: " $computerSystem.Name, $newline, "Manufacturer: " $Man, $newline, "Model: " $Mod, $newline, "Serial: " $Ser, $newline, "No. CPU's: " $CPUs,$newline, "No. Cores's: " $Cores, $newline, "Memory: " $RAM, $newline, "HDD Size: " $HDD, $newline, "HDD Free Space: " $HDDF, $newline, "Operating System: " $OS, $newline, "Built Date: " $OSB, $newline, "User Logged in: " $Usr
            $outputBoxHW.text=$Check
            }
 
            { 
               out-file $labelPing.text         
            }
             }
				           
############################################## End Check Hardware functions

############################################## Start Heading Label

$labelHead = New-Object Windows.Forms.Label
$labelHead.ForeColor = "darkgray"
$labelHead.Location = New-Object Drawing.Point 320,40
$labelHead.Size = New-Object Drawing.Point 300,60
$labelHead.text = "Desktop Information Tool"
$Form.Controls.Add($labelHead)

############################################## end heading Label

############################################## Start HostName Label

$labelHN = New-Object Windows.Forms.Label
$labelHN.Location = New-Object Drawing.Point 10,25
$labelHN.Size = New-Object Drawing.Point 120,20 
$labelHN.text = "Enter Hostname"
$Form.Controls.Add($labelHN)

############################################## end Hostname Label

############################################## Start Ping Label

$labelPing = New-Object Windows.Forms.Label
$labelPing.Location = New-Object Drawing.Point 10,382
$labelPing.Size = New-Object Drawing.Point 50,17 
$labelPing.text = "Pinging?"
$Form.Controls.Add($labelPing)

############################################## end Ping functions

############################################## Start Hardware Info Label

$labelquery = New-Object Windows.Forms.Label
$labelquery.Location = New-Object Drawing.Point 10,100
$labelquery.Size = New-Object Drawing.Point 170,17 
$labelquery.text = "Hardware Information"
$Form.Controls.Add($labelquery)

############################################## end Hardware Info Label

############################################## Start Version Label

$labelver = New-Object Windows.Forms.Label
$labelver.Location = New-Object Drawing.Point 725,450
$labelver.Size = New-Object Drawing.Point 30,30 
$labelver.text = "v1.0"
$Form.Controls.Add($labelver)

############################################## end Version Labe

############################################## Start text fields

$InputBox = New-Object System.Windows.Forms.TextBox
$InputBox.Location = New-Object System.Drawing.Size(10,45)
$InputBox.Size = New-Object System.Drawing.Size(150,20)
$Form.Controls.Add($InputBox)

$outputBoxHW = New-Object System.Windows.Forms.TextBox
$outputBoxHW.Location = New-Object System.Drawing.Size(10,120)
$outputBoxHW.Size = New-Object System.Drawing.Size(325,245)
$outputBoxHW.MultiLine = $True
$Form.Controls.Add($outputBoxHW)

############################################## end text fields

############################################## Start Output Build Check Box

$outputBoxBC = New-Object System.Windows.Forms.TextBox
$outputBoxBC.Location = New-Object System.Drawing.Size(365,120)
$outputBoxBC.Size = New-Object System.Drawing.Size(325,245)
$outputBoxBC.MultiLine = $True
$Form.Controls.Add($outputBoxBC)

############################################## end Output Build Check Box

############################################## Start Ping Output box

$outputBoxping = New-Object System.Windows.Forms.TextBox
$outputBoxping.Location = New-Object System.Drawing.Size(70,380)
$outputBoxping.Size = New-Object System.Drawing.Size(50,20)
$Form.Controls.Add($outputBoxping)

############################################## end Ping Output box

############################################## Start Hardware buttons

$ButtonHW = New-Object System.Windows.Forms.Button
$ButtonHW.Location = New-Object System.Drawing.Size(180,30)
$ButtonHW.Size = New-Object System.Drawing.Size(75,50)
$ButtonHW.Text = "Check Hardware"
$ButtonHW.Add_Click({$outputBoxHW.Text=""})
$ButtonHW.Add_Click({$outputBoxping.Text=""})
$ButtonHW.Add_Click({checkhw})
$Form.Controls.Add($ButtonHW)

############################################## end Hardware buttons

############################################## Start Close buttons

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Size(700,413)
$cancelButton.Size = New-Object System.Drawing.Size(75,25)
$cancelButton.Text = "Close"
$cancelButton.Add_Click({ $form.Tag = $null; $form.Close() })
$Form.Controls.Add($cancelButton)

############################################## end Close buttons

$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
```

---

<div class="post-metadata">

**Author:** ![bobmccoy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bobmccoy/32/650_2.png) [@bobmccoy](https://forums.powershell.org/u/bobmccoy)\
**Post date:** [March 17, 2016, 10:00pm UTC](https://forums.powershell.org/t/stop-script-if-test-connection-failes/6022/9 "2016-03-17T22:00:04Z")

</div>

If you don’t want external data that’s fine. But the multi-replace is very inefficient and would be better served by the hash table in your function.

---

<div class="post-metadata">

**Author:** ![bobmccoy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bobmccoy/32/650_2.png) [@bobmccoy](https://forums.powershell.org/u/bobmccoy)\
**Post date:** [March 17, 2016, 10:04pm UTC](https://forums.powershell.org/t/stop-script-if-test-connection-failes/6022/10 "2016-03-17T22:04:48Z")

</div>

And I am confused at to your use of $labelping in your function checkhw as it is defined after checkhw and will not be available to it.

---

<div class="post-metadata">

**Author:** ![andy-smith](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@andy-smith](https://forums.powershell.org/u/andy-smith)\
**Post date:** [March 17, 2016, 10:04pm UTC](https://forums.powershell.org/t/stop-script-if-test-connection-failes/6022/11 "2016-03-17T22:04:51Z")

</div>

Yea i’ll do that Bob.  
Not sure how though 🙂  
It’s only required because Lenovo use a code instead of an easy name… All HP desktops display fine.

---

<div class="post-metadata">

**Author:** ![andy-smith](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@andy-smith](https://forums.powershell.org/u/andy-smith)\
**Post date:** [March 17, 2016, 10:25pm UTC](https://forums.powershell.org/t/stop-script-if-test-connection-failes/6022/12 "2016-03-17T22:25:53Z")

</div>

Thanks Bob

I got the Hash table working and removed the $labelping.

Only thing i’m stuck on now is it displaying test-connection false when the script stops

---

<div class="post-metadata">

**Author:** ![bobmccoy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bobmccoy/32/650_2.png) [@bobmccoy](https://forums.powershell.org/u/bobmccoy)\
**Post date:** [March 17, 2016, 10:48pm UTC](https://forums.powershell.org/t/stop-script-if-test-connection-failes/6022/13 "2016-03-17T22:48:50Z")

</div>

Did you “lift” this code from somewhere else? There are a lot of inefficiencies in it, and I’m just looking a the one checkhw function. For instance you make three separate calls to the WMI Win32\_OperatingSystem calls when all you need is one and then split out the parameters (as was done with Win32\_ComputerSystem).

---

<div class="post-metadata">

**Author:** ![andy-smith](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@andy-smith](https://forums.powershell.org/u/andy-smith)\
**Post date:** [March 17, 2016, 11:07pm UTC](https://forums.powershell.org/t/stop-script-if-test-connection-failes/6022/14 "2016-03-17T23:07:48Z")

</div>

Yes bob

I googled everything about this and changing it to suit my needs… There is more to the code but has company info in it so not posting 🙂

I appriciate all the assistance you have given me.

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [March 18, 2016, 1:00am UTC](https://forums.powershell.org/t/stop-script-if-test-connection-failes/6022/15 "2016-03-18T01:00:22Z")

</div>

Don’t put the test connection inside the function.

```
function checkhw {
	
	param($computername)
	
	Write-Host $computername
	
}

foreach ($computername in $computernames) {
	
	if (Test-Connection $computername -Count 1 -Quiet) { checkhw $computername }
	
}
```

---

<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/stop-script-if-test-connection-failes/6022/16 "2024-05-16T20:42:37Z")

</div>


