# check-pxeservers.ps1 # # Monitors the netstat table to see if pxe server communications are broken # and if so, restarts the service on the target server. # # NOTE: This script should be run on the PXE Manager Server! # # Author: Robbie Foust (rfoust@duke.edu) # Last Modified: November 15, 2007 # # Based on a vbscript by Eric Olsen # (http://juice.altiris.com/download/2671/pxe-monitor) # # separate your pxe server IP addresses with commas $pxeServers = "192.168.0.5","192.168.0.10" # quick function to convert netstat output into a psobject function get-netstat { $null,$title,$null,$header,$data = netstat -an $data | % { $protocol,$local_ip,$local_port,$foreign_ip,$foreign_port,$state = [regex]::replace($_.trimstart(" "),"\s{2,}|[:]",",").split(",") new-object psobject | add-member -pass -mem NoteProperty Protocol $protocol | add-member -pass -mem NoteProperty "Local IP" $local_ip | add-member -pass -mem NoteProperty "Local Port" $local_port | add-member -pass -mem NoteProperty "Foreign IP" $foreign_ip | add-member -pass -mem NoteProperty "Foreign Port" $foreign_port | add-member -pass -mem NoteProperty State $state } } while ($true) { $activePxe = @() # Run netstat to get a list of all active connections and # create a list of IP addresses that are talking to us on port 406 get-netstat | % { if ($_."Local Port" -eq 406) { $activePxe += $_."Foreign IP" } } # look for the absense of pxe server IPs, and restart their services $pxeServers | % { if ($activePxe -notcontains $_) { write-host "No active connection from $_, checking service..." $svc = gwmi -query "select * from win32_service where name = 'Altiris PXE Config Helper'" -computer $_ if ($svc.state -eq "Running") { write-host "Service is currently running, attempting restart..." if ($svc.stopservice().ReturnValue -ne 0) { write-warning "Unable to stop PXE service!" } start-sleep 10 } else { write-host "Service is down, attempting to start it..." } if ($svc.startservice().ReturnValue -ne 0) { write-warning "Unable to start PXE services!" } else { write-host "Service started successfully." } start-sleep 5 } start-sleep 5 } }