Right, so yesterday I posted about this batch file I created to set a laptop to sleep or do nothing depending on whether it’s out of office or in office (which the batch file determines by pinging servers in each network).
What I want to do now is automate the running of this batch file. I don’t want to have the user clicking on this file. Since the file only needs to run each time a change in the network state happens ideally something should just trigger it to run when the state changes.
Use Task Scheduler
I know the Windows Task Scheduler is quite powerful and can trigger tasks on Event Log entries. So if I can find the Event Log entries generated when I connect and disconnect the network, am good to go! I checked the Event Logs on the user laptop and sure enough there were entries. For the wired port they seemed to be tied to the card driver and generated by the driver, but for the wireless port they were generated by Windows itself. Here’s what I found:
- Event ID 33 is generated in the System log from source “e1dexpress” whenever the LAN port is connected to a network.
- Event ID 27 is generated in the System log from source “e1dexpress” whenever the LAN port is disconnected to a network.
- Event ID 8001 is generated in the Applications and Services > Microsoft > Windows > WLAN-AutoConfig > Operational log from source “WLAN-AutoConfig” whenever the WiFi is connected to a network.
- Event ID 8003 is generated in the Applications and Services > Microsoft > Windows > WLAN-AutoConfig > Operational log from source “WLAN-AutoConfig” whenever the WiFi is disconnected from a network.
Based on this I can create a new scheduled task entry to run my batch file and add triggers for the Event Log entries above. Here’s an example:
And here’s the final list with all the triggers:
Notice I added a trigger to run when the user logs in too. Added this for situations where the user may dock the laptop while it’s powered off and then login. In such I wasn’t sure whether the network connect event log would be generated or not and whether it would be generated before the Task Scheduler runs – was too lazy to test really, so I added an entry to run when the user logs in.
That’s it really! Once such a task is created it runs the batch file as expected.
I did two additional things after creating the task:
- By default tasks are set to run only if the computer is on AC power. Since we want it to run when on battery too, be sure to untick that.
- Right click the task and export it. This way I can easily import it on other similar laptops. For dis-similar laptops (where the network card model may be different) I’ll have to import and add additional triggers. Still, better than recreating the task from scratch, and I think what I’ll do is add triggers for such models too to the task I created so eventually I’ll have a task that can handle all models in our firm. This way I can import it to any machine without worrying about the model differences.
Importing task on a remote machine
Here’s what I for users who want this on their laptop: I copy the batch file and list of IPs to a folder on the user machine; then I launch Task Scheduler on my machine, connect to the Task Scheduler on user machine, import the task I made above. I don’t even have to visit the user machine, how cool is that!
Importing task on a local machine
If I am at a user machine and want to set them up with the batch file, I don’t want to bother with copying the files and importing a task. I want to make that an easy step too so I created a batch file that copies over the two files and imports the task and runs it once! Just double click the install batch file in such cases and it will do the rest.
copy "L:\Rakhesh\Laptop lid\closelid.bat" C:\
copy "L:\Rakhesh\Laptop lid\officepoints.txt" C:\
schtasks /Create /XML "L:\Rakhesh\Laptop lid\Laptop Lid close.xml" /TN "Laptop Lid closing"
schtasks /Run /TN "Laptop Lid closing"
Quite obvious what’s happening. The trick here is to use the
schtasks command which is a command line interface to manage Scheduled Tasks. Using
schtasks I can import the task and run it.
I can use
schtasks with a remote computer too so one of these days I’ll probably create another batch file to handle remote additions too.
That’s all for now!
Update: See this post for an updated version of the batch file as well as the XML file I use to import the task.