Split Array based on Test-Connection

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
kingkale
Posts: 9
Last visit: Mon Jun 18, 2018 5:34 pm

Split Array based on Test-Connection

Post by kingkale »

Hello,

I am trying to split the $HostList ArrayList into $OnlineHosts and $OfflineHosts based on Test-Connection.

I know this command is supposed to work, I just tested it on one of my normal ps1 scripts by immediately doing a Write-Host of the online and offline arrays. Here is the section of working code from my ps1:
Clear-Host
$HostList = Get-Content "$Home\Documents\Scripts\ComputerList.txt"
Write-Host "Pinging selected computers to verify connection..."

$OnlineHosts, $OfflineHosts = $HostList.Where({Test-Connection $_ -Count 1 -Quiet},'Split')
$OnlineHosts
pause
$OfflineHosts
pause


Clear-Host
Write-Host "Could not contact the following computers: `n`n$OfflineHosts`n`nThe following
computers are online: `n`n$OnlineHosts`n`nContinue anyways?" -ForegroundColor Red
$ReadHost = Read-Host " (y/n) "
Switch ($ReadHost)
{
Y {Write-Host "Yes, Continuing to main menu."}
N {Write-Host "No, preparing to exit"
Pause
Exit}
}
Default
{
Write-Host "Yes, Continuing to main menu."}
}
However, in my Powershell Studio Project, it does not work. A little context: this is a new form that being passed the $HostList variable from a parent form. I tested that the child form is receiving the variable by doing Write-Host "$HostList woohoo!" I tested placing the Test-Connection code both into
and outside of the $formTest_Load command. Honestly I'm not sure what exactly to put or not put into the $formTest_Load command, so information about that would be helpful as well.

Here's the code from my PowerShell Studio Project:
param
(
[parameter(Mandatory = $true)]
[string]$global:HostList
)
$global:OnlineHosts = [System.Collections.ArrayList]@()
Write-Host "$HostList woohoo!"

