Mapping Drives at Login with script... incorrect username appearing

echalupka
New Contributor III

Hello,

The organization that I work for maps local samba shares to macs at login when on campus. We use the following script (which has worked flawlessly for years). Unfortunately when running from login hook, the username field now fills with "root" everytime, and the user has to re-input their username to map to the drive. Any ideas on how to fix this?

 

username="$3"
    if [ -z "$username" ]; then # Checks if the variable is empty (user running script from Self Service)
        username="$USER"
    fi
    echo "User: $username"

# Mount the drive
mount_script=`/usr/bin/osascript > /dev/null << EOT
  delay 10
tell application "Finder"
mount volume "smb://[SERVERIP]/[Drive]/$username/Documents"
end tell
EOT`

exit 0

 

1 ACCEPTED SOLUTION

@echalupka Our HomeDrive is named My Documents below is a sanitized version of my script. Hope this helps.

 

 

#! /bin/sh

current_User=$(/usr/bin/stat -f%Su /dev/console)
homeDrive_Path="smb://[SERVER]/[PERSONAL_SHARE]/$current_User/My Documents"
homeDrive_Name="My Documents"


sudo -u "${current_User}" osascript <<EOT
on listMountedDisks()
list disks
end listMountedDisks

if (listMountedDisks() does not contain "${homeDrive_Name}") then
mount volume "${homeDrive_Path}"
end if

EOT

 

 

View solution in original post

6 REPLIES 6

geoff_widdowson
Contributor II

It could be down to server updates. If the smb share is on a Windows Server, older smb protocols like smb 1 have been deprecated due to no enryption. I had issues with smb print shares and had to switch to lpd. I don't use smb drive shares anymore, so not seen this issue myself.

thanks - hoping that isn't the culprit, although we did just run into that same SMB error with SMB print shares and had to update to lpd in that scenario.

mrheathjones
New Contributor III

For good measure i always use the following when the script needs to be run in the user context or reflect data related to the user. I've had issues with $3 parameter in the past and using the following has never failed me. 

currentUser=$(stat -f "%Su" /dev/console)

 

Cheers!

echalupka
New Contributor III

Thanks @mrheathjones -- forgive my naivety; would this be the correct update to the script?

 

username="$(stat -f"%Su" /dev/console)"

# Mount the drive
	mount_script=`/usr/bin/osascript > /dev/null << EOT
	   delay 10
	tell application "Finder"
	mount volume "smb://[SERVERIP]/[Drive]/$username/Documents"
	end tell
EOT`

exit 0

 

@echalupka Our HomeDrive is named My Documents below is a sanitized version of my script. Hope this helps.

 

 

#! /bin/sh

current_User=$(/usr/bin/stat -f%Su /dev/console)
homeDrive_Path="smb://[SERVER]/[PERSONAL_SHARE]/$current_User/My Documents"
homeDrive_Name="My Documents"


sudo -u "${current_User}" osascript <<EOT
on listMountedDisks()
list disks
end listMountedDisks

if (listMountedDisks() does not contain "${homeDrive_Name}") then
mount volume "${homeDrive_Path}"
end if

EOT

 

 

Thanks @mrheathjones -- Updated with the info you provided and we're back up and running!

Eric