How to launch a command on network connection/disconnection?

59

34

I have a wifi connection that requires to authenticate using a web form once the wireless link is established. I know how to automate the authentication with a script that uses curl/curlIE.

But how can I ask Windows to call my script every time I connect to a particular network connection?

I would be also interested in receiving the name of the wireless profile or the ESSID on the command-line of my script.

dolmen

Posted 2011-03-26T17:23:36.297

Reputation: 1 075

Did you ever get an answer to this that satisfied your question? – Menasheh – 2016-11-10T23:30:36.067

1

Probably useless but I found this through googling. It apparently enumerates your connections and might provide something you can query with a script to determine when an adapter connects/disconnects. http://msdn.microsoft.com/en-us/library/ms739931%28v=VS.85%29.aspx

– CreeDorofl – 2011-03-26T19:26:16.383

I don't want to have to check the state every few seconds. Network Location Awareness notifications is interesting, but I see no easy way to use it with only scripting.

– dolmen – 2011-03-26T20:23:59.347

dolmen - is there a way you can post the script you wrote for the authentication too. this question would be a lot more complete with it in it. – quest49 – 2012-05-04T20:19:13.383

@quest49 I understand that my script may interest you, but it is irrelevant to the question. – dolmen – 2012-10-24T19:54:44.847

Answers

80

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:

  1. Open the Task Scheduler. You can find it by typing Task Scheduler into the start menu search box, or under Programs | Accessories | System Tools.
  2. In the Task Scheduler library, create a new task by clicking Create Task in the Actions panel on the right side.

    add task

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

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

    dropdown trigger

  5. Choose the following settings:

    • Log: Microsoft-Windows-NetworkProfile/Operational
    • Source: NetworkProfile
    • Event ID: 10000
  6. Click OK, then go to the Conditions tab.
  7. Check the box for Start only if the following network connection is available and choose the network you want to run the script with
  8. 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.
  9. Set any other task settings you want to, then click OK.

Setting an event for when you disconnect from the network:

  1. Follow steps 2-4 above
  2. Use the following event trigger settings:
    • Log: Microsoft-Windows-NetworkProfile/Operational
    • Source: NetworkProfile
    • Event ID: 10001
  3. 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.
  4. Follow steps 8-9 again

nhinkle

Posted 2011-03-26T17:23:36.297

Reputation: 35 057

1For the record, I had to specify as "source" Microsoft-Windows-NetworkProfile (W10 1709) – Joril – 2018-03-21T12:33:33.833

Thanks. What about XP? – Hrvoje T – 2018-12-03T12:16:30.570

1Great! 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. – dolmen – 2011-03-26T22:25:55.980

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 Millerchip – 2013-07-10T06:00:09.720

1@AdamMillerchip I just opened the event viewer, disconnected/reconnected, and then looked around. – nhinkle – 2013-07-10T06:47:52.077

Yeah, looks like that's the way it needs to be done. Thanks. :-) – Adam Millerchip – 2013-07-10T10:07:16.300

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? – einpoklum – 2013-10-14T13:07:42.777

1Strange, I tried this and nothing.... – Menios – 2014-01-22T12:01:08.567

@maythesource.com what did you actually try? – nhinkle – 2014-01-22T19:17:37.497

@nhinkle♦ Tried this: http://superuser.com/questions/705305/cannot-launch-application-on-wifi-connection . Thanks, KR !

– Menios – 2014-01-22T19:24:14.027

12

It seems that Start only if the following network connection is available is broken after Windows 10 anniversary update. Use this custom trigger instead:

<QueryList>
  <Query Id="0" Path="System">
    <Select Path="Microsoft-Windows-NetworkProfile/Operational">
     *[System[(EventID=10000)]] and *[EventData[(Data[@Name="Name"]="YOUR-SSID-HERE")]]
    </Select>
  </Query>
</QueryList>

In corporate networks use the name of the domain instead of the SSID. In this case the category of the event will be "Domain Authenticated" and not "Private".

krrr

Posted 2011-03-26T17:23:36.297

Reputation: 221

Start only if the following network connection is available seems broken indeed. If I set that option and run the task on demand I get this error: Task Scheduler Service is not available. Task scheduler will attempt to reconnect to it. – Federico Destefanis – 2018-11-21T09:01:39.230

Is this bug still exists in Windows 10 1803 or later? – krrr – 2019-11-05T03:14:38.467

3

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.

Tankman六四

Posted 2011-03-26T17:23:36.297

Reputation: 349