$formTest_Load =
{
$OnlineHosts.Add($HostList)
$OnlineHosts, $OfflineHosts = $HostList.Where({Test-Connection $_ -Count 1 -Quiet }, 'Split')
}
Here is the error code I receive when I run my PowerShell Studio program:
ERROR: Test-Connection : Generic failure
formPing.psf (14, 49): ERROR: At Line: 14 char: 49
ERROR: + ... neHosts = $HostList.Where({Test-Connection $_ -Count 1 -Quiet }, 'Spl ...
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : InvalidOperation: (:) [Test-Connection], ManagementException
ERROR: + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand
ERROR:
My Test-Connection code seems to be character by character the same as from my old ps1 file.

Can anyone see what might be causing the error?

Thanks.

Also, here is information about what I am running:
-Windows 10 Pro
-PowerShell Studio 2018
-version 5.5.152.0
-64 bit
Last edited by kingkale on Sun May 27, 2018 5:17 am, edited 1 time in total.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Split Array based on Test-Connection

Post by jvierra »

You cannot use scope specifier in parameters and the parameter needs to be a string array.

Code: Select all

param (
     [parameter(Mandatory = $true)]
     [string[]]$HostList
)
User avatar
kingkale
Posts: 9
Last visit: Mon Jun 18, 2018 5:34 pm

Re: Split Array based on Test-Connection

Post by kingkale »

Progress update:
When I tried to retype my Test-Connection code, the '.Where' did not autofill and was not on the drop down.

Upon investigation, I noticed that the value of $HostList is being passed to the child form but it is being registered as a string, not an array. I changed the param block to reflect it as an ArrayList. It now reflects this change when I hover over $HostList in the child form and I am not receiving any error about my Test-Connection script.

However, when I do a Write-Host of $OnlineHosts, $OfflineHosts and $HostList at the very end of the child form, the value is still inside of $HostList.

Any thoughts?
User avatar
kingkale
Posts: 9
Last visit: Mon Jun 18, 2018 5:34 pm

Re: Split Array based on Test-Connection

Post by kingkale »

jvierra wrote: Sun May 27, 2018 6:21 am You cannot use scope specifier in parameters and the parameter needs to be a string array.

Code: Select all

param (
     [parameter(Mandatory = $true)]
     [string[]]$HostList
)
Just saw your reply, I got rid of the scope specifier and I tried to change it back to a string but it errored saying I cannot set it as string since its an ArrayList.

The following is what I have now and it's not pulling any errors but my 3 Write-Host tests at the bottom showed that the value is still in $HostList

param
(
[parameter(Mandatory = $true)]
[System.Collections.ArrayList]$HostList
)
$global:OnlineHosts = [System.Collections.ArrayList]@()

$formTest_Load =
{
$OnlineHosts, $OfflineHosts = $HostList.Where({Test-Connection $_ -Count 1 -Quiet }, 'Split')
}
Write-Host "1 $OnlineHosts"
Write-Host "2 $OnlineHosts"
Write-Host "3 $HostList"
Output:
1
2
3 lean-machine

jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Split Array based on Test-Connection

Post by jvierra »

You don't want an arraylist. Just get the file and use it.

I highly recommend doing the following Microsoft introductory video tutorial to PowerShell. It will clear up most of you misconceptions about programming with PowerShell.

https://mva.microsoft.com/en-us/trainin ... shell-8276

The following link will help you understand how to format your code and gives a good set of rules for designing and writing code.

PowerShell Style Guide
User avatar
kingkale
Posts: 9
Last visit: Mon Jun 18, 2018 5:34 pm

Re: Split Array based on Test-Connection

Post by kingkale »

Thanks for your time and help, I'm also trying to actively debug this while posting update. I will watch the video, but I am not a complete novice (despite what it may sound like). I do not have concrete knowledge of terms and many concepts but I know enough to google and find the answer.

I chose an ArrayList when creating a previous console version of this program because it needs to hold different numbers of values inside depending on user input, and also while I was writing the following command:

Code: Select all

$OnlineHosts, $OfflineHosts = $HostList.Where({Test-Connection $_ -Count 1 -Quiet }, 'Split')
I found out that Arrays cannot be split or deleted while being accessed, so I used an ArrayList instead. I'm sure it would be possible to do this with a string, but it would take some fancy footwork to select each value in the ArrayList in things like foreach, and it would be tough to define the right delimiters because there are different ways the user can input information into the ArrayList.

Does any of this make sense or matter? I am the beginner here so if you say to do it with a string, I'll go back and rewrite my code and learn how to make that happen. But I have gotten very far here and I have used the Test-Connect script block successfully in a ps1, so I dont think my issue is with the script block. But again, I'll go with whatever you think I should do as long as you have all of the information.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Split Array based on Test-Connection

Post by jvierra »

What is it that is not working? I cannot tell from your code bits what the exact issue is.

I still see no reason for an arraylist. It adds nothing in any code you have posted.

The code you posted should work with any kind of collection.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Split Array based on Test-Connection

Post by jvierra »

What makes you think that the "Where" will remove anything from $HostList?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Split Array based on Test-Connection

Post by jvierra »

The "Where" clause works as described here:

https://learn-powershell.net/2013/11/26 ... rshell-v4/

It does not alter the collection. It returns the result as a new object or collection.
User avatar
kingkale
Posts: 9
Last visit: Mon Jun 18, 2018 5:34 pm

Re: Split Array based on Test-Connection

Post by kingkale »

Run this code in ISE:

Code: Select all

$HostList = [System.Collections.ArrayList]@()
$HostList += HostName 
$HostList += "FakeComputer"
Clear-Host

Write-Host "HostList contains: $HostList"
pause
Clear-Host

Write-Host "Verifying connection to target hosts..."
$OnlineHosts, $OfflineHosts = $HostList.Where({Test-Connection $_ -Count 1 -Quiet }, 'Split')
Clear-Host

Write-Host "HostList contains: $HostList`n"
Write-Host "OnlineHosts contains: $OnlineHosts`n"
Write-Host "OfflineHosts contains: $OfflineHosts`n"
pause
You will see that your computer name and a fake computer name get added to the $HostList array.
When you hit enter, the test connection runs and tests each computer inside of $HostList and splits them based on Test-Connection returning true or false.
This topic is 5 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked