Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Would this be for Windows then? If so, which version? –  squillman Jun 15 '09 at 18:25
    
Ah, yes, WindowsXP. Other flavors are welcome, too, but we're predominately winxp for now. –  Michael Haren Jun 15 '09 at 18:26
    
Not that it helps, but Windows 7 has some nice features for better network location profiling. –  MathewC Jun 15 '09 at 18:45
    
Please post references--that'll help me eventually and perhaps others right now. –  Michael Haren Jun 15 '09 at 18:46

6 Answers 6

up vote 3 down vote accepted

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.

share|improve this answer
    
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 6:27

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

share|improve this answer
    
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

I've had some success with Net Profiles before.

share|improve this answer
    
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

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.

share|improve this answer
    
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

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.

share|improve this answer

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 "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 "Suspend" You can use Log "Microsoft-Windows-Kernel-Power/Thermal-Operational", Source "Kernel-Power" and the Event ID is 42.
For "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 "Startup" and "Shutdown" events under "Computer Configuration" folder in the same window. The Startup script runs under system account before the user logs on.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.