Share via


Powershell script to detect when PC is turned on & Connected to domain

Question

Wednesday, April 9, 2014 3:32 PM

Hi

Wonder if anyone can advise.

I would like to run a powershell script (Say every 30 mins) that will check if a Windows PC (Notebook) is connected to the domain and email me an alert with the PC name stating that it is connected. I will take the PC names to monitor from a CSV file so I can easily update the list as required.  It should not email me if the PC is not connected.

The notebooks are typically used standalone by community workers so can go for several weeks without being connected.  I would like to use this to capture a connection so I can facilitate updates etc.

Any help on how to write the PS Script appreciated.

Will

 

All replies (5)

Wednesday, April 9, 2014 3:52 PM âś…Answered

Hi Will,

Here's a starter for you (this assumes your CSV header is ComputerName):

Import-Csv .\computerList.csv | ForEach {

    If (Test-Connection $_.ComputerName -Count 1 -Quiet) {

        $body = "Laptop detected - $($_.ComputerName)"
        Send-MailMessage -To [email protected] -From [email protected] -Subject 'Laptop detected' -Body $body -SmtpServer smtp.domain.com

    }

}

Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)


Wednesday, April 9, 2014 4:01 PM

Hi Mike

Thank you for your fast response. Brilliant, Greatly appreciated !

Will


Wednesday, April 9, 2014 4:36 PM

Cheers, you're very welcome.

Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)


Wednesday, April 9, 2014 4:48 PM

Just to be clear, it doesn't actually check for connection to a domain, it sends an ICMP packet (aka "ping") and waits for a response. While this script likely does what you need it to do, it's not actually testing if a computer is "connected to a domain"

[email protected]


Thursday, April 10, 2014 8:57 AM

zarberg

Thank you for the clarification.

Yes it does meet with my requirements as I basically need to know when the computer is on site.