Replies: 1 comment
-
Not really sure what you're seeing here. It works as expected for me: $servers = 'google.com', 'facebook.com', 'github.com'
foreach ($server in $servers) {
if (Test-Connection $server -Quiet -Count 1) {
write-host "$server is online"
$online += $server
}
else {
write-host "$server is offline"
$offline += $server
}
}
PS> $online
I will note that since you've used If you use |
Beta Was this translation helpful? Give feedback.
-
I have this small command that worked fine until I updated to the latest powershell 7 build.
Any idea why the array collection would not work? After it completes, the $onlinecomputers and $offlinecomputers are a single variable with all the computer names mashed together instead of an array.
I ran this on a machine that had not been updated to Powershell 7, and the array collections worked fine.
[system.collections.arraylist]$Onlinecomputers=@()
[system.collections.arraylist]$Offlinecomputers=@()
ForEach ($server in $servers)
{
if (Test-Connection -ComputerName $server -Count 1 -Quiet)
{
Write-Host $server is online
$onlinecomputers += $server
}
Else
{
Write-Host $server is offline
$offlinecomputers += $server
}
}
Beta Was this translation helpful? Give feedback.
All reactions