Friday, June 15, 2012

Remotely copy hosts file to Windows 7


I have users that use laptops to work remote.  Unfortunately, when they VPN into to our server they can route, but they have problems with DNS.   So for these users I have a hosts file setup with the servers that they need.   Normally, I would just go to their C drive \\computername\c$  and drill down and manually replace it.   That works fine, but since I started using PDQ Deploy - From Admin Arsenal (This is a super Great application - I strongly recommend it) I wanted to see if I could deploy the hosts file to my Windows 7 Professional laptops using a batch file. 

I ran into all kinds of access denied problems.  Everywhere I searched on the Internet for help I ran into you can't it is a security feature in Windows 7.   I tried many things that I found online, but with no success.  That was until I ran into this post: http://stackoverflow.com/questions/10724591/how-to-remote-execute-an-elevated-remote-script-in-powershell.

To be honest, I didn't know what "start-process" was, but since I have googled it.  After putting together my script, I know there more efficient ways to write this script, but for a quick batch file that is easy to follow this is what I wrote.


start-process cmd.exe -verb runas /env /user:username@domainname.com
echo y |copy "\\servername\apps\misc\Windows 7 hosts file\hosts" C:\windows\temp
echo y |copy "\\servername\apps\misc\Windows 7 hosts file\hosts.orig" C:\windows\temp
echo y |copy C:\windows\temp\hosts C:\windows\system32\drivers\etc\hosts
echo y |copy C:\windows\temp\hosts.orig C:\windows\system32\drivers\etc\hosts.orig

Let me explain the script a little bit.   I didn't test, but maybe I could have copied from the network share to C:\windows\system32\drivers\etc directly, but I remember getting an error about copying from a network location, so I decided to copy to the temp directory on the computer and then copy over the original hosts file under the etc folder.   Please experiment or write better code or show me how you would have done this.    Also, I have a copy of an original windows 7 hosts file that I copy into place (the etc folder) so that if I ever need to change it or the user needs to change it while on the road it is there.   

The "echo y" just answers the questions that I do want to overwrite the original file.  I named my batch file copyhost.bat

Now that I have the script written Let's turn to PDQ Deploy and set it up to depoly the hosts file.




If you are already familiar with PDQ Deploy you know how easy it is to setup deploying files.   I just choose my batch file and include the entire directory which includes "copyhost.bat", "hosts", "hosts.orig"  and that it.  I save it and the I just deploy it by choosing deploy and then choosing which computers I want to deploy this to.  Within seconds I'm done and I have a status report saying "successful".

Let me know if you figured out another way to do this.

2 comments:

Perry L. said...

I just used this program and this script that you created. I modified the script a little to not include the second location of the original hosts file. I ran it using this same program and VOILA! It worked like a charm. Definitely passing this along to my colleagues.

Perry L. said...

I just used this program and this script that you created. I modified the script a little to not include the second location of the original hosts file. I ran it using this same program and VOILA! It worked like a charm. Definitely passing this along to my colleagues.