0

I have a powershell script that manually sets my dns. I need it to run whenever the network interface comes up or changes.

For example, when I go on and off the docking station (that has wired internet), I need the script to fire.

It needs to run as administrator.

What is the path to do this? (a job that runs every couple of minutes seems.... not elegant?)

Dead Ends Found So Far

It has been suggested to follow the steps here, How to Launch Command on Network Connection. This does not solve for the following reason:

Looking at Windows 10, 20H2 (what is running here), I see that:

Event ID: 10000

Is logged three times when the system boots up.

I don't want my script to run three times (certainly not on top of each other, which it appears would happen), AND for my script it is important that DHCP has come up and the route table has settled down.

My script needs to run whenever networking comes up or there is a change in the Active network interface (or the number of interfaces that are Up).

How to achieve that?

Current Solve Direction

Having a scheduled task hang from an event looks like a great solve direction, however so far I cannot identify a clean event to hang the event on. (I have dug through a ton of the event log, without finding any clean events to use. (Not even much that is unclean.) Hints welcome!)

  • 2
    If you know the event code you are looking for, then you can create a task (in Task Scheduler) that launches when a specific event is added to the event log. Maybe pull up the Event Viewer to see which codes are generated (if any) when you dock/undock. Edit: here is a link that might help, https://superuser.com/questions/262799/how-to-launch-a-command-on-network-connection-disconnection – JG7 Feb 15 '21 at 23:22
  • 1
    Kindly check if methods in this similar thread was helpful: [How can I run a script when my network connection changes?](https://serverfault.com/questions/26056/how-can-i-run-a-script-when-my-network-connection-changes) – Sunny Feb 16 '21 at 08:46
  • 1
    @DrinkMorePimpJuiceIT Thanks, but not a solve. OP enhanced to address this angle. – Jonesome Reinstate Monica Feb 16 '21 at 19:04
  • @Sunny Thanks for the tip! I read that thread. It is not a solve. (It is ancient, it is all over the map, and the event log path -- the best looking path -- is not viable. OP enhanced. – Jonesome Reinstate Monica Feb 16 '21 at 19:06
  • @JG7 Thank you for the tip! I have dug through a TON of the event log (though not all, by any means) and cannot find, so far, a useful event to monitor. (I love the idea of hanging a scheduled task off an event!). I read the thread you suggested. Sadly, it is not a fit. OP enhanced to explain why not a fit. Thank you! – Jonesome Reinstate Monica Feb 16 '21 at 19:07

1 Answers1

-1

If you only need to run it when you connect or disconnect from the doc. You can use the event: System > Kernel-Power > 105.

This will run when your power is source changed.