Probe for online computers

Sometimes you need to be alerted to when a computer comes online.  This does that with a popup.  Able to watch many computers at once, using jobs and pipeline.

I call this Alert-ComputerIsOnline.PS1

[Powershell code][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") > $null

function Find-OnlineComputer-WithJobs {
# this processes a pipeline full of computers
[CmdletBinding()]
Param(
    [Parameter (mandatory=$True, ValueFromPipeline = $true)]
    [String]$ComputerName,

    [Parameter (mandatory=$False, ValueFromPipeline = $False)]    
    [Switch]$NoPopup
)
    Begin{
        # remove any orphaned jobs from previous run
        Get-Job -Name "FindOnline*" | Stop-Job -PassThru | Remove-Job 
        $TestRate = 6 # seconds between probe of the job status (fast at first)
    }
    Process {
        # process ONE COMPUTER
        # start a job - Enclose in [scriptblock]::Create() to force pre-evaluation of $computername
        Start-Job        -Name  "FindOnline_$ComputerName" `
                  -ScriptBlock  ([scriptblock]::Create(" `
                   
                                    [CustomPSObject]
                                    while (!(Test-Connection -ComputerName $ComputerName)) {start-sleep 60*5}
                                    '$ComputerName' # from remote machine: return the results
                                
                                ")) > $null           # suppress any output to the local machine
        }

    End{
            # for each ID, if completed, retrieve contents and delete the job
            # sleep
        Write-Verbose -Verbose ("Created " + (Get-Job -Name "FindOnline*").Count + " jobs to test for connectivity")
        Write-Verbose -Verbose "Waiting for jobs to end"
        while ((Get-Job -Name "FindOnline*").Count -gt 0) { 
            (Get-Job -Name "FindOnline*") `
                | Foreach {
                    $MyOutput = (get-date -UFormat "%m/%d/%y %H:%M:%S") + (" [" + ($_.name -replace "^FindOnline_","") + "]")
                    #Write-Verbose ("[" + ($_.name -replace "^FindOnline_","") + "] ")
                    #Write-Output ($_.completed)
                    if ($_.state -eq 'Completed') {
                        Write-Verbose -Verbose ($MyOutput + " is online")
                        if (!$NoPopup) {
                            [System.Windows.MessageBox]::Show( (new-object System.Windows.Window -Property @{TopMost = $True}), `
                                "Computer is online`n`n" + (Receive-job $_),"Search is over",0,64) > $null
                        }
                        Remove-Job $_
                    }
                }
            if ((Get-Job -Name "FindOnline*").Count -gt 0) {
                (Get-Job -Name "FindOnline*") `
                | Foreach {
                    $MyOutput = (get-date -UFormat "%m/%d/%y %H:%M:%S") + (" [" + ($_.name -replace "^FindOnline_","") + "]")
                                Write-Verbose -Verbose ($MyOutput + " ... Still waiting")
                          }

                Write-Output "Sleeping $TestRate"
                Start-Sleep -Seconds $TestRate
                $TestRate = 10
            } else {
                Write-Verbose -Verbose "Processing ends"
            }
        }
    }
}

@(`
"srv3",`
"junk",`
"media3") | Find-OnlineComputer-WithJobs -nopopup

Leave a comment