# List of computer names or IP addresses.
# Note: Un-comment the preferred source
#$computers = @("ServerA", "ServerB")
#$computers = Get-Content -Path "C:\TEMP\AllNodes.txt
foreach ($computer in $computers) {
Write-Host "Users logged on to"$computer":"
try {
$loggedOnUsers = query user /server:$computer 2>&1 | ForEach-Object {
# Split the line into individual pieces of information
$userDetails = $_ -split '\s+'
if ($userDetails[1] -ne "USERNAME" -and $userDetails[1] -ne "No") {
# Filter out header and empty lines
$userDetails[1]
}
}
if ($loggedOnUsers -eq $null) {
Write-Host "No users are currently logged on to $computer."
} else {
$loggedOnUsers
}
} catch {
Write-Host "Error connecting to "$computer": $_"
}
Write-Host
}