11

I'd like to create a script to do various things when the network connection changes. For example, if the machine connects to my office network I might desire it to:

  • mute itself
  • change its DNS
  • change its proxy settings
  • change default printer

Then, when the machine is off the office network it might revert some of those.

Presumably the "active network" would be based on IP address/gateway range or WIFI network name, etc.

Do you have any recommended tools for accomplishing this? I can see something like this being applicable to many IT pros so I'm sure there are some good tips out there.

Note that this is desired to function when the network changes--not just when the user logs on/off.

Michael Haren
  • 1,301
  • 7
  • 18
  • 31

6 Answers6

11

You can do it using Task Scheduler:

  1. Right-click on the "Task Scheduler Library"
  2. Click "Create Task"
  3. For "Name:" field write Your new task's name.
  4. Check that the "Security options" area has checkboxes where You want them to be.
  5. Under "Triggers" tab click "New..."
  6. Select "Begin the task:" => "On an event"
  7. Select "Log:" => "Microsoft-Windows-NetworkProfile/Operational"
  8. Select "Source:" => "NetworkProfile"
  9. Write "Event ID:" => 10000
  10. OK this dialog.
  11. Under "Actions" tab click "New..."
  12. Select "Action:" => "Start a program"
  13. For "Program/script:" browse to Your favorite script file.
  14. OK this dialog.
  15. Setup the "Conditions" tab regarding "Idle", "AC power" etc conditions to Your liking.
  16. Also do setup under "Settings" tab.
  17. OK, all done here.
  18. Test the new event.

Just in case, there are also events for computer "Suspend" and "Resume" available there. I, for example, use them to stop and restart Hamachi on these events, since otherwise it seems to have problems on my machine.
For computer "Suspend" You can use Log "Microsoft-Windows-Kernel-Power/Thermal-Operational", Source "Kernel-Power" and the Event ID is 42.
For computer "Resume" You can use Log "System", Source "Power-Troubleshooter" and the Event ID is 1.

For user login/logoff, I do not know events under Task Scheduler (but I believe they are available there too), but You can configure it instead here:

  1. Launch gpedit.msc
  2. Open "User Configuration"
  3. Open "Windows Settings"
  4. Click "Scripts (Logon/Logoff)"
  5. In the right pane You can assign scripts for "Logon" and "Logoff" events. Note, if You assign multiple scripts then do not assume that they are run synchronously. They may not. For this case use single script instead, that calls other scripts in sequence. I haven't checked under which account these scripts run - is it user or system?

You can do similar stuff for computer "Startup" and "Shutdown" events under "Computer Configuration" folder in the same window. The Startup script runs under System account before the user logs on.

Roland Pihlakas
  • 271
  • 3
  • 7
  • 1
    This is "The Answer". you supply the hidden part: "The trigger". The rest is a matter of programming. Thanks – Saic Siquot Mar 20 '15 at 15:38
  • Note, on Win10, that event 10000 fires three times when the OS comes up, and it appears it fires before TCPIP and routing tables are settled (not certain on this, but that is what it looks like). This last aspect means this approach does not fly for my use case. – Jonesome Reinstate Monica Feb 16 '21 at 19:25
2

The "Network Location Awareness" service (http://msdn.microsoft.com/en-us/library/ms739931(VS.85).aspx) in Windows XP (and 2000? I don't recall...) and up will enable this functionality, but I haven't found where anybody has written an application to take advantage of it. I'd love to code something myself, but I don't have enough spare cycles to even begin to think about it.

This wouldn't be a simple little VBScript thing, but it wouldn't be that much coding either. Maybe somebody could pick up the idea and run with it. There's even sample code at http://www.microsoft.com/downloads/details.aspx?familyid=ef8a6228-f11d-4ba0-b73e-dd8dc7dd11e8&displaylang=en.

There have been numerous times I've wanted this functionality, and I'd think there are more than a few people who would like to see it.

Evan Anderson
  • 141,881
  • 20
  • 196
  • 331
  • I, sir, will heed your call...eventually. I might dabble in this very soon as my long overdue contribution to the open source community. – Michael Haren Jun 15 '09 at 18:33
  • Seriously, look me up if you're going to code something. If you'll license your code with GPL, MPL, or some other reasonably open license (and the code actually works) I'll throw some money your way. – Evan Anderson Jun 15 '09 at 19:10
  • I don't know if it's improved any in recent years (Post XP SP1 at any rate) but I burned a lot of time some years ago trying to build code that used NLA and I came to the conclusion that NLA was pretty much useless because it could take minutes to fire an event following a change (network connect\disconnect\whatever). If I recall correctly the IPHelper API's NotifyRouteChange was much faster (fractions of a second) but obviously more limited in what would trigger it. – Helvick Jun 30 '09 at 06:27
1

I've had some success with Net Profiles before.

MattB
  • 11,194
  • 1
  • 30
  • 36
  • This seems to work quite well. I need to figure out how to do individual things like mute the machine but it looks like I could tie a cmdlet into this pretty easily. It switches profiles based on WIFI name which is enough for me. Thanks! – Michael Haren Jun 15 '09 at 20:29
1

Using .net framework version 2 it is possible to use the System.Net.NetworkInformation namespace. From this you can determine:

network availability: NetworkAvailabilityChangedEventHandler() network address changes: NetworkAddressChangedEventHandler() ipaddress: IPAddressInformation

and so on.

How much of this is available through powershell I don't know because I don't use it. But I have written a very simple vb.net tray application that just monitors network availability and connects to a network share when the network is available. If you're interested in the bare-bones of the code then let me know.

RobS
  • 136
  • 4
  • I'd be interested by the vb.net tray application you developed. Could u please share the source code with the community ? Thx – crousti Oct 29 '10 at 10:16
1

This is kind of a sideways and partial answer (and not even documented because I can't seem to find the original article), but I'm fairly sure Windows 7 has a property sheet on the printer object allowing you to change defaults depending on subnet.

I'd love to see features addressing the rest of your list, as well.

Kara Marfia
  • 7,892
  • 5
  • 33
  • 57
0

You could create a batch file that reads the results of an ipconfig command, and have it run every 5 seconds or so...

sangretu
  • 372
  • 1
  • 2
  • 8
  • Eww... ick. There is already a service in the OS to handle this. Somebody just needs to write the code. I'd be more than willing to contribute some money to development of an open source utility to utilize this functionality. – Evan Anderson Jun 15 '09 at 18:31
  • While I appreciate the gets-things-done mentality behind this approach, I'll hold out for something a little more elegant – Michael Haren Jun 15 '09 at 18:34