Ping a list of computers and Find Who's Logged On ?

by May 9, 2014

If someone wouldn't mind taking pity on my terrible PowerShell skills, I'd greatly appreciate it.

Work for a school district and I'm always having to check to see if the computers in the labs are on and if anyone's logged in so I can then install software, patches etc..

I'm trying to get a script together that will ping a list of computers to see if they're on, then query them and tell me who is logged into each computer and output it to a .csv file.

 

I found these different scripts that do what I want separately but I just can't figure out how to put them together.

 

This one by Jaap Brasser does the Ping —- 

 

Get-Content -Path "c:powershellinputMachineList.txt" | ForEach-Object{

    Get-Content .comps.txt | ForEach-Object {

if(Test-Connection -ComputerName $_ -Quiet -Count 1) {
New-Object -TypeName PSCustomObject -Property @{
ComputerName = $_
'Ping Status' = 'Ok'
}
} else {
New-Object -TypeName PSCustomObject -Property @{
ComputerName = $_
'Ping Status' = 'Failed'
}
}
} | Export-Csv -Path "c:powershelloutputMachineList.csv -NoTypeInformation

 

I've got this other one that finds the logged on users —– it writes the output to the screen then to a file.

 

$MachineList = Get-Content -Path "c:powershellinputMachineList.txt"; 

foreach ($Machine in $MachineList)

($Machine + ": " + @(Get-WmiObject -ComputerName $Machine -Namespace rootcimv2 -Class Win32_ComputerSystem -erroraction silentlycontinue)[0].UserName); 

 

Write-Output ($Machine + ": " + @(Get-WmiObject -ComputerName $Machine -Namespace rootcimv2 -Class Win32_ComputerSystem -erroraction silentlycontinue)[0].UserName) | Out-File "c:powershelloutputMachineList.txt" -Append

}