Windows batch script help

hello to all the people of tek syndicate. long time no see :).

i have a little problem and i was wondering if someone could help.
i have a home server running windows, a machine build up from left over parts. it works quite stable and with an uptime of about few months now i did not have any problems.
except for the tunnel im using..

to connect all my computers im using logmein hamachi 2 tunneling program. it works well and i dont have any problems with it. but from time to time, it shuts down. i dont know why, i cant fond any solid reason.
then, i need to remote over network to the server and restart the logmein hamachi process and it works fine again.

the problem is if the tunnel goes down when im away from the country, i cant restart it and i cant remote to the server. so i was digging around and found a partial solution.
a batch file that can restart any process.. but that is also a manual solution.

so, i would like to make a batch file that loads at startup and would do that automatically.
a script that checks ipconfig status, and when there is no ip for the tunnel (windows sees hamachi as a hardware network card) the file would restart the hamachi service with net stop and net start commands.

something like this
0. windows loads .bat file at startup
1. .bat file waits 300s
2. .bat file checks ipconfig of the machine, looking only for the ip address of the hamachi tunnel (i dont know how to do this)
3.1 if everything is ok, goto 1.
3.1 else no ip for the hamachi network interface, restart service -> net stop hamachi service, wait 20s, net start hamachi service, goto 1.

i would be very grateful if someone could help me with this problem, and i would be haply that i learned a little windows batch file scripting :).

What version and SP of windows are you using? This will probably be simpler to do in PowerShell else you might find you need to script to look for regular expessions.

In PowerShell 3.0 we can use something like the following command to gather up all the network adaptors and identify which ones have a connection:

get-ciminstance cim_networkadaptor | select netconnect, name, netconnectionstatus

A netconnectionstatus of 2 means connected, so we should be able to then start an IFstatement if that comes back empty...

Unfortunately I am not a PowerShell expert by any means, I usually just google for code snippets and use the help command to piece together what I need.

If I get a spare half hour tomorrow I'll see if I can come up with anything.

In task scheduler you can trigger a script as often as every 5 mins. So you dont need a looping batch file. In not sure on the actual script but.

I don't know if its possible in the version of windows you're running but you should be able to everyhting directly in task scheduler. Task scheduler can start services/programs/scripts on startup and even restart them if they crash/stop/take a break.

Ok, I haven't had much time to look into this but the following PowerShell command works perfectly on Windows 8.1 for any physical network adapters that are disconnected.

Get-NetAdpater | where status -ne 'Up' | Restart-NetAdapter

And that's it. If there is a network adapter that is not connected it will get restarted. Just put the command into a .PS1 file and schedule through task scheduler with highest privileges and remember that to call a .PS1 file you specify PowerShell as the program under the Actions tab and precede it with a .\ e.g.

If you are on an older version of Windows or that does not work for you (perhaps your adapter shows as connected but without an IP) then the following blogpost has a scripted solution for exactly your kind of problem and should work on Vista/Win7:

Stupid Windows Tricks: Restart network adapter when it hangs