2 Answers
2

In Windows Vista and later, you can do this using a scheduled task with an event log trigger. The first event will be triggered by connecting to the network, and you will specify which network you must be connected to for it to run. The second event will be triggered when disconnecting from any network. Each event will run a specific task that you specify; likely the scripts you mentioned having written.

Setting an event for when you connect to the network:

Open the Task Scheduler. You can find it by typing Task Scheduler into the start menu search box, or under Programs | Accessories | System Tools.

In the Task Scheduler library, create a new task by clicking Create Task in the Actions panel on the right side.

Give the task a name like "detect network connect" or whatever you choose

On the Triggers tab, click New... and select On an Event from the dropdown box.

Choose the following settings:

Log: Microsoft-Windows-NetworkProfile/Operational

Source: NetworkProfile

Event ID: 10000

Click OK, then go to the Conditions tab.

Check the box for Start only if the following network connection is available and choose the network you want to run the script with

Under the Actions tab, click New... and select Start a program. Enter the location of the script file you want to run, then click OK.

Set any other task settings you want to, then click OK.

Setting an event for when you disconnect from the network:

Follow steps 2-4 above

Use the following event trigger settings:

Log: Microsoft-Windows-NetworkProfile/Operational

Source: NetworkProfile

Event ID: 10001

Skip steps 6-7, as you will no longer be attached to any network at all. This event will therefore run any time you disconnect from any network.

Great! Thank you for that detailled answer! This is much more than I expected. I found out I can even use a condition on the network connection name, so I can run the command only for a particular WLAN profile.
–
dolmenMar 26 '11 at 22:25

Where did you get the values from that you provided in step 5? It's possible to work them out via trial and error in the event log, but I'm wondering if there a better way to identify a specific event, via some documentation for example.
–
Adam MillerchipJul 10 '13 at 6:00

@AdamMillerchip I just opened the event viewer, disconnected/reconnected, and then looked around.
–
nhinkle♦Jul 10 '13 at 6:47

Yeah, looks like that's the way it needs to be done. Thanks. :-)
–
Adam MillerchipJul 10 '13 at 10:07

What parameters will my script get? Can you give an example of such a script which, say, brings up a message box with its parameters? Or appends them to some file?
–
einpoklumOct 14 '13 at 13:07

If you happen to be using a Thinkpad, you can take advantage of ThinkVantage Access Connection, available for Windows 7, Vista and XP.

ThinkVantage Access Connection is the network connection manager software shipped with ThinkPad - if not, downloadable from their support site. When you make a new profile for your location, there is a wizard which asks if you want to configure a list of program to start when that location is connected. It offers to kill the program when the net is out, too.

Note that the software can be configured to work without any location profile at all, and that's perhaps the initial status - in that case, there is no way to configure a list of program to start when wifi is connnected: you have to create a connection profile to contain the list.

For my own experience, I try to avoid using the software - it takes a lot more time to get connected than without it, and sometimes fails to establish new connection when booting (can be fixed by manually reconnect).

The software only works with the WIFI driver provided with the thinkpad.

On a side note, this software offers to establish connection before user logs in, as an option when you create the profile. I don't know if this is possible without it.