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:
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 intoClear-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."}
}
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:
Here is the error code I receive when I run my PowerShell Studio program: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')
}
My Test-Connection code seems to be character by character the same as from my old ps1 file.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:
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