Skip to main content
Solved

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

  • December 28, 2021
  • 6 replies
  • 17 views

Forum|alt.badge.img+4

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

 

Best answer by mrheathjones

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

 

 

6 replies

geoff_widdowson
Forum|alt.badge.img+8

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.


Forum|alt.badge.img+10
  • Contributor
  • December 28, 2021

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!


Forum|alt.badge.img+4
  • Author
  • Contributor
  • December 29, 2021

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

 


Forum|alt.badge.img+4
  • Author
  • Contributor
  • December 29, 2021

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.


Forum|alt.badge.img+10
  • Contributor
  • Answer
  • December 29, 2021

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

 

 


Forum|alt.badge.img+4
  • Author
  • Contributor
  • December 29, 2021

@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