Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.


ok the idea is to run a script (pref either powershell or vbs) on the event that there is a network failure (e.g. when a network cable gets unplugged for whatever reason).

This possible? im going to guess so but how?
cheers

share|improve this question

3 Answers 3

up vote 0 down vote accepted

You can do this from a .net application, as there are a couple of hooks that can be used to trigger it when the network cable is unplugged, in particular Shell Notifications, which in theory could be accessed via Powershell, as its .net based. However the problem is that your application/script would need to be running when the event is trigged, to catch the event and do something.

I guess what your wanting is just to have a script called when the network cable is removed without anything else running in the background. I don't think this will be possible without have some application, even if its just a stub service that will then run your script, waiting for the event to happen.

That said, someone might have an ingenious solution I hadn't thought of.

share|improve this answer
    
thanks yeah ill just create a service which will hook in. –  Aceth Nov 12 '09 at 11:40

You can create a task in the task scheduler that will be triggered whenever a particular event is logged. If you can figure out what event ID is logged when the network connection is lost, you can have that task run your script.

share|improve this answer

Okay, there's a simple Powershell code to start with. Note you can use DNS name instead of IP Address. You can change {Write "Working"} block for {} (do nothing), but it'll ping too much times a second. So use Sleep commandlet inside the block. Replace {write "Failure!"} scriptblock for the actions you need to do in case of failure. You can extend that script to ping the array of addresses sequentially, or you can set some counter to throw your action not for the first lost ping, but after a bunch of them are lost. You can even use some case statement to take different actions for different failed hosts. Ask if you need some further assistance in scripting.

while ((get-WmiObject Win32_PingStatus -filter "Address='192.168.1.1' ").Statuscode -eq 0) {Write "Working"; Sleep 2};{write "Failure!"}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.