68

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
  • 1,312

6 Answers6

91

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
einpoklum
  • 10,666
nhinkle
  • 37,661
18

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
  • 280
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.

0

To update the answers from @nhinkle and @KRR: I had to use the following XML Query as Windows will sometimes set the "Name" field something like "SSID_5G 2" for the SSID "SSID_5G". Using "Description" matches instead (Windows 10, Version 20H2)

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

Some people in the comments wanted the script version of making a scheduled task that runs a command on network connection/disconnection. So I wrote one here in Powershell.
To make PowerShell close its window after finishing instructions, remove -NoExit argument from below script.
For network disconnections You can use event ID 10001 instead of 10000 as mentioned in other answers.
To make this task to be run after connecting to network with specified name (SSID), then below this line in eventQuery
*[System[(EventID=10000)]]
add these 2 lines
and
*[EventData[(Data[@Name="Name"]="Your_network_SSID_name_here")]]

[string] $userName = [Environment]::UserName;
[string] $taskName = "NetworkConnectionTask";
[string] $taskDescription = "Task which is run whenever connection to network is established";
[string] $taskExecutable = "powershell.exe";
[string] $taskArguments = "-NoExit -NoLogo -ExecutionPolicy Bypass ""Write-Output 'Network Connected' "" ";
$taskAction = New-ScheduledTaskAction -Execute $taskExecutable -Argument $taskArguments;
[bool] $runAsAdmin = $true;
[string] $taskLevel = '';

if($runAsAdmin -eq $true) { $taskLevel = "Highest"; } else { $taskLevel = "Limited"; }

[string] $eventQuery = @" <QueryList> <Query Id='0' Path='System'> <Select Path='Microsoft-Windows-NetworkProfile/Operational'> *[System[(EventID=10000)]] </Select> </Query> </QueryList> "@;

important instruction to extract Event Trigger Class

$CIMTriggerClass = Get-CimClass -ClassName MSFT_TaskEventTrigger -Namespace Root/Microsoft/Windows/TaskScheduler

$eventTrigger = New-CimInstance -CimClass $CIMTriggerClass -ClientOnly $eventTrigger.Enabled = $True; $eventTrigger.Subscription = $eventQuery;

$taskSettings = New-ScheduledTaskSettingsSet -RunOnlyIfNetworkAvailable -MultipleInstances IgnoreNew -RestartCount 3 -RestartInterval (New-Timespan -Minutes 1);

Register-ScheduledTask -Action $taskAction -Trigger $eventTrigger -TaskName $taskName -Description $taskDescription -User $userName -RunLevel $taskLevel -Settings $taskSettings -Force

I also noticed that events with IDs 55 and 56 can also be used for network connections/disconnections. These events have 2 states true and false respectively, which can be used in queries in event triggers.
55 - Is the Internet available: true
56 - Is a free network available: false
Here is an example query for 55 event trigger with true state meaning connection established.

<QueryList>
  <Query Id='0' Path='Microsoft-Windows-UniversalTelemetryClient/Operational'>
    <Select Path='Microsoft-Windows-UniversalTelemetryClient/Operational'>
    *[System[Provider[@Name='Microsoft-Windows-UniversalTelemetryClient'] and EventID=55]]
    and
    *[EventData[Data[@Name='State']='true']]
    </Select>
  </Query>
</QueryList>
0

I had no success with Microsoft-Windows-NetworkProfile/Operational because for some reason Windows 11 didn't resolve the name or resolved it incorrectly. This worked for me as an custom event trigger with XPath/XML format:

<QueryList>
  <Query Id="0" Path="System">
    <Select Path="Microsoft-Windows-WLAN-AutoConfig/Operational">
 *[System[(EventID=8001)]] and *[EventData[(Data[@Name="SSID"]="YOUR_SSID_NAME_HERE")]]
  </Select>
 </Query>
</QueryList>