Hi,
Does anyone use a Mac script to mount AD SMB Path as home folder. I am trying to get it working but struggling to get it to mount the SMB path.
The current script looks at the current user in AD and gets the SMB Path. But doesn't mount it. Does anyone use a script to do the same?
Script:
#!/bin/bash
# Log writing function
writelog() {
echo "$(date): $1" | tee -a /var/log/smb_mount.log
}
writelog "STARTING: User drive mount"
# Check if the user's personal network drive is already mounted
isMounted=$(mount | grep -c "/Volumes/$USER")
if [ $isMounted -ne 0 ]; then
writelog "Network share already mounted for $USER"
exit 0
fi
# Retrieve SMBHome attribute for the current user
writelog "Retrieving SMBHome attribute for $USER"
ShortDomainName=$(dscl /Active\\ Directory/ -read . | grep SubNodes | sed 's|SubNodes: ||g')
adHome=$(dscl /Active\\ Directory/"$ShortDomainName"/All\\ Domains -read /Users/$USER SMBHome)
if [ $? -ne 0 ]; then
writelog "ERROR: Cannot read ${USER}'s SMBHome attribute from '/Active Directory/$ShortDomainName/All Domains'. Exiting script."
exit 1
else
adHome=$(echo "${adHome}" | sed 's|SMBHome:||g' | sed 's/^[\\\\]*//' | sed 's:\\\\:/:g' | sed 's/ \\/\\///g' | tr -d '\\n' | sed 's/ /%20/g')
fi
# Check if the SMBHome attribute is populated
if [ -z "$adHome" ]; then
writelog "ERROR: ${USER}'s SMBHome attribute does not have a value set. Exiting script."
exit 1
else
writelog "Active Directory user's SMBHome attribute identified as $adHome"
fi
# Mount the network home
osascript <<EOT
try
mount volume "smb://${adHome}" as user name "$USER" with password "$PASSWORD"
on error errMsg number errNum
tell application "System Events" to display dialog "Failed to mount SMB share: " & errMsg buttons {"OK"} default button 1
end try
EOT
# Check if mount was successful
if mount | grep "/Volumes/$USER"; then
writelog "SMB share mounted successfully."
else
writelog "Failed to mount SMB share."
exit 1
fi
# Set the SMB share as the home directory
writelog "Setting SMB share as home directory"
dscl . -create /Users/$USER NFSHomeDirectory "/Volumes/$USER"
writelog "Script completed"
exit 0