Author Topic: HOW TO: Map samba shares as network drives for XP clients and redirect My Docs  (Read 12293 times)

Barrydocks

  • Zen Warrior
  • ***
  • Posts: 145
  • Karma: +4/-0
    • View Profile
Following on from this excellent post on mapping samba shares as networked drives for XP clients:
http://forum.zentyal.org/index.php?topic=2019.0
I have tried to collate some information to make this more useful and also redirect "My Documents" to the users /home folder.

Ok to make this work properly you need to have the appropriate logon.bat, logon.vbs file, a separate vbs file to map my documents called map_mydocs.vbs and a file to make the changes to samba configuration.

1. create a logon.bat file:
Code: [Select]
nano /home/samba/netlogon/logon.batpaste the following:
Quote
@echo off

REM removes any network drives in use by user
@if exist h: net use h: /delete /yes
@if exist g: net use g: /delete /yes
#@if exist p: net use p: /delete /yes #repeat this for any other mapped drives

REM Maps shares as network drives
cscript \\yourservername\netlogon\logon.vbs

REM Syncronize the time on the workstation to that of the server.
net time \\yourservername /set /yes

REM Map "My Documents" to the user's G: Drive
if EXIST "G:\" (
cscript "\\yourservername\netlogon\map_mydocs.vbs"
)

2. create the logon.vbs as per the original post:
Code: [Select]
nano /home/samba/netlogon/logon.vbspaste the following:
Code: [Select]
'First make sure all variables are dimensioned.
'This isn't necessary for functionality; it's for coding discipline only.
Option Explicit
'dimension all our variables
dim objNetwork
dim strDriveLetter, strRemotePath, strUser, strGrp
dim strGroupADSPath, strUserADSPath, grp

'This script will use the MapNetworkDrive method
'for each network drive mapped.

'We'll be using the Wscript.Network Object to enumerate the user as well as to map drives.
'We only need to instantiate it once at the beginning.
Set objNetwork = Wscript.CreateObject("Wscript.Network")

'First let's get the user name since we'll use it for mapping the home directory
'as well as checking group memberships.  
strUser = objNetwork.UserName

'In just about every network at least two drives are mapped:
'One for the user's home directory, and one for an organizational public share.
'We'll map those first since they don't depend on group memberships.

'User Drive to G:
strDriveLetter = "G:"
strRemotePath = "\\yourservername"
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath & "\" & strUser

'PublicShare Drive to P:
strDriveLetter = "P:"
strRemotePath = "\\yourservername\public"
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath


'next we'll enumerate groups and map drives for specific departmental memberships
'The code to check for memberships and map the drives is in a subroutine called IsMember.
'All we have to do is copy the same block over again for each group membership we want to check.
'The block only needs to set the string values for the group name, the desired drive letter, and the share path.
'Then it calls the IsMember subroutine down below.

'Q: for members of groupq
strGrp="groupq"
strDriveLetter = "q:"
strRemotePath = "\\yourservername\groupqpvt"
IsMember

'R: for members of groupr
strGrp="groupr"
strDriveLetter = "R:"
strRemotePath = "\\yourservername\grouprpvt"
IsMember

'Repeat for as many private groups and their respective enumerated shares as you wish.

'We're done with the login script.
'Let's tidy up variables first to make sure we're not leaving anything behind.
objNetwork = ""
strDriveLetter = ""
strRemotePath = ""
strUser = ""
strGrp = ""
strGroupADSPath = ""
strUserADSPath = ""
grp = ""
'That's all.  Close the script processor.
wscript.quit


sub IsMember
'set the directory service path to enumerate the group
strGroupADSPath = "WinNT://yourdomain/" & strGrp & ",group"
'poll the PDC for the group
set grp = GetObject(strGroupADSPath)
'set the user directory service path to enumerate the user
strUserADSPath = "WinNT://yourdomain/" & strUser
'Check membership in the group.  
If (grp.IsMember(strUserADSPath)) Then
'map the drive
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
End If
'clean up variables for next group check.
strGrp = ""
strDriveLetter = ""
strRemotePath = ""
'Rinse, lather and repeat this code as many times as needed.
End sub

3. Then create the script map_mydocs.vbs:
Code: [Select]
nano /home/samba/netlogon/map_mydocs.vbspaste the following:
Code: [Select]
Dim WSHShell
Set WSHShell = CreateObject("WScript.Shell")
WSHShell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal", "G:\"

You will also need to remove the line from smb.conf that maps the /home folder to H:
Code: [Select]
nano /etc/ebox/hooks/samba.postsetconfpaste the following:
Code: [Select]
#!/bin/sh
sed -i '/logon drive =/ d' /etc/samba/smb.conf
        exit 0
Make the file executable:
Code: [Select]
chmod +x /etc/ebox/hooks/samba.postsetconf
The samba.postsetconf script is called just as before samba is started and will make the appropriate alterations to the smb.conf which would otherwise be overwritten by zentyal

Please post any glaring errors or typos or any useful amendments  :)

Thanks to the guys who post on the original thread
« Last Edit: February 12, 2011, 01:59:00 pm by Barrydocks »

Luke

  • Zen Monk
  • **
  • Posts: 78
  • Karma: +3/-0
    • View Profile
Thanks for the script.

I cant get the group thing to work. Only the group Admin works. But do i need to put every group locally on the xp machine also?

Barrydocks

  • Zen Warrior
  • ***
  • Posts: 145
  • Karma: +4/-0
    • View Profile
Thanks for the script.

I cant get the group thing to work. Only the group Admin works. But do i need to put every group locally on the xp machine also?
just check the simple things like case (I think it might be case sensitive), also I have this setup with the LDAP users having real local accounts on the server, not sure if it will work with virtual users.

Don't know the answer to the last bit sorry

philmills

  • Zen Warrior
  • ***
  • Posts: 161
  • Karma: +8/-0
    • View Profile
A little word of warning regarding re-mapping My Documents, and about Windows XP default behaviour in that respect.

When My Documents is re-mapped to a network share, the windows Offline Files feature is automatically turned on.
In theory this is a great feature which would keep your files and folders synchronised and accessible even if there's a problem with the network connection.
Unfortunately, in practice (fairly widely documented on the net) the same feature can cause windows to switch at random into offline mode, rendering your network inaccessible, even when the network is connected.
Restarting the PC or logging off doesn't clear it, and your login scripts will no longer be able to run at logon.
This "feature" has caused us a lot headaches and for our users annoyance. And the only fix has been to manually turn off the feature in GPC on each workstation.

Christophe

  • Zen Warrior
  • ***
  • Posts: 170
  • Karma: +6/-1
    • View Profile
this script works with win 7 ????
DELL PowerEDGE R210 - ESXi 4.1 - 4 VM Zentyal

philmills

  • Zen Warrior
  • ***
  • Posts: 161
  • Karma: +8/-0
    • View Profile
The script is designed for XP.
The share mapping should work in Win 7 and Vista, but the My Documents redirect probably doesn't.