tri_94 Posted January 18, 2019 Posted January 18, 2019 Hi there I would like a script to let me know when certain pc's are online. I've got the following script that will let me know if a pc is online or not. But ideally I would like this to run in the back ground and then possibly a popup to tell me that the pc is now online? Anyone able to help Thanks in advance Nick $ComputerName= "PCname" If (Test-Connection -ComputerName $computer -Count 1 -Quiet) { Write-Host "$computer is Online" } else { Write-Host "$computer is offline" }
howartp Posted January 18, 2019 Posted January 18, 2019 while ($true) { $ComputerName= "facebook" If (Test-Connection -ComputerName $ComputerName -Count 1 -Quiet) { Write-Host "$ComputerName is Online" } else { Write-Host "$ComputerName is offline" } Start-Sleep 10 } That will continue looping forever, and check every 10 seconds. while ($true) { $ComputerName= "facebook" If (Test-Connection -ComputerName $ComputerName -Count 1 -Quiet) { Write-Host "$ComputerName is Online" break } else { Write-Host "$ComputerName is offline" } Start-Sleep 10 } That will loop until it's online, then stop.
MartinByard Posted January 18, 2019 Posted January 18, 2019 Taking that one step further, this will loop until it's online and then pop up a message box saying when it came back online, then stop while ($true) { $ComputerName= "facebook" If (Test-Connection -ComputerName $ComputerName -Count 1 -Quiet) { $DT = get-date -format HH:mm Start-Job -ScriptBlock{[system.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms");[system.Windows.Forms.MessageBox]::Show("Computer $using:computername came back online at $using:DT", "Online Notification")} #Write-Host "$ComputerName is Online" break } else { #Write-Host "$ComputerName is offline" } Start-Sleep 10 } 1
tri_94 Posted January 21, 2019 Author Posted January 21, 2019 Thank you both very much that's perfect!
tri_94 Posted January 21, 2019 Author Posted January 21, 2019 Would it be possible to change it to be able to enter more then one pc ideally from a txt file. Thanks again
howartp Posted January 21, 2019 Posted January 21, 2019 (edited) $csv = Import-Csv n:\computers.csv $done = @() while ($true) { $csv | ForEach { $ComputerName = $_.PC if ($done.Contains($computername) -eq $false) { If (Test-Connection -ComputerName $ComputerName -Count 1 -Quiet) { $DT = Get-Date -Format HH:mm Start-Job -ScriptBlock{[system.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms");[system.Windows.Forms.MessageBox]::Show("Computer $using:computername came back online at $using:DT", "Online Notification")} $done += $ComputerName #Write-Host "$ComputerName is Online" #break } else { #Write-Host "$ComputerName is offline" } } } Start-Sleep 10 } CSV called computers.csv with a header line of "PC" eg: PC facebook off-netman standby-01 standby-02 Edited January 21, 2019 by howartp Missed initialising empty array 1
howartp Posted January 21, 2019 Posted January 21, 2019 (Just updated code if you were quick enough to grab first version)
tri_94 Posted January 21, 2019 Author Posted January 21, 2019 Great that seems to work fine me. Thanks again.
MatthewL Posted January 21, 2019 Posted January 21, 2019 I have a VBS on a PC that runs on start up and emails me when its logged on.
tri_94 Posted January 21, 2019 Author Posted January 21, 2019 I have a VBS on a PC that runs on start up and emails me when its logged on. Any chance of a copy of that please? Thanks
MatthewL Posted January 21, 2019 Posted January 21, 2019 VBS with this in, just enter relevant details and then place in startup folder: Set objMail = CreateObject("CDO.Message") Set objConf = CreateObject("CDO.Configuration") Set objFlds = objConf.Fields objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'cdoSendUsingPort objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "MAILSERVER" 'your smtp server domain or IP address goes here objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 'default port for email 'uncomment next three lines if you need to use SMTP Authorization objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "USERNAME" objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "PASSWORD" objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'cdoBasic objFlds.Update objMail.Configuration = objConf 'objMail.FromName = "e.g.server_name" objMail.From = "FROMADDRESS" objMail.To = "TOADDRESS" objMail.Subject = "Login Notification from my PC" objMail.TextBody = "PC has been logged in" objMail.Send Set objFlds = Nothing Set objConf = Nothing Set objMail = Nothing
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now