4

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.

amsys
  • 55
  • 1
  • 6

4 Answers4

4

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

02strich
  • 61
  • 5
  • You are the best, don't know to which answer to put now the green check :x – amsys Sep 30 '10 at 07:46
  • +1 - Didn't know that trick. Looks a little tricky to configure but with enough playing looks like it would be pretty cool. – Matt Sep 30 '10 at 16:01
2

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'
Matt
  • 1,903
  • 13
  • 12
  • Thank you very much for such an brief answer, down there is also additional answer how to make it even simpler with Windows Task Scheduler which I was looking for. – amsys Sep 30 '10 at 07:46
0

That sounds like the problem that the Network Location Awareness service tries to solve. I bet PowerShell could be employed to use whatever .NET plumbing exists to listen for NLA service events.

AndyN
  • 1,769
  • 12
  • 14
0

You could set up a permanent WMI event consumer, using either the CommandLineEventConsumer or ActiveScriptEventConsumer.

http://msdn.microsoft.com/en-us/library/aa384749(v=VS.85).aspx - Active Script Event Consumer

http://msdn.microsoft.com/en-us/library/aa393249(v=VS.85).aspx - Command Line Event Consumer

Cheers

Trevor Sullivan
  • 2,063
  • 3
  • 14
  • 19