Sunday 8 February 2009

Automating VPN connections with Batch Files

If you frequently need to connect to your office from home or away via a VPN, I have listed below a simple batch file that utilises rasdial.exe to make the connection and disconnection with one click. It also uses mstsc.exe to connect to the server using Remote Desktop (Terminal Services client). The error handling makes sure that you are aware of any errors but otherwise it is completely automatic. After finishing the Terninal Services session the VPN is automatically disconnected.

All these programs and utilities are standard in Microsoft XP Professional.

@echo off
rasdial YourVPN mike secretpassword
if NOT %ERRORLEVEL% ==0 goto failed
mstsc OurServer.rdp
rasdial YourVPN /DISCONNECT
if NOT %ERRORLEVEL% ==0 goto failed2
exit

:failed
echo.
echo Failed to connect to VPN!
pause
exit

:failed
echo.
echo Failed to disconnect from VPN!
pause
exit

Notes on the batch file
  1. In the batchfile replace YourVPN with the exact name of the VPN that you need to create in Network Connections
  2. Replace mike with your username for the VPN
  3. Replace secretpassword with your VPN password
  4. Replace OurServer.rdp with the exact name of the Remote Desktop connection that you need to create in Start, All Programs, Accessories, Communications, Remote Desktop Connection.
Create the batch file by 'right-clicking' in a folder and selecting New.. Text Document This will prompt you for a name. Call it Connect.bat. Copy and paste the code above and save it.

For more details on rasdial.exe click on the link below

Saturday 7 February 2009

CPU running at 30% continuously

Today I came across a problem where the CPU was sitting constantly at around 30%. Below are the steps I used to determine the problem, the software tools used and the ultimate solution.

Using Task Manager and viewing the Processes Tab, I determined that the culprit was an entry for svchost.exe. Unfortunately this particular program is responsible for running the various services that Windows uses in the background. There were 4 instances of svchost.exe and no way in Task Manager to determine the one service that was running out of control.

After a short search on the web, I found a free program from Windows sysinternals called processor explorer.  

This is a tiny utility 1.6MB that needs no installing and displays all the processes that are currently running. It gives you tons of info about how much cpu time, memory and I/O each process is using.

When it came to the offending svchost.exe I was able to further explore by double-clicking on the process. This gave me a window that listed all the services running under its control. There was a list of about 20. I proceeded to 'Kill' each service, keeping an eye on the cpu usage graph. When I came to Network Connection Manager the cpu dropped to zero even though it said it had trouble stopping it.

So now I knew which service was responsible I went into Services from the Adminstrative Tools applet in Control Panel, found Network Connection Manager and disabled it.

After restarting the computer the cpu usage was sitting where I would expect at between 1% and 2%. I then manually started the offending service which seemed to start correctly and the cpu usage remained around the 1% mark from then on. I will see what happens when I restart at a later date as I still haven't got to the bottom of the problem but at least my computer is running a lot faster!.

I have since restarted the computer several times and the problem has gone!