Mounting Windows Hosted Network Drives

user-kmLBwPhtha
New Contributor

Hi Everyone,

I am very new to Jamf and I have scoured the forums high and low. What I am needing to do is mount three separate directories hosted on a Windows file server. The directories need to be seen as separate directories when mounted on a Mac. For instance we have a: G: drive, I: drive, and H: drive. All of these drives are just directories that are shared from one server. The H: drive is what would be an employee's self storage for work. I believe it has an object in Active Directory because it is mapped like this in a Windows environment "servernameshortname$." I would like to know if anyone has recommendations on a script that will take care of this, or if Jamf Pro has a feature built into it that will allow this to happen, that's great too! All of the Mac's on campus are binded to Active Directory so that any user on campus has the ability to login to them, but they are placed in a "No-inheritance" container.

Here is a copy of the script I have started with, but I am not seeing any of the mounts at all.

"#!/bin/bash

theuser=$(/usr/bin/who | awk '/console/{ print $1 }')
/usr/bin/osascript > /dev/null << EOT

tell application "Finder" activate mount volume "smb://servername/shared directory/" mount volume "smb://servername/shared directory/" end tell

EOT

echo $theuser
killall cfprefsd
defaults write com.apple.finder ShowMountedServersOnDesktop true
killall -HUP Finder"

The server name would be replaced with our server, and the shared directory is replaced with the name of the shared directory. I have not had the chance to tackle the H: drive much. It will be different because it is user specific.

3 REPLIES 3

PaulHazelden
Valued Contributor

I am using Catalina here, and I gave up trying to script this, so that it works automatically. Osascript needs permission to launch and then to access Finder, and when a launchAgent sets this off for me, it never asks for this permission.

I went to Automator and used it to create an Application. Now my Users can run the Applications and get each drive mounted. The Automator App is a mix of Shell script and Apple Script, the shell script finds the correct attribute to populate the H drive. It still needs to be given permission to access Finder, but once done it works. I also have added in a launch of the Applications to my script, and once it has run the once and been given permission to run, it works.

georgecm12
Contributor III

I'd look at a shell script rather than AppleScript. Here's an example of a script I use to mount our "Q:" and "X:" drives from Active Directory:

#!/bin/bash 

currentuser=$USER 
searchpath=$(dscl localhost -read /Search CSPSearchPath|grep 'Active'|awk '{$1=$1}1') 
smbhome=$(dscl "$searchpath" -read /Users/$currentuser SMBHome)

if [ $(id -u $currentuser) -lt 1000 ] ; then 
    logger -t "edu.school.networkdrive" user $currentuser is a local account. 
    exit 
fi 

if [ ! -d /Users/${USER}/Volumes ]; then
    mkdir /Users/${USER}/Volumes
    chflags hidden /Users/${USER}/Volumes
fi

# Convert the smbhome from a UNC location to URI format (sans the "smb:" part at the beginning)
# Split the smbhome at the ": ", take the second half, percent-encode it, then swap the slashes
smbhome=$(echo 'smb:'"$(echo $smbhome|awk -F ': ' '{printf $2}'|perl -p -e 's/([^\:A-Za-z0-9-._~])/sprintf("%%%02X", ord($1))/seg'|sed -e 's/\///g')")

if [ "$smbhome" == 'smb:' ] ; then 
    logger -t "edu.school.networkdrive" could not get smb home for user $currentuser.  Not defined for the account? 
else 
    # Mount the Q-drive
    QDRIVE=/Users/${USER}/Volumes/Q-Drive
    mkdir $QDRIVE
    mount -t smbfs $smbhome $QDRIVE

    if [[ !  `mount | grep $QDRIVE` ]]; then
        rmdir $QDRIVE
        logger -t "edu.school.networkdrive" User $currentuser has a Q-drive defined, but no folder exists. 
    fi
fi

distinguishedName=$(dscl "$searchpath" -read /Users/$currentuser | grep "distinguishedName" -A1 | tail -n 1 | grep -c -i "student")

if [[ ! $distinguishedName ]] ; then 
    logger -t "X-Drive" User $currentuser is not a student, and has no X-drive.
else
    # Mount the X-drive
    XDRIVE=/Users/${USER}/Volumes/X-Drive
    mkdir $XDRIVE
    mount -t smbfs "smb://servername/directory/$currentuser/directory/" $XDRIVE

    if [[ ! `mount | grep $XDRIVE` ]]; then
        rmdir $XDRIVE
        logger -t "X-Drive" User $currentuser is a student, but has no X-drive.
    fi
fi

exit 0

I use this script in conjunction with a LaunchAgent, so that the drives get connected at login.

PaulHazelden
Valued Contributor

Using Automator...

In the shell script I use /usr/bin/ldapsearch to find the ndsHomeDirectory attribute for the current user. We use eDirectory here, so this bit will probably be different for you.

This tells me where their H drive is located. Then I set that result to be the input for an Apple script...

on run {input, parameters}
    set sharePoint to input
    tell application "Finder"
        activate
        mount volume "smb://" & sharePoint
    end tell
    return input
end run

You will need to sort a way to pick up the Home server address for each user.
You can easily throw in more Apple scripts to mount more share points. If they are known shares, all you do is change the Applescript a bit...

on run {input, parameters}
    tell application "Finder"
        activate
        mount volume "smb://<SHARE POINT ADDRESS>" 
    end tell
    return input
end run

Save as a workflow, you can easily test it from Automator. Once you are happy make it into an application. Package and distribute it.
At worst, your users will have to manually run the app. You could make a LaunchAgent to set the app off at user log in. Or add an open command to an existing script. The first time the App launches on Catalina it will need to be given permission to access Finder. This will be for each user for each Mac. But once done it will be happy.