batch mount a samba share

Posted by Dark Training on February 12, 2009 tags: | batch | windows

Mounting a samba share with a .bat file (batch file) is actually pretty easy.

net use <drive letter> \\unc or ip-address\share name

That is for a basic samba share, now lets suppose that you need to mount a share that has authentication. You have two options, the first is you can login once and save the credential:

net use <drive letter> \\unc or ip-address\share name /USER:<user-name> /savecred

This will ask you for the password of the user that you want to login as. Once entered it will remain in memory and you wont be prompted again. However this may be less desirable to some especially if you are doing this as a repeated batch process.

The alternate way is to pass the credential in the connection with password:

net use <drive letter> \\unc or ip-address\share name /USER:<user-name> password

This will connect to the share with the user name and password that you supply. One down side to this method is that the information is sent in clear text, which means that if some one is snooping on your network they can see the user name and password you are sending. You can alter the threat this poses by creating a service account that has no privileges EXCEPT to the share you are trying to access.

The last question I get asked is, "how do I assign the next available driver letter using NET?"
This one is pretty easy

net use <strong>*</strong> \\unc or ip-address\share name

That will mount the drive with the next available drive letter. This would be good for day to day map's but is not desirable for a script since you now do not know what drive to point to in your script.