Mount SMB share and place under Finder Locations or Desktop?

robert1051
New Contributor II

Hello all,

    I've tried difference scripts (bash and AppleScript) but none seem to work for me.  Basically, (and I see this has been asked a lot but varies), I would like for any user that logs into the Macbook, connect to their smb share AND place that drive as a shortcut in either the Locations (in Finder) OR Desktop.  Users should not be prompted for their username and password.  Here's what the smb path looks like:

smb://servername/Userdata/UserName/My%20Documents

Where UserName can be any user that logs in to that Mac device.  But do not prompt for username and password if possible.  If password is a must, then I'll take whatever I can work with.

5 REPLIES 5

AJPinto
Honored Contributor

You can adjust a finder setting (configuration profile) to show network drives on the desktop, or you can make an alias and put it on their desktop.

 

How do you plan on authenticating the network share for the user? The network share needs to get a ticket from somewhere or something to identify and authenticate the user. You either need some 3rd party SSO Plug-In or to configure Apples SSO Extension. The user would still need to log in to the plug-in or Extension to generate a ticket that can be passed to the network share. Apples Platform SSO may change this once that gets off the ground. Once you have a ticket, its a matter of making sure your server supports that ticket and make it use the ticket.

Kerberos Single Sign-on extension with Apple devices - Apple Support

Microsoft Enterprise SSO plug-in for Apple devices - Microsoft Entra | Microsoft Learn

HCS Technology Group - A Guide for Configuring the macOS Catalina Kerberos Single Sign-On Extension ...

robert1051
New Contributor II

The Macs are binded to Active Directory.  So I can get it to the point where if a user logs in they get prompted to enter their password to connect to smb server.  This might be fine.  But I can only get it to work if I put the actual username in the UserName field I described in the path.  But users vary.  

AJPinto
Honored Contributor

Something like this would prompt the user for their password, map the network drive. 

 

#!/bin/bash

loggedInUser=$(/usr/bin/who | awk '/console/{ print $1 }')

echo "Prompting for userToAdd credentials."

## Prompt for Password
userPass=$(/usr/bin/osascript<<END
  tell application "System Events"
  activate
  set the answer to text returned of (display dialog "Enter your Password:" default answer "" with hidden answer buttons {"Continue"} default button 1)
  end tell
END
)

Share="//$UserID:$UserPass@path/to/share"

mount_smbfs -d 777 -f 777 smb:$Share /Users/$loggedInUser

 

 

If Kerberos tickets are happy, something like this should work.

 

#!/bin/bash

loggedInUser=$(/usr/bin/who | awk '/console/{ print $1 }')
Share="SMB://path/to/share"

sudo -u $loggedInUser osascript -e 'mount volume "'$True_Path'"'

 

 

This post has a good script for domain bound devices. It uses Apple Script to mount the share as the currently logged in user. We are not domain bound anymore so I can test to see if it still works.

MacOS - Map Network Drives - Jamf Nation Community - 169764

 

 

robert1051
New Contributor II

Regarding the latter, I had something like this but the logged in user is right smack in the middle of the smb path.

smb://servername/Userdata/USERNAME/My%20Documents.

How do I replace the username with the current logged in user?

Bol
Contributor III

Store all the share details in a variable to use later. Or even add the script to a jamf policy and use options to parse the info to your script if you will be using this more than once.

protocol="smb"
serverName="ServerName"
shareName="ShareName"
currentUser=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')
mountPath="${protocol}://${serverName}/${shareName}/${currentUser}/My%20Documents"

For a desktop alias, similar to AJ's answer use Applescript but call as the logged in user;

aliasName="${shareName} drive"
uid=$(id -u "${currentUser}")

runAsUser() {  
launchctl asuser "${uid}" sudo -u "${currentUser}" "$@"
}


runAsUser osascript <<EOT
on listMountedDisks()
    list disks
end listMountedDisks

if (listMountedDisks() does not contain "${shareName}") then
    mount volume "${mountPath}"
end if

set shareMount to POSIX file "/Volumes/$shareName" as alias

tell application "Finder"
    make new alias to shareMount at desktop
    set name of result to "${aliasName}"
    open disk "${shareName}"
end tell

EOT
Also remove another prompt by;

defaults write /Library/Preferences/com.apple.NetworkAuthorization AllowUnknownServers -bool YES