Skip to content

Check online status on a bunch och clients

To check a bunch of clients to see if they are online or not, when trying to push software onto them and it takes longer than usual.

I wrote this to help me when i worked with SCCM, because sometimes the SCCM client would just not update information in timely fashion, just as a control to see if they where online or not, when pushing important software updates.

The script requires to be run from a machine with access to the client network at hand and a list of the machines you wish to check.

You can output the status either to the console or to a log file, i named the script CheckOnline.ps1, but you can name it anything you want.

Output to console:

.\CheckOnline.ps1 -File .\Computerlist.txt

Output to log:

.\CheckOnline.ps1 -File .\Computerlist.txt -Output .\ComputerStatus.txt

Full script.

# Required parameters.
param (
    # -File is the file with all the computers to be checked, each on a seperate row in the document.
    [Parameter(Mandatory=$true)][string]$File,
    # -Output is if you want the result to end up in a file instead of the console, add desired file name.
    [Parameter(Mandatory=$false)][string]$Output
)

# Checks to see if output file already exists.
if($Output.Length -gt "0"){
$FileExist = Get-ChildItem $Output -ErrorAction SilentlyContinue
    If ($FileExist.Exists){
        Write-Host "File already exist, please remove it or use another file name..."
        exit 1
    }
}

# Set counter for iteration
$RowCount = 0

# Iterate through the list and present the result to the console or into a file.
foreach($Line in Get-Content $File) {
    if($Line -match $regex){
        $Status = Test-Connection $Line -Quiet
        if($Status -eq 'True'){
            if($Output.Length -gt "0"){
                Write-Output "$($Line): Online" | Out-File -FilePath $Output -Append
                $RowCount++
                Write-Output "$($RowCount) rows added to File"
            }
            else{
                Write-Output "$($Line): Online"
            }
        }
        else {
            if($Output.Length -gt "0"){
                Write-Output "$($Line): Offline" | Out-File -FilePath $Output -Append
                $RowCount++
                Write-Output "$($RowCount) rows added to File"
            }
            else{
                Write-Output "$($Line): Offline"
            }
        }
    }
}