Speeding up the daily grind...
I telecomute for my current job. Yes, it is nice to be home when the kids get out of school, but it also means that I'm only a minute away from being back at work. And since my current position is on-call support for our customers, there are evenings that I make multiple trips "to the office" to assist customers. Making the most of my time matters, so speeding up the time needed to get the job done was important. One of the biggest pains was restarting the VPN and associated tools after logging off for the day.
For most organizations, telecomuters are a small fraction of the user base and thus a small fraction of the time the IT department has to devote to supporting their unique needs. In the course of the day, there are a number of resources I need to access (support ticketing system, e-mail, file shares on servers, the Internet, etc). The documented method that the IT team provides is aimed at the average user who needs to get in from the hotel or on the weekend, but not for those of us who rely on using it 8+ hours a day. As necessity is the mother of invention, I came up with this script to automate my regular login steps on my Windows 7 laptop.
Script named "SetMappings.bat":
@echo off
rem Set %USERNAME% to your login ID on the %DOMAIN%...
set VPNNAME="MyCompanyVPN"
set USERNAME={UserName}
set PASSWORD={Password}
set DOMAIN={CompanyDomain}
set PERSISTENT="/PERSISTENT:NO"
set NTPSVR=ntp.MyCompany.com
rasdial %VPNNAME% %USERNAME% %PASSWORD% /domain:%DOMAIN%
echo VPN connection returned: %ERRORLEVEL%
if NOT ERRORLEVEL 800 goto VPNGOOD
rem Errorlevel 800 == failed to connect.
echo Exiting on error %ERRORLEVEL%...
goto EOF
:VPNGOOD
echo Pause for 3 seconds until the VPN settles down..
ping -w 1000 -r 2 1.2.3.4 2>NUL: >NUL:
echo Setting time
net stop W32Time
net start W32Time
w32tm /config /syncfromflags:manual /manualpeerlist:%NTPSVR%
w32tm /config /update
w32tm /register
ping -w 1000 -r 2 1.2.3.4 2>NUL: >NUL:
echo Using NTP server:
w32tm /query /source /verbose
echo "Mapping to a domain controller"
net use \\dc2.MyCompany.com\ipc$ /user:%DOMAIN%\linder %PASSWORD% %PERSISTENT%
echo "Mapping to ex2k3"
net use \\ex2k3.MyCompany.com\ipc$ /user:%DOMAIN%\linder %PASSWORD% %PERSISTENT%
echo Mapping M...
net use M: /delete
net use M: \\MyCompany.com\dfs\data /user:%DOMAIN%\%username% %PERSISTENT% %PASSWORD%
echo Mapping N...
net use N: /delete
net use N: \\MyCompany.com\dfs\home /user:%DOMAIN%\%username% %PERSISTENT% %PASSWORD%
echo Mapping O...
net use O: /delete
net use O: \\MyCompany.com\dfs\apps /user:%DOMAIN%\%username% %PERSISTENT% %PASSWORD%
echo Mapping P...
net use P: /delete
net use P: \\MyCompany.com\dfs\tools /user:%DOMAIN%\%username% %PERSISTENT% %PASSWORD%
start "Outlook" "c:\Program Files (x86)\Microsoft Office\Office12\OUTLOOK.EXE" /recycle
cd "C:\Program Files (x86)\Avaya\Avaya one-X Communicator\"
start "SoftPhone" "C:\Program Files (x86)\Avaya\Avaya one-X Communicator\onexcui.exe"
cd "%HomePath%"
:EOF
Before you can run this on your system, you'll need to configure a couple of things:
- Configure the VPN and name it "MyCompanyVPN"
- For configuring the VPN under Windows 7, go to the "Network and Sharing Center" and click on the "Set up a new connection or network.
- Choose "Connect to a workplace", and choose "No, create a new connetion".
- Choose the "Use my Internet connection", then fill in the remainder of the windows with information provided by your IT department.
- The "Destination Name" field needs to match the value in line 3.
- Run the batch file as an administrator.
- To run the batch file as an administrator, open a command prompt as an administrator user by right-clicking on the "Command Prompt" under the Accessories folder, then choosing "Run as administrator".
- It will open up in the "C:\Windows\system32\" directory - place the SetMappings.bat file here.
Now to explain the script in detail:
Lines 3-9 : The script begins with defining the values used throughout the script so we only have to do it once. Some people might consider the password stored in the batch file a security risk. I agree, but the batch file is stored on my laptop which is never out of my posession, has a password to login, is firewalled, and is not sharing anything with the outside world. No, it's not impervious but the stored password is the method I chose. If you don't like it, you are free to modify the code to prompt for the password each tiem it is run.
Lines 11-18 : This tells the system to use "rasdial" (Explaination here) to connect to the named VPN connection setup supplying the name/password for the connection. If the "rasdial" exits with error code 800, then jump to the end of the script (line 86) and exit.
Line 21 : Assuming IP address 1.2.3.4 does NOT exist, this ping command will pause for three seconds. The "-r 2" says to retry two times after the initial ping fails to receive a reply after 1000 milliseconds
Lines 23-31 : This section makes sure my laptop clock is in sync with a good known source. Since the servers at work use an internal NTP server, I make sure my laptop is synchronized with that same server. This ensures that timestamps on e-mails and meeting invitations are consistent.
Lines 33-57 : My company uses many network drives to store data and other shared resources. There is a small compiled VisualBasic script that was provided, but it had issues whenever I ran it so I re-implemented the drive mappings using the "net use" command. Theoretically the mapping of the "IPC$" share (lines 34 and 37) should ensure my login credentials are shared throughout the servers in the domain. Additionaly, I map the required drive letters manually each time to ensure they are available.
Line 59 : I live most of my day responding to e-mails that customers and co-workers send me. This line runs Microsoft Outlook for me, the "/recycle" option should ensure that only a single copy of Outlook is started (just in case I had to re-run the script and Outlook was already running).
Line 62 : Since I am remote, I also use a softphone so I can talk with customers and co-workers. This line starts it for me - it prefers to start after the system is completely up and the VPN is stable, running this last helps ensure that.

Dan Linder
Reader Comments