How to run script on interface state change in Windows?

I basically want to shutdown/start some services based on Network name and type I connect, so even better to run script when the network is automatically detected is it possible? Thanks.


There may be some tool out there, but you can roll your own with some work.

See this link about working with WMI events: http://trevorsullivan.net/2009/11/16/powershell-getting-started-with-wmi-events/

Basically you'd be writing a powershell script or c# program to listen for WMI events, which can be something like a dhcp lease being renewed or the connection coming online. But you'd have to do some more digging for the specific event you want if it's not a the dhcp lease event (which it sounds like it's not.) Then your script can do whatever you want when that event comes up - like starting services etc.

The link should provide you with some direction to keep looking in, or punt because it's kind of a pain.

You'd want to use some kinda of query like this in wbemtest to drill down and see which event you are interested in (like the article spells out). Then you'd do the necessary register-wmi event stuff in powershell once you're happy with your query.

SELECT * FROM __InstanceModificationEvent WITHIN 3 WHERE TargetInstance ISA 'Win32_NetworkAdapterConfiguration'

You can also use the Windows Task Scheduler for listening on the WMI event